This commit is contained in:
weilin1982 2024-05-15 15:40:47 +08:00 committed by GitHub
parent 3665aeea36
commit ace2ab3ba8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 1 deletions

View File

@ -17,8 +17,26 @@ import { safeJSONParse } from 'helpers/json';
import { toggleMark } from 'prosemirror-commands'; import { toggleMark } from 'prosemirror-commands';
import { DOMParser as PMDOMParser, Fragment, Node, Schema } from 'prosemirror-model'; import { DOMParser as PMDOMParser, Fragment, Node, Schema } from 'prosemirror-model';
import { EditorState, Plugin, PluginKey, TextSelection } from 'prosemirror-state'; import { EditorState, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
import { uploadFile } from 'services/file';
const htmlToProsemirror = (editor: CoreEditor, html, isPasteMarkdown = false) => { const reuploadImageAndUpdateSrc = async (img: HTMLImageElement) => {
try {
const resp = await fetch(img.src);
if (!resp.ok) {
return false;
}
const blob = await resp.blob();
const url = await uploadFile?.(blob);
img.src = url;
return true;
} catch (error) {
return false;
}
};
const htmlToProsemirror = async (editor: CoreEditor, html, isPasteMarkdown = false) => {
const firstNode = editor.view.state.doc.content.firstChild; const firstNode = editor.view.state.doc.content.firstChild;
const shouldInsertTitleText = !!(firstNode?.textContent?.length <= 0 ?? true); const shouldInsertTitleText = !!(firstNode?.textContent?.length <= 0 ?? true);
@ -27,6 +45,13 @@ const htmlToProsemirror = (editor: CoreEditor, html, isPasteMarkdown = false) =>
const parser = new window.DOMParser(); const parser = new window.DOMParser();
const { body } = parser.parseFromString(fixHTML(html), 'text/html'); const { body } = parser.parseFromString(fixHTML(html), 'text/html');
try {
const imgs = body.querySelectorAll('img');
await Prosemise.all([...imgs].map(reuploadImageAndUpdateSrc)
} catch (e) {
//
}
const schema = getSchema( const schema = getSchema(
[].concat( [].concat(
Document, Document,