Skip to content

Commit 14c1cd3

Browse files
committed
caret adapter now stores blocks instead of inputs
1 parent 9b3d2ab commit 14c1cd3

File tree

4 files changed

+181
-100
lines changed

4 files changed

+181
-100
lines changed

packages/core/src/components/BlockManager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ export class BlocksManager {
168168
toolName
169169
);
170170

171+
/**
172+
* We store blocks managers in caret adapter to give it access to blocks` inputs
173+
* without additional storing inputs in the caret adapter
174+
* Thus, it won't care about block index change (block removed, block added, block moved)
175+
*/
176+
this.#caretAdapter.attachBlock(blockToolAdapter, index);
177+
171178
const tool = this.#toolsManager.blockTools.get(data.name);
172179

173180
if (tool === undefined) {

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
IndexBuilder,
1111
type ModelEvents,
1212
TextAddedEvent,
13-
TextRemovedEvent
13+
TextRemovedEvent,
14+
Index
1415
} from '@editorjs/model';
1516
import type {
1617
EventBus,
@@ -138,8 +139,6 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
138139

139140
builder.addBlockIndex(this.#blockIndex).addDataKey(key);
140141

141-
this.#caretAdapter.attachInput(input, builder.build());
142-
143142
const value = this.#model.getText(this.#blockIndex, key);
144143
const fragments = this.#model.getFragments(this.#blockIndex, key);
145144

@@ -167,12 +166,6 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
167166
* @todo Let BlockTool handle DOM update
168167
*/
169168
input.remove();
170-
this.#caretAdapter.detachInput(
171-
new IndexBuilder()
172-
.addBlockIndex(this.#blockIndex)
173-
.addDataKey(key)
174-
.build()
175-
);
176169

177170
this.#attachedInputs.delete(key);
178171

@@ -481,7 +474,21 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
481474

482475
const isInputNative = isNativeInput(input);
483476
let start: number;
484-
let end: number;
477+
478+
/**
479+
* @todo support input merging
480+
*/
481+
482+
/**
483+
* In all cases we need to handle delete selected text if range is not collapsed
484+
*/
485+
if (!range.collapsed) {
486+
if (isInputNative) {
487+
this.#handleDeleteInNativeInput(payload, input as HTMLInputElement | HTMLTextAreaElement, key, range);
488+
} else {
489+
this.#handleDeleteInContentEditable(input, key, range);
490+
}
491+
}
485492

486493
switch (inputType) {
487494
case InputType.InsertReplacementText:
@@ -523,25 +530,13 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
523530
case InputType.DeleteEntireSoftLine:
524531
case InputType.DeleteWordBackward:
525532
case InputType.DeleteWordForward: {
526-
if (isInputNative === true) {
527-
this.#handleDeleteInNativeInput(payload, input as HTMLInputElement | HTMLTextAreaElement, key, range);
528-
} else {
529-
this.#handleDeleteInContentEditable(input, key, range);
530-
}
533+
/**
534+
* We already handle delete above
535+
*/
531536
break;
532537
}
533538

534539
case InputType.InsertParagraph:
535-
console.log('insert paragraph', input);
536-
537-
if (isInputNative) {
538-
// start = (input as HTMLInputElement | HTMLTextAreaElement).selectionStart as number;
539-
this.#handleDeleteInNativeInput(payload, input as HTMLInputElement | HTMLTextAreaElement, key, range);
540-
} else {
541-
this.#handleDeleteInContentEditable(input, key, range, true);
542-
// start = getAbsoluteRangeOffset(input, range.startContainer, range.startOffset);
543-
}
544-
545540
/**
546541
*
547542
*/
@@ -767,4 +762,29 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
767762
this.#handleModelUpdateForContentEditableElement(event, input, dataKey!);
768763
}
769764
};
765+
766+
/**
767+
* Public getter for block index.
768+
* Can be used to find a particular block, for example, in caret adapter
769+
*/
770+
public getBlockIndex(): Index {
771+
return new IndexBuilder()
772+
.addBlockIndex(this.#blockIndex)
773+
.build();
774+
}
775+
776+
/**
777+
* Public getter for all attached inputs.
778+
* Can be used to loop through all inputs to find a particular input(s)
779+
*/
780+
public getAttachedInputs(): Map<DataKey, HTMLElement> {
781+
return this.#attachedInputs;
782+
}
783+
784+
/**
785+
* Allows access to a particular input by key
786+
*/
787+
public getInput(key: DataKey): HTMLElement | undefined {
788+
return this.#attachedInputs.get(key);
789+
}
770790
}

0 commit comments

Comments
 (0)