From 9685493004cf50c3f03c5f922b128fbb2713b68c Mon Sep 17 00:00:00 2001 From: zhangw Date: Wed, 30 Oct 2024 22:08:23 +0800 Subject: [PATCH] feat: update --- .../text-x/build-utils/text-x-utils.ts | 2 +- .../core/src/docs/data-model/text-x/utils.ts | 33 +++---------------- .../src/views/hyper-link-edit/index.tsx | 2 +- .../src/utils/replace-selection-factory.ts | 5 +-- 4 files changed, 10 insertions(+), 32 deletions(-) diff --git a/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts b/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts index 86d7ef33248..7bb56ea6e24 100644 --- a/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts +++ b/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts @@ -270,7 +270,7 @@ export const replaceSelectionTextX = (params: IReplaceSelectionTextXParams) => { const body = doc.getSelfOrHeaderFooterModel(segmentId)?.getBody(); if (!body) return false; - const oldBody = selection.collapsed ? null : getBodySlice(body, selection.startOffset, selection.endOffset - 1); + const oldBody = selection.collapsed ? null : getBodySlice(body, selection.startOffset, selection.endOffset); const diffs = textDiff(oldBody ? oldBody.dataStream : '', insertBody.dataStream); let cursor = 0; const actions = diffs.map(([type, text]) => { diff --git a/packages/core/src/docs/data-model/text-x/utils.ts b/packages/core/src/docs/data-model/text-x/utils.ts index f943d2aaf2a..b9f12784492 100644 --- a/packages/core/src/docs/data-model/text-x/utils.ts +++ b/packages/core/src/docs/data-model/text-x/utils.ts @@ -224,39 +224,16 @@ export function normalizeBody(body: IDocumentBody): IDocumentBody { export function getCustomRangeSlice(body: IDocumentBody, startOffset: number, endOffset: number) { const { customRanges = [] } = body; - let leftOffset = 0; - let rightOffset = 0; + const leftOffset = 0; + const rightOffset = 0; const relativeCustomRanges = customRanges .filter((customRange) => Math.max(customRange.startIndex, startOffset) <= Math.min(customRange.endIndex, endOffset - 1)) .map((range) => ({ ...range, - startIndex: range.startIndex, - endIndex: range.endIndex, + startIndex: Math.max(range.startIndex, startOffset), + endIndex: Math.min(range.endIndex, endOffset - 1), })); - if (relativeCustomRanges.length) { - relativeCustomRanges.forEach((customRange) => { - if (customRange.startIndex < startOffset) { - leftOffset += 1; - } - if (customRange.endIndex > (endOffset - 1)) { - rightOffset += 1; - } - }); - - for (let i = 0; i < leftOffset; i++) { - const range = relativeCustomRanges[i]; - range.startIndex = startOffset - leftOffset + i; - } - if (rightOffset) { - const sorted = [...relativeCustomRanges].sort((pre, aft) => aft.endIndex - pre.endIndex); - for (let i = 0; i < rightOffset; i++) { - const range = sorted[i]; - range.endIndex = endOffset + rightOffset - i - 1; - } - } - } - return { customRanges: relativeCustomRanges.map((range) => ({ ...range, @@ -280,7 +257,7 @@ export function getCustomDecorationSlice(body: IDocumentBody, startOffset: numbe customDecorationSlice.push({ ...copy, startIndex: Math.max(copy.startIndex - startOffset, 0), - endIndex: Math.min(copy.endIndex, endOffset) - startOffset, + endIndex: Math.min(copy.endIndex, endOffset - 1) - startOffset, }); } }); diff --git a/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx b/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx index 47c09085fa9..a3fd3761392 100644 --- a/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx +++ b/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx @@ -66,7 +66,7 @@ export const DocHyperLinkEdit = () => { const matchedRange = body?.customRanges?.find((i) => editing?.linkId === i.rangeId); if (doc && matchedRange) { setLink(matchedRange.properties?.url ?? ''); - setLabel(BuildTextUtils.transform.getPlainText(getBodySlice(body!, matchedRange.startIndex, matchedRange.endIndex).dataStream)); + setLabel(BuildTextUtils.transform.getPlainText(getBodySlice(body!, matchedRange.startIndex, matchedRange.endIndex + 1).dataStream)); } return; } diff --git a/packages/docs/src/utils/replace-selection-factory.ts b/packages/docs/src/utils/replace-selection-factory.ts index 2a833a6e18e..395345761c7 100644 --- a/packages/docs/src/utils/replace-selection-factory.ts +++ b/packages/docs/src/utils/replace-selection-factory.ts @@ -59,8 +59,8 @@ export function replaceSelectionFactory(accessor: IAccessor, params: IReplaceSel return false; } const textRanges = params.textRanges ?? [{ - startOffset: selection.startOffset + insertBody.dataStream.length + 1, - endOffset: selection.startOffset + insertBody.dataStream.length + 1, + startOffset: selection.startOffset + insertBody.dataStream.length, + endOffset: selection.startOffset + insertBody.dataStream.length, collapsed: true, segmentId, }]; @@ -70,6 +70,7 @@ export function replaceSelectionFactory(accessor: IAccessor, params: IReplaceSel body: insertBody, doc: docDataModel, }); + if (!textX) { return false; }