diff --git a/src/editor/core/cursor/Cursor.ts b/src/editor/core/cursor/Cursor.ts index edfbd9c..8a7e07b 100644 --- a/src/editor/core/cursor/Cursor.ts +++ b/src/editor/core/cursor/Cursor.ts @@ -41,7 +41,7 @@ export class Cursor { agentCursorDom.focus() agentCursorDom.setSelectionRange(0, 0) }) - // fillText位置 + 文字基线到底部距离 - 模拟光标偏移量 + // fillText位置 + 文字基线到底部距离 - 模拟光标偏移量 const descent = metrics.boundingBoxDescent < 0 ? 0 : metrics.boundingBoxDescent const cursorTop = (leftTop[1] + ascent) + descent - (cursorHeight - offsetHeight) const curosrleft = rightTop[0] diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index 3029e46..b4ac4ff 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -163,6 +163,7 @@ export class Draw { this.position.setPositionList([]) const positionList = this.position.getPositionList() // 基础信息 + const { defaultSize } = this.options const canvasRect = this.canvas.getBoundingClientRect() // 绘制背景 this.background.render(canvasRect) @@ -180,7 +181,7 @@ export class Draw { height: 0, ascent: 0, elementList: [], - rowFlex: this.elementList?.[1].rowFlex + rowFlex: this.elementList?.[1]?.rowFlex }) } for (let i = 0; i < this.elementList.length; i++) { @@ -212,7 +213,7 @@ export class Draw { this.ctx.font = this.getFont(element) const fontMetrics = this.ctx.measureText(element.value) metrics.width = fontMetrics.width - metrics.boundingBoxAscent = fontMetrics.actualBoundingBoxAscent + metrics.boundingBoxAscent = i === 0 ? defaultSize : fontMetrics.actualBoundingBoxAscent metrics.boundingBoxDescent = fontMetrics.actualBoundingBoxDescent } const ascent = metrics.boundingBoxAscent + rowMargin @@ -342,7 +343,9 @@ export class Draw { if (isSubmitHistory) { const self = this const oldElementList = deepClone(this.elementList) + const { startIndex, endIndex } = this.range.getRange() this.historyManager.execute(function () { + self.range.setRange(startIndex, endIndex) self.elementList = deepClone(oldElementList) self.render({ curIndex, isSubmitHistory: false }) }) diff --git a/src/editor/core/event/CanvasEvent.ts b/src/editor/core/event/CanvasEvent.ts index dd65e9f..fd6f5fb 100644 --- a/src/editor/core/event/CanvasEvent.ts +++ b/src/editor/core/event/CanvasEvent.ts @@ -245,7 +245,7 @@ export class CanvasEvent { public paste(evt: ClipboardEvent) { const text = evt.clipboardData?.getData('text') - this.input(text || '') + this.input(text?.replaceAll(`\n`, ZERO) || '') evt.preventDefault() } diff --git a/src/editor/index.ts b/src/editor/index.ts index c3f04aa..5760a70 100644 --- a/src/editor/index.ts +++ b/src/editor/index.ts @@ -42,7 +42,7 @@ export default class Editor { canvas.height = parseInt(canvas.style.height) * dpr canvas.style.cursor = 'text' ctx.scale(dpr, dpr) - if (elementList[0].value !== ZERO) { + if (elementList[0]?.value !== ZERO) { elementList.unshift({ value: ZERO })