Skip to content

Commit 8dfd733

Browse files
e11sygohabereg
andauthored
imp(dom-adapters): save formatting on block split (#105)
* imp(dom-adapters): save formatting on block split * imp(dom-adapter): cover case with different inputs * chore(dom-adapters): lint fix * Update packages/dom-adapters/src/BlockToolAdapter/index.ts Co-authored-by: George Berezhnoy <[email protected]> * imp(dom-adapters): description * chore(dom-adapters): fix --------- Co-authored-by: George Berezhnoy <[email protected]>
1 parent d4da224 commit 8dfd733

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

packages/dom-adapters/src/BlockToolAdapter/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
359359
const currentValue = this.#model.getText(this.#blockIndex, key);
360360
const newValueAfter = currentValue.slice(end);
361361

362+
const relatedFragments = this.#model.getFragments(this.#blockIndex, key, end, currentValue.length);
363+
364+
/**
365+
* Fragment ranges bounds should be decreased by end index, because end is the index of the first character of the new block
366+
*/
367+
relatedFragments.forEach(fragment => {
368+
fragment.range[0] = Math.max(0, fragment.range[0] - end);
369+
fragment.range[1] -= end;
370+
});
371+
362372
this.#model.removeText(this.#config.userId, this.#blockIndex, key, start, currentValue.length);
363373
this.#model.addBlock(
364374
this.#config.userId,
@@ -368,7 +378,7 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
368378
[key]: {
369379
$t: 't',
370380
value: newValueAfter,
371-
fragments: [],
381+
fragments: relatedFragments,
372382
},
373383
},
374384
},

packages/dom-adapters/src/CaretAdapter/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,29 @@ export class CaretAdapter extends EventTarget {
264264

265265
const selection = document.getSelection()!;
266266

267-
let absoluteStartOffset = getAbsoluteRangeOffset(input, selection.anchorNode!, selection.anchorOffset);
268-
let absoluteEndOffset = getAbsoluteRangeOffset(input, selection.focusNode!, selection.focusOffset);
267+
let isStartEqualsCurrent = false;
268+
let isEndEqualsCurrent = false;
269+
270+
const start = getBoundaryPointByAbsoluteOffset(input, textRange[0]);
271+
const end = getBoundaryPointByAbsoluteOffset(input, textRange[1]);
269272

270273
/**
271-
* For right-to-left selection, we need to swap start and end offsets to compare with model range
274+
* If selection is outside of the input, it is different from the model range
272275
*/
273-
if (absoluteStartOffset > absoluteEndOffset) {
274-
[absoluteStartOffset, absoluteEndOffset] = [absoluteEndOffset, absoluteStartOffset];
275-
}
276+
if (input.contains(selection.anchorNode!) && input.contains(selection.focusNode!)) {
277+
let absoluteStartOffset = getAbsoluteRangeOffset(input, selection.anchorNode!, selection.anchorOffset);
278+
let absoluteEndOffset = getAbsoluteRangeOffset(input, selection.focusNode!, selection.focusOffset);
276279

277-
const start = getBoundaryPointByAbsoluteOffset(input, textRange[0]);
278-
const end = getBoundaryPointByAbsoluteOffset(input, textRange[1]);
280+
/**
281+
* For right-to-left selection, we need to swap start and end offsets to compare with model range
282+
*/
283+
if (absoluteStartOffset > absoluteEndOffset) {
284+
[absoluteStartOffset, absoluteEndOffset] = [absoluteEndOffset, absoluteStartOffset];
285+
}
279286

280-
const isStartEqualsCurrent = textRange[0] === absoluteStartOffset;
281-
const isEndEqualsCurrent = textRange[1] === absoluteEndOffset;
287+
isStartEqualsCurrent = textRange[0] === absoluteStartOffset;
288+
isEndEqualsCurrent = textRange[1] === absoluteEndOffset;
289+
}
282290

283291
/**
284292
* If selection is already the same, we don't need to update it to not interrupt browser's behaviour

0 commit comments

Comments
 (0)