@blocksuite/virgo
Advanced tools
Comparing version 0.0.0-20230917124854-21f7463c-nightly to 0.0.0-20230919000022-43249faf-nightly
@@ -192,4 +192,2 @@ import { assertExists } from '@blocksuite/global/utils'; | ||
return; | ||
// Sometimes input event will directly come from some scripts (e.g. browser extension), | ||
// so we need to resync the vRange. | ||
const targetRanges = event.getTargetRanges(); | ||
@@ -196,0 +194,0 @@ if (targetRanges.length > 0) { |
@@ -655,7 +655,3 @@ import { expect, test } from '@playwright/test'; | ||
await page.waitForTimeout(100); | ||
await type(page, 'abc'); | ||
await press(page, 'Enter'); | ||
await type(page, 'def'); | ||
await press(page, 'Enter'); | ||
await type(page, 'ghi'); | ||
await type(page, 'abc\ndef\nghi'); | ||
expect(await editorA.innerText()).toBe('abc\ndef\nghi'); | ||
@@ -715,2 +711,3 @@ expect(await editorB.innerText()).toBe('abc\ndef\nghi'); | ||
await press(page, 'ArrowLeft'); | ||
await page.waitForTimeout(100); | ||
page.keyboard.down('Shift'); | ||
@@ -717,0 +714,0 @@ await press(page, 'ArrowLeft'); |
function handleInsertText(vRange, data, editor, attributes) { | ||
if (vRange.index >= 0 && data) { | ||
editor.insertText(vRange, data, attributes); | ||
editor.setVRange({ | ||
index: vRange.index + data.length, | ||
length: 0, | ||
}, false); | ||
} | ||
if (!data) | ||
return; | ||
editor.insertText(vRange, data, attributes); | ||
editor.setVRange({ | ||
index: vRange.index + data.length, | ||
length: 0, | ||
}, false); | ||
} | ||
function handleInsertParagraph(vRange, editor) { | ||
if (vRange.index >= 0) { | ||
editor.insertLineBreak(vRange); | ||
editor.setVRange({ | ||
index: vRange.index + 1, | ||
length: 0, | ||
}, false); | ||
} | ||
editor.insertLineBreak(vRange); | ||
editor.setVRange({ | ||
index: vRange.index + 1, | ||
length: 0, | ||
}, false); | ||
} | ||
function handleDeleteBackward(vRange, editor) { | ||
if (vRange.index >= 0) { | ||
if (vRange.length > 0) { | ||
editor.deleteText(vRange); | ||
editor.setVRange({ | ||
index: vRange.index, | ||
length: 0, | ||
}, false); | ||
return; | ||
} | ||
if (vRange.index > 0) { | ||
const originalString = editor.yText.toString().slice(0, vRange.index); | ||
const segments = [...new Intl.Segmenter().segment(originalString)]; | ||
const deletedLength = segments[segments.length - 1].segment.length; | ||
editor.deleteText({ | ||
index: vRange.index - deletedLength, | ||
length: deletedLength, | ||
}); | ||
editor.setVRange({ | ||
index: vRange.index - deletedLength, | ||
length: 0, | ||
}, false); | ||
} | ||
} | ||
function handleDelete(vRange, editor) { | ||
editor.deleteText(vRange); | ||
editor.setVRange({ | ||
index: vRange.index, | ||
length: 0, | ||
}, false); | ||
} | ||
function handleDeleteForward(editor, vRange) { | ||
if (vRange.index < editor.yText.length) { | ||
if (vRange.length > 0) { | ||
editor.deleteText(vRange); | ||
editor.setVRange({ | ||
index: vRange.index, | ||
length: 0, | ||
}, false); | ||
} | ||
else { | ||
const originalString = editor.yText.toString(); | ||
const segments = [...new Intl.Segmenter().segment(originalString)]; | ||
const slicedString = originalString.slice(0, vRange.index); | ||
const slicedSegments = [...new Intl.Segmenter().segment(slicedString)]; | ||
const deletedLength = segments[slicedSegments.length].segment.length; | ||
editor.deleteText({ | ||
index: vRange.index, | ||
length: deletedLength, | ||
}); | ||
editor.setVRange({ | ||
index: vRange.index, | ||
length: 0, | ||
}, false); | ||
} | ||
export function transformInput(inputType, data, attributes, vRange, editor) { | ||
if (!editor.isVRangeValid(vRange)) | ||
return; | ||
if (inputType === 'insertText') { | ||
handleInsertText(vRange, data, editor, attributes); | ||
} | ||
} | ||
function handleDeleteWordBackward(editor, vRange) { | ||
const matches = /\S+\s*$/.exec(editor.yText.toString().slice(0, vRange.index)); | ||
if (matches) { | ||
const deleteLength = matches[0].length; | ||
editor.deleteText({ | ||
index: vRange.index - deleteLength, | ||
length: deleteLength, | ||
}); | ||
editor.setVRange({ | ||
index: vRange.index - deleteLength, | ||
length: 0, | ||
}, false); | ||
else if (inputType === 'insertParagraph' || | ||
inputType === 'insertLineBreak') { | ||
handleInsertParagraph(vRange, editor); | ||
} | ||
} | ||
function handleDeleteWordForward(editor, vRange) { | ||
const matches = /^\s*\S+/.exec(editor.yText.toString().slice(vRange.index)); | ||
if (matches) { | ||
const deleteLength = matches[0].length; | ||
editor.deleteText({ | ||
index: vRange.index, | ||
length: deleteLength, | ||
}); | ||
editor.setVRange({ | ||
index: vRange.index, | ||
length: 0, | ||
}, false); | ||
else if (inputType.startsWith('delete')) { | ||
handleDelete(vRange, editor); | ||
} | ||
} | ||
function handleDeleteLine(editor, vRange) { | ||
if (vRange.length > 0) { | ||
editor.deleteText(vRange); | ||
editor.setVRange({ | ||
index: vRange.index, | ||
length: 0, | ||
}, false); | ||
else { | ||
return; | ||
} | ||
if (vRange.index > 0) { | ||
const str = editor.yText.toString(); | ||
const deleteLength = vRange.index - Math.max(0, str.slice(0, vRange.index).lastIndexOf('\n')); | ||
editor.deleteText({ | ||
index: vRange.index - deleteLength, | ||
length: deleteLength, | ||
}); | ||
editor.setVRange({ | ||
index: vRange.index - deleteLength, | ||
length: 0, | ||
}, false); | ||
} | ||
} | ||
export function transformInput(inputType, data, attributes, vRange, editor) { | ||
// You can find explanation of inputType here: | ||
// [Input Events Level 2](https://w3c.github.io/input-events/#interface-InputEvent-Attributes) | ||
switch (inputType) { | ||
case 'insertText': { | ||
handleInsertText(vRange, data, editor, attributes); | ||
return; | ||
} | ||
case 'insertParagraph': { | ||
handleInsertParagraph(vRange, editor); | ||
return; | ||
} | ||
// Chrome and Safari on Mac: Backspace or Ctrl + H | ||
case 'deleteContentBackward': | ||
case 'deleteByCut': { | ||
handleDeleteBackward(vRange, editor); | ||
return; | ||
} | ||
// Chrome on Mac: Fn + Backspace or Ctrl + D | ||
// Safari on Mac: Ctrl + K or Ctrl + D | ||
case 'deleteContentForward': { | ||
handleDeleteForward(editor, vRange); | ||
return; | ||
} | ||
// On Mac: Option + Backspace | ||
// On iOS: Hold the backspace for a while and the whole words will start to disappear | ||
case 'deleteWordBackward': { | ||
handleDeleteWordBackward(editor, vRange); | ||
return; | ||
} | ||
// onMac: Fn + Option + Backspace | ||
// onWindows: Control + Delete | ||
case 'deleteWordForward': { | ||
handleDeleteWordForward(editor, vRange); | ||
return; | ||
} | ||
// deleteHardLineBackward: Safari on Mac: Cmd + Backspace | ||
// deleteSoftLineBackward: Chrome on Mac: Cmd + Backspace | ||
case 'deleteHardLineBackward': | ||
case 'deleteSoftLineBackward': { | ||
handleDeleteLine(editor, vRange); | ||
return; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=transform-input.js.map |
{ | ||
"name": "@blocksuite/virgo", | ||
"version": "0.0.0-20230917124854-21f7463c-nightly", | ||
"version": "0.0.0-20230919000022-43249faf-nightly", | ||
"description": "A micro editor.", | ||
@@ -27,3 +27,3 @@ "type": "module", | ||
"zod": "^3.22.2", | ||
"@blocksuite/global": "0.0.0-20230917124854-21f7463c-nightly" | ||
"@blocksuite/global": "0.0.0-20230919000022-43249faf-nightly" | ||
}, | ||
@@ -30,0 +30,0 @@ "scripts": { |
@@ -270,4 +270,2 @@ import { assertExists } from '@blocksuite/global/utils'; | ||
// Sometimes input event will directly come from some scripts (e.g. browser extension), | ||
// so we need to resync the vRange. | ||
const targetRanges = event.getTargetRanges(); | ||
@@ -274,0 +272,0 @@ if (targetRanges.length > 0) { |
@@ -798,7 +798,3 @@ import { expect, test } from '@playwright/test'; | ||
await type(page, 'abc'); | ||
await press(page, 'Enter'); | ||
await type(page, 'def'); | ||
await press(page, 'Enter'); | ||
await type(page, 'ghi'); | ||
await type(page, 'abc\ndef\nghi'); | ||
@@ -875,2 +871,3 @@ expect(await editorA.innerText()).toBe('abc\ndef\nghi'); | ||
await press(page, 'ArrowLeft'); | ||
await page.waitForTimeout(100); | ||
page.keyboard.down('Shift'); | ||
@@ -877,0 +874,0 @@ await press(page, 'ArrowLeft'); |
@@ -11,165 +11,35 @@ import type { VRange } from '../types.js'; | ||
) { | ||
if (vRange.index >= 0 && data) { | ||
editor.insertText(vRange, data, attributes); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index + data.length, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
if (!data) return; | ||
editor.insertText(vRange, data, attributes); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index + data.length, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
function handleInsertParagraph(vRange: VRange, editor: VEditor) { | ||
if (vRange.index >= 0) { | ||
editor.insertLineBreak(vRange); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index + 1, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
} | ||
function handleDeleteBackward(vRange: VRange, editor: VEditor) { | ||
if (vRange.index >= 0) { | ||
if (vRange.length > 0) { | ||
editor.deleteText(vRange); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
return; | ||
} | ||
if (vRange.index > 0) { | ||
const originalString = editor.yText.toString().slice(0, vRange.index); | ||
const segments = [...new Intl.Segmenter().segment(originalString)]; | ||
const deletedLength = segments[segments.length - 1].segment.length; | ||
editor.deleteText({ | ||
index: vRange.index - deletedLength, | ||
length: deletedLength, | ||
}); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index - deletedLength, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
} | ||
} | ||
function handleDeleteForward(editor: VEditor, vRange: VRange) { | ||
if (vRange.index < editor.yText.length) { | ||
if (vRange.length > 0) { | ||
editor.deleteText(vRange); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} else { | ||
const originalString = editor.yText.toString(); | ||
const segments = [...new Intl.Segmenter().segment(originalString)]; | ||
const slicedString = originalString.slice(0, vRange.index); | ||
const slicedSegments = [...new Intl.Segmenter().segment(slicedString)]; | ||
const deletedLength = segments[slicedSegments.length].segment.length; | ||
editor.deleteText({ | ||
index: vRange.index, | ||
length: deletedLength, | ||
}); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
} | ||
} | ||
function handleDeleteWordBackward(editor: VEditor, vRange: VRange) { | ||
const matches = /\S+\s*$/.exec( | ||
editor.yText.toString().slice(0, vRange.index) | ||
editor.insertLineBreak(vRange); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index + 1, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
if (matches) { | ||
const deleteLength = matches[0].length; | ||
editor.deleteText({ | ||
index: vRange.index - deleteLength, | ||
length: deleteLength, | ||
}); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index - deleteLength, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
} | ||
function handleDeleteWordForward(editor: VEditor, vRange: VRange) { | ||
const matches = /^\s*\S+/.exec(editor.yText.toString().slice(vRange.index)); | ||
if (matches) { | ||
const deleteLength = matches[0].length; | ||
editor.deleteText({ | ||
function handleDelete(vRange: VRange, editor: VEditor) { | ||
editor.deleteText(vRange); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index, | ||
length: deleteLength, | ||
}); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
function handleDeleteLine(editor: VEditor, vRange: VRange) { | ||
if (vRange.length > 0) { | ||
editor.deleteText(vRange); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
return; | ||
} | ||
if (vRange.index > 0) { | ||
const str = editor.yText.toString(); | ||
const deleteLength = | ||
vRange.index - Math.max(0, str.slice(0, vRange.index).lastIndexOf('\n')); | ||
editor.deleteText({ | ||
index: vRange.index - deleteLength, | ||
length: deleteLength, | ||
}); | ||
editor.setVRange( | ||
{ | ||
index: vRange.index - deleteLength, | ||
length: 0, | ||
}, | ||
false | ||
); | ||
} | ||
} | ||
export function transformInput<TextAttributes extends BaseTextAttributes>( | ||
@@ -182,51 +52,16 @@ inputType: string, | ||
) { | ||
// You can find explanation of inputType here: | ||
// [Input Events Level 2](https://w3c.github.io/input-events/#interface-InputEvent-Attributes) | ||
switch (inputType) { | ||
case 'insertText': { | ||
handleInsertText(vRange, data, editor, attributes); | ||
return; | ||
} | ||
if (!editor.isVRangeValid(vRange)) return; | ||
case 'insertParagraph': { | ||
handleInsertParagraph(vRange, editor); | ||
return; | ||
} | ||
// Chrome and Safari on Mac: Backspace or Ctrl + H | ||
case 'deleteContentBackward': | ||
case 'deleteByCut': { | ||
handleDeleteBackward(vRange, editor); | ||
return; | ||
} | ||
// Chrome on Mac: Fn + Backspace or Ctrl + D | ||
// Safari on Mac: Ctrl + K or Ctrl + D | ||
case 'deleteContentForward': { | ||
handleDeleteForward(editor, vRange); | ||
return; | ||
} | ||
// On Mac: Option + Backspace | ||
// On iOS: Hold the backspace for a while and the whole words will start to disappear | ||
case 'deleteWordBackward': { | ||
handleDeleteWordBackward(editor, vRange); | ||
return; | ||
} | ||
// onMac: Fn + Option + Backspace | ||
// onWindows: Control + Delete | ||
case 'deleteWordForward': { | ||
handleDeleteWordForward(editor, vRange); | ||
return; | ||
} | ||
// deleteHardLineBackward: Safari on Mac: Cmd + Backspace | ||
// deleteSoftLineBackward: Chrome on Mac: Cmd + Backspace | ||
case 'deleteHardLineBackward': | ||
case 'deleteSoftLineBackward': { | ||
handleDeleteLine(editor, vRange); | ||
return; | ||
} | ||
if (inputType === 'insertText') { | ||
handleInsertText(vRange, data, editor, attributes); | ||
} else if ( | ||
inputType === 'insertParagraph' || | ||
inputType === 'insertLineBreak' | ||
) { | ||
handleInsertParagraph(vRange, editor); | ||
} else if (inputType.startsWith('delete')) { | ||
handleDelete(vRange, editor); | ||
} else { | ||
return; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
507552
8326
+ Added@blocksuite/global@0.0.0-20230919000022-43249faf-nightly(transitive)
- Removed@blocksuite/global@0.0.0-20230917124854-21f7463c-nightly(transitive)
Updated@blocksuite/global@0.0.0-20230919000022-43249faf-nightly