Skip to content

Commit

Permalink
feat: copy style when insert tab element #974
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Jan 22, 2025
1 parent f0ffe31 commit ae8bbb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/editor/core/event/handlers/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function input(data: string, host: CanvasEvent) {
const cursor = draw.getCursor()
cursor.clearAgentDomValue()
}
const { TEXT, HYPERLINK, SUBSCRIPT, SUPERSCRIPT, DATE } = ElementType
const { TEXT, HYPERLINK, SUBSCRIPT, SUPERSCRIPT, DATE, TAB } = ElementType
const text = data.replaceAll(`\n`, ZERO)
const { startIndex, endIndex } = rangeManager.getRange()
// 格式化元素
Expand All @@ -47,6 +47,7 @@ export function input(data: string, host: CanvasEvent) {
(!copyElement.title?.disabled && !copyElement.control?.disabled)
) {
const nextElement = elementList[endIndex + 1]
// 文本、超链接、日期、上下标:复制所有信息(元素类型、样式、特殊属性)
if (
!copyElement.type ||
copyElement.type === TEXT ||
Expand All @@ -64,10 +65,12 @@ export function input(data: string, host: CanvasEvent) {
}
})
}
// 使用默认样式(组合输入法输入 || 无法匹配元素类型时逻辑
if (defaultStyle) {
// 仅复制样式:存在默认样式设置 || 无法匹配文本类元素时(TAB
if (defaultStyle || copyElement.type === TAB) {
EDITOR_ELEMENT_STYLE_ATTR.forEach(attr => {
const value = defaultStyle[attr as keyof IRangeElementStyle]
const value =
defaultStyle?.[attr as keyof IRangeElementStyle] ||
copyElement[attr]
if (value !== undefined) {
newElement[attr] = value as never
}
Expand Down
14 changes: 11 additions & 3 deletions src/editor/core/event/handlers/keydown/tab.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { EDITOR_ELEMENT_STYLE_ATTR } from '../../../../dataset/constant/Element'
import { ElementType } from '../../../../dataset/enum/Element'
import { MoveDirection } from '../../../../dataset/enum/Observer'
import { IElement } from '../../../../interface/Element'
import { pickObject } from '../../../../utils'
import { formatElementContext } from '../../../../utils/element'
import { CanvasEvent } from '../../CanvasEvent'

Expand All @@ -17,14 +19,20 @@ export function tab(evt: KeyboardEvent, host: CanvasEvent) {
direction: evt.shiftKey ? MoveDirection.UP : MoveDirection.DOWN
})
} else {
const rangeManager = draw.getRange()
const elementList = draw.getElementList()
const { startIndex, endIndex } = rangeManager.getRange()
// 插入tab符
const anchorStyle = rangeManager.getRangeAnchorStyle(elementList, endIndex)
// 仅复制样式
const copyStyle = anchorStyle
? pickObject(anchorStyle, EDITOR_ELEMENT_STYLE_ATTR)
: null
const tabElement: IElement = {
...copyStyle,
type: ElementType.TAB,
value: ''
}
const rangeManager = draw.getRange()
const { startIndex } = rangeManager.getRange()
const elementList = draw.getElementList()
formatElementContext(elementList, [tabElement], startIndex, {
editorOptions: draw.getOptions()
})
Expand Down

0 comments on commit ae8bbb8

Please sign in to comment.