@blocksuite/virgo
Advanced tools
Comparing version 0.4.1-20230223195957-963a0ba to 0.4.1-20230224181613-0101128
{ | ||
"name": "@blocksuite/virgo", | ||
"version": "0.4.1-20230223195957-963a0ba", | ||
"version": "0.4.1-20230224181613-0101128", | ||
"description": "A micro editor.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.ts", |
134
src/virgo.ts
@@ -86,3 +86,3 @@ import { assertExists, Signal } from '@blocksuite/global/utils'; | ||
'beforeinput', | ||
this._onBefoeInput.bind(this), | ||
this._onBeforeInput.bind(this), | ||
{ | ||
@@ -173,3 +173,3 @@ signal: this._rootElementAbort.signal, | ||
if ( | ||
index + delta.insert.length > vRange.index && | ||
index + delta.insert.length >= vRange.index && | ||
index < vRange.index + vRange.length | ||
@@ -332,3 +332,8 @@ ) { | ||
let index = 0; | ||
for (let i = 0; i < lineElements.length; i++) { | ||
if (anchorText && focusText) { | ||
break; | ||
} | ||
const textElements = Array.from( | ||
@@ -339,2 +344,6 @@ lineElements[i].querySelectorAll('[data-virgo-text="true"]') | ||
for (let j = 0; j < textElements.length; j++) { | ||
if (anchorText && focusText) { | ||
break; | ||
} | ||
const textNode = getTextNodeFromElement(textElements[j]); | ||
@@ -359,3 +368,3 @@ if (!textNode) { | ||
// the one becasue of the line break | ||
// the one because of the line break | ||
index += 1; | ||
@@ -391,3 +400,3 @@ } | ||
* 2.2 | ||
* the vRange of first Editor is null, the second is {index: 0, length: 5}, | ||
* the vRange of first Editor is null, the second is {index: 5, length: 1}, | ||
* the third is {index: 0, length: 2} | ||
@@ -409,57 +418,11 @@ * 3. anchor and focus are in another Editor | ||
let anchorText: Text | null = null; | ||
let anchorTextOffset = anchorOffset; | ||
let focusText: Text | null = null; | ||
let focusTextOffset = focusOffset; | ||
const [anchorText, anchorTextOffset] = getTextAndOffset( | ||
anchorNode, | ||
anchorOffset | ||
); | ||
const [focusText, focusTextOffset] = getTextAndOffset( | ||
focusNode, | ||
focusOffset | ||
); | ||
if (anchorNode instanceof Text && isVText(anchorNode)) { | ||
anchorText = anchorNode; | ||
anchorTextOffset = anchorOffset; | ||
} else if ( | ||
anchorNode instanceof HTMLElement && | ||
anchorNode.dataset.virgoElement === 'true' | ||
) { | ||
const textNode = getTextNodeFromElement(anchorNode); | ||
if (textNode) { | ||
anchorText = textNode; | ||
anchorTextOffset = anchorOffset; | ||
} | ||
} else if ( | ||
anchorNode instanceof HTMLElement && | ||
anchorNode.parentElement instanceof VirgoLine | ||
) { | ||
const firstTextElement = anchorNode.querySelector('v-text'); | ||
if (firstTextElement) { | ||
const textNode = getTextNodeFromElement(firstTextElement); | ||
if (textNode) { | ||
anchorText = textNode; | ||
anchorTextOffset = 0; | ||
} | ||
} | ||
} | ||
if (focusNode instanceof Text && isVText(focusNode)) { | ||
focusText = focusNode; | ||
focusTextOffset = focusOffset; | ||
} else if ( | ||
focusNode instanceof HTMLElement && | ||
focusNode.dataset.virgoElement === 'true' | ||
) { | ||
const textNode = getTextNodeFromElement(focusNode); | ||
if (textNode) { | ||
focusText = textNode; | ||
focusTextOffset = focusOffset; | ||
} | ||
} else if ( | ||
focusNode instanceof HTMLElement && | ||
focusNode.parentElement instanceof VirgoLine | ||
) { | ||
const firstTextElement = focusNode.querySelector('v-text'); | ||
if (firstTextElement) { | ||
const textNode = getTextNodeFromElement(firstTextElement); | ||
if (textNode) { | ||
anchorText = textNode; | ||
anchorTextOffset = 0; | ||
} | ||
} | ||
} | ||
// case 1 | ||
@@ -553,3 +516,3 @@ if (anchorText && focusText) { | ||
private _onBefoeInput(event: InputEvent): void { | ||
private _onBeforeInput(event: InputEvent): void { | ||
event.preventDefault(); | ||
@@ -604,6 +567,6 @@ | ||
const tmpString = this.yText.toString().slice(0, this._vRange.index); | ||
const deletedChracater = [...tmpString].slice(-1).join(''); | ||
const deletedCharacter = [...tmpString].slice(-1).join(''); | ||
this.deleteText({ | ||
index: this._vRange.index - deletedChracater.length, | ||
length: deletedChracater.length, | ||
index: this._vRange.index - deletedCharacter.length, | ||
length: deletedCharacter.length, | ||
}); | ||
@@ -613,3 +576,3 @@ | ||
{ | ||
index: this._vRange.index - deletedChracater.length, | ||
index: this._vRange.index - deletedCharacter.length, | ||
length: 0, | ||
@@ -831,6 +794,47 @@ }, | ||
function isVText(text: Text) { | ||
return text.parentElement?.dataset.virgoText === 'true' ?? false; | ||
function isVText(text: unknown): text is Text { | ||
return ( | ||
text instanceof Text && | ||
(text.parentElement?.dataset.virgoText === 'true' ?? false) | ||
); | ||
} | ||
function isVElement(element: unknown): element is HTMLElement { | ||
return ( | ||
element instanceof HTMLElement && element.dataset.virgoElement === 'true' | ||
); | ||
} | ||
function isVLine(element: unknown): element is HTMLElement { | ||
return ( | ||
element instanceof HTMLElement && element.parentElement instanceof VirgoLine | ||
); | ||
} | ||
function getTextAndOffset(node: unknown, offset: number) { | ||
let text: Text | null = null; | ||
let textOffset = offset; | ||
if (isVText(node)) { | ||
text = node; | ||
textOffset = offset; | ||
} else if (isVElement(node)) { | ||
const textNode = getTextNodeFromElement(node); | ||
if (textNode) { | ||
text = textNode; | ||
textOffset = offset; | ||
} | ||
} else if (isVLine(node)) { | ||
const firstTextElement = node.querySelector('v-text'); | ||
if (firstTextElement) { | ||
const textNode = getTextNodeFromElement(firstTextElement); | ||
if (textNode) { | ||
text = textNode; | ||
textOffset = 0; | ||
} | ||
} | ||
} | ||
return [text, textOffset] as const; | ||
} | ||
function findDocumentOrShadowRoot(editor: VEditor): Document | ShadowRoot { | ||
@@ -837,0 +841,0 @@ const el = editor.getRootElement(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52853
1756