项目文件夹

文件
wehub-resource-sync c48612c494
CI / E2E Tests (push) Has been cancelled
CI / Lint, Typecheck & Unit Tests (push) Has been cancelled
Docs Build / Build docs site (push) Has been cancelled
Publish @openmaic packages / Build, validate & publish (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:23 +08:00

33 行
1018 B
TypeScript

import { EditorState } from 'prosemirror-state';
import { type DirectEditorProps, EditorView } from 'prosemirror-view';
import { Schema, DOMParser } from 'prosemirror-model';
import { buildPlugins, type PluginOptions } from './plugins/index';
import { schemaNodes, schemaMarks } from './schema/index';
const schema = new Schema({
nodes: schemaNodes,
marks: schemaMarks,
});
export const createDocument = (content: string) => {
const htmlString = `<div>${content}</div>`;
const parser = new window.DOMParser();
const element = parser.parseFromString(htmlString, 'text/html').body.firstElementChild;
return DOMParser.fromSchema(schema).parse(element as Element);
};
export const initProsemirrorEditor = (
dom: Element,
content: string,
props: Omit<DirectEditorProps, 'state'>,
pluginOptions?: PluginOptions,
) => {
return new EditorView(dom, {
state: EditorState.create({
doc: createDocument(content),
plugins: buildPlugins(schema, pluginOptions),
}),
...props,
});
};