Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 committed Oct 30, 2024
1 parent 9cde333 commit 9685493
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand Down
33 changes: 5 additions & 28 deletions packages/core/src/docs/data-model/text-x/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/docs/src/utils/replace-selection-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}];
Expand All @@ -70,6 +70,7 @@ export function replaceSelectionFactory(accessor: IAccessor, params: IReplaceSel
body: insertBody,
doc: docDataModel,
});

if (!textX) {
return false;
}
Expand Down

0 comments on commit 9685493

Please sign in to comment.