diff --git a/src/editor/assets/css/index.css b/src/editor/assets/css/index.css index 0f50964..d976bd2 100644 --- a/src/editor/assets/css/index.css +++ b/src/editor/assets/css/index.css @@ -27,6 +27,7 @@ position: absolute; outline: none; background-color: #000000; + pointer-events: none; } .cursor--animation { diff --git a/src/editor/core/event/CanvasEvent.ts b/src/editor/core/event/CanvasEvent.ts index cde7e8c..9e351ce 100644 --- a/src/editor/core/event/CanvasEvent.ts +++ b/src/editor/core/event/CanvasEvent.ts @@ -19,6 +19,7 @@ import { HistoryManager } from "../history/HistoryManager" import { Listener } from "../listener/Listener" import { Position } from "../position/Position" import { RangeManager } from "../range/RangeManager" +import { LETTER_REG, NUMBER_LIKE_REG } from "../../dataset/constant/Regular" export class CanvasEvent { @@ -62,6 +63,7 @@ export class CanvasEvent { this.pageContainer.addEventListener('mousedown', this.mousedown.bind(this)) this.pageContainer.addEventListener('mouseleave', this.mouseleave.bind(this)) this.pageContainer.addEventListener('mousemove', this.mousemove.bind(this)) + this.pageContainer.addEventListener('dblclick', this.dblclick.bind(this)) } public setIsAllowDrag(payload: boolean) { @@ -376,6 +378,49 @@ export class CanvasEvent { } } + public dblclick() { + const cursorPosition = this.position.getCursorPosition() + if (!cursorPosition) return + const { value, index } = cursorPosition + const elementList = this.draw.getElementList() + // 判断是否是数字或英文 + let upCount = 0 + let downCount = 0 + const isNumber = NUMBER_LIKE_REG.test(value) + if (isNumber || LETTER_REG.test(value)) { + // 向上查询 + let upStartIndex = index - 1 + while (upStartIndex > 0) { + const value = elementList[upStartIndex].value + if ((isNumber && NUMBER_LIKE_REG.test(value)) || (!isNumber && LETTER_REG.test(value))) { + upCount++ + upStartIndex-- + } else { + break + } + } + // 向下查询 + let downStartIndex = index + 1 + while (downStartIndex < elementList.length) { + const value = elementList[downStartIndex].value + if ((isNumber && NUMBER_LIKE_REG.test(value)) || (!isNumber && LETTER_REG.test(value))) { + downCount++ + downStartIndex++ + } else { + break + } + } + } + // 设置选中区域 + this.range.setRange(index - upCount - 1, index + downCount) + // 刷新文档 + this.draw.render({ + isSubmitHistory: false, + isSetCursor: false, + isComputeRowList: false + }) + } + public input(data: string) { const isReadonly = this.draw.isReadonly() if (isReadonly) return diff --git a/src/editor/core/position/Position.ts b/src/editor/core/position/Position.ts index 0ac401d..3d51906 100644 --- a/src/editor/core/position/Position.ts +++ b/src/editor/core/position/Position.ts @@ -148,8 +148,8 @@ export class Position { const isHead = x < this.options.margins[3] // 是否在头部 if (isHead) { - const headIndex = positionList.findIndex(p => p.rowNo === firstLetterList[j].rowNo) - curPositionIndex = ~headIndex ? headIndex : index + const headIndex = positionList.findIndex(p => p.pageNo === curPageNo && p.rowNo === firstLetterList[j].rowNo) + curPositionIndex = ~headIndex ? headIndex - 1 : index } else { curPositionIndex = index } diff --git a/src/editor/dataset/constant/Regular.ts b/src/editor/dataset/constant/Regular.ts new file mode 100644 index 0000000..63be9dd --- /dev/null +++ b/src/editor/dataset/constant/Regular.ts @@ -0,0 +1,4 @@ +export const NUMBER_REG = /[0-9]/ +export const LETTER_REG = /[a-zA-Z]/ +export const NUMBER_LIKE_REG = /[0-9.]/ +export const CHINESE_REG = /[\u4e00-\u9fa5]/ \ No newline at end of file diff --git a/src/editor/utils/clipboard.ts b/src/editor/utils/clipboard.ts index 23ce309..e074888 100644 --- a/src/editor/utils/clipboard.ts +++ b/src/editor/utils/clipboard.ts @@ -39,5 +39,5 @@ export function writeTextByElementList(elementList: IElement[]) { } pickTextFromElement(elementList) if (!text) return - writeText(text) + writeText(text.replace(new RegExp(`^${ZERO}`), '')) } \ No newline at end of file