@lvce-editor/editor-worker 19.68.0 → 19.68.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/editorWorkerMain.js +101 -2
- package/package.json +1 -1
package/dist/editorWorkerMain.js
CHANGED
|
@@ -11707,9 +11707,108 @@ const pasteText = (editor, text) => {
|
|
|
11707
11707
|
return scheduleDocumentAndCursorsSelections(editor, changes);
|
|
11708
11708
|
};
|
|
11709
11709
|
|
|
11710
|
-
const
|
|
11711
|
-
const
|
|
11710
|
+
const getImageExtension = type => {
|
|
11711
|
+
const subtype = type.slice('image/'.length).toLowerCase().split('+', 1)[0];
|
|
11712
|
+
if (subtype === 'jpeg') {
|
|
11713
|
+
return 'jpg';
|
|
11714
|
+
}
|
|
11715
|
+
return /^[a-z0-9]+$/.test(subtype) ? subtype : 'png';
|
|
11716
|
+
};
|
|
11717
|
+
const getImageUri = (documentUri, fileName) => {
|
|
11718
|
+
if (URL.canParse(documentUri)) {
|
|
11719
|
+
const url = new URL(documentUri);
|
|
11720
|
+
url.pathname = `${url.pathname.slice(0, url.pathname.lastIndexOf('/') + 1)}${fileName}`;
|
|
11721
|
+
url.search = '';
|
|
11722
|
+
url.hash = '';
|
|
11723
|
+
return url.href;
|
|
11724
|
+
}
|
|
11725
|
+
const slashIndex = documentUri.lastIndexOf('/');
|
|
11726
|
+
if (slashIndex === -1) {
|
|
11727
|
+
throw new Error('Save the Markdown document before pasting an image.');
|
|
11728
|
+
}
|
|
11729
|
+
return `${documentUri.slice(0, slashIndex + 1)}${fileName}`;
|
|
11730
|
+
};
|
|
11731
|
+
const toBinaryString = async blob => {
|
|
11732
|
+
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
11733
|
+
const chunks = [];
|
|
11734
|
+
for (let index = 0; index < bytes.length; index += 8192) {
|
|
11735
|
+
chunks.push(String.fromCodePoint(...bytes.subarray(index, index + 8192)));
|
|
11736
|
+
}
|
|
11737
|
+
return chunks.join('');
|
|
11738
|
+
};
|
|
11739
|
+
const isFileExistsError = error => {
|
|
11740
|
+
if (!(error instanceof Error)) {
|
|
11741
|
+
return false;
|
|
11742
|
+
}
|
|
11743
|
+
const cause = 'cause' in error ? error.cause : undefined;
|
|
11744
|
+
return /already exists|EEXIST/i.test(error.message) || cause !== undefined && isFileExistsError(cause);
|
|
11745
|
+
};
|
|
11746
|
+
const writeClipboardImage = async (editor, blob) => {
|
|
11747
|
+
const extension = getImageExtension(blob.type);
|
|
11748
|
+
for (let index = 0; index < 1000; index++) {
|
|
11749
|
+
const fileName = index === 0 ? `image.${extension}` : `image-${index}.${extension}`;
|
|
11750
|
+
const uri = getImageUri(editor.uri, fileName);
|
|
11751
|
+
if (await invoke$9(editor.applicationId, 'FileSystem.exists', uri)) {
|
|
11752
|
+
continue;
|
|
11753
|
+
}
|
|
11754
|
+
try {
|
|
11755
|
+
await invoke$9(editor.applicationId, 'FileSystem.createFile', uri);
|
|
11756
|
+
} catch (error) {
|
|
11757
|
+
if (isFileExistsError(error)) {
|
|
11758
|
+
continue;
|
|
11759
|
+
}
|
|
11760
|
+
throw error;
|
|
11761
|
+
}
|
|
11762
|
+
try {
|
|
11763
|
+
await invoke$9(editor.applicationId, 'FileSystem.writeFile', uri, await toBinaryString(blob), 'binary');
|
|
11764
|
+
return {
|
|
11765
|
+
fileName,
|
|
11766
|
+
uri
|
|
11767
|
+
};
|
|
11768
|
+
} catch (error) {
|
|
11769
|
+
try {
|
|
11770
|
+
await invoke$9(editor.applicationId, 'FileSystem.remove', uri);
|
|
11771
|
+
} catch {}
|
|
11772
|
+
throw error;
|
|
11773
|
+
}
|
|
11774
|
+
}
|
|
11775
|
+
throw new Error('Could not find an unused filename for the pasted image.');
|
|
11776
|
+
};
|
|
11777
|
+
const paste = async (editor, pastedText) => {
|
|
11778
|
+
let imageReadError;
|
|
11779
|
+
if (editor.languageId === 'markdown') {
|
|
11780
|
+
let image;
|
|
11781
|
+
try {
|
|
11782
|
+
image = await invoke$d('ClipBoard.readImage');
|
|
11783
|
+
} catch (error) {
|
|
11784
|
+
imageReadError = error;
|
|
11785
|
+
}
|
|
11786
|
+
if (image instanceof Blob) {
|
|
11787
|
+
let savedImage;
|
|
11788
|
+
try {
|
|
11789
|
+
savedImage = await writeClipboardImage(editor, image);
|
|
11790
|
+
} catch (error) {
|
|
11791
|
+
throw new Error('Failed to save the clipboard image beside the Markdown document.', {
|
|
11792
|
+
cause: error
|
|
11793
|
+
});
|
|
11794
|
+
}
|
|
11795
|
+
const currentEditor = get$8(editor.uid)?.newState;
|
|
11796
|
+
if (!currentEditor || currentEditor.uri !== editor.uri || currentEditor.languageId !== 'markdown') {
|
|
11797
|
+
try {
|
|
11798
|
+
await invoke$9(editor.applicationId, 'FileSystem.remove', savedImage.uri);
|
|
11799
|
+
} catch {}
|
|
11800
|
+
throw new Error('The Markdown document changed while saving the clipboard image. Paste the image again.');
|
|
11801
|
+
}
|
|
11802
|
+
return pasteText(currentEditor, ``);
|
|
11803
|
+
}
|
|
11804
|
+
}
|
|
11805
|
+
const text = pastedText ?? (await readClipBoardText());
|
|
11712
11806
|
string(text);
|
|
11807
|
+
if (imageReadError && text.length === 0) {
|
|
11808
|
+
throw new Error('Unable to read an image from the clipboard. Check clipboard permissions and try again.', {
|
|
11809
|
+
cause: imageReadError
|
|
11810
|
+
});
|
|
11811
|
+
}
|
|
11713
11812
|
return pasteText(editor, text);
|
|
11714
11813
|
};
|
|
11715
11814
|
|