2025-07-29 12:25:43 -07:00
|
|
|
import { DOMParser as ProseMirrorDomParser } from 'prosemirror-model';
|
|
|
|
|
import { EditorView } from 'prosemirror-view';
|
|
|
|
|
import schema from '../schema';
|
|
|
|
|
import { EditorState, Plugin } from 'prosemirror-state';
|
|
|
|
|
|
2025-09-30 09:34:03 -07:00
|
|
|
export type PluginList = Plugin[]|(Plugin|Plugin[])[];
|
2025-07-29 12:25:43 -07:00
|
|
|
|
|
|
|
|
interface Options {
|
|
|
|
|
parent?: HTMLElement;
|
|
|
|
|
html: string;
|
|
|
|
|
plugins?: PluginList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createTestEditor = ({ html, parent = null, plugins = [] }: Options) => {
|
2025-09-30 09:34:03 -07:00
|
|
|
if (parent === null) {
|
|
|
|
|
// Create a test parent -- some code adds tooltips, etc to view.dom.parent.
|
|
|
|
|
parent = document.createElement('div');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 12:25:43 -07:00
|
|
|
const htmlDocument = new DOMParser().parseFromString(html, 'text/html');
|
|
|
|
|
const proseMirrorDocument = ProseMirrorDomParser.fromSchema(schema).parse(htmlDocument);
|
|
|
|
|
return new EditorView(parent, {
|
|
|
|
|
state: EditorState.create({
|
|
|
|
|
doc: proseMirrorDocument,
|
|
|
|
|
plugins: plugins.flat(),
|
|
|
|
|
schema,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default createTestEditor;
|