webapp/src/components/Editor/prosemirror/helpers.ts

30 lines
964 B
TypeScript
Raw Normal View History

2022-09-09 11:53:35 +00:00
import { Plugin, EditorState } from 'prosemirror-state'
import type { Node, Schema, SchemaSpec } from 'prosemirror-model'
import type { Decoration, EditorView, NodeView } from 'prosemirror-view'
export interface ProseMirrorExtension {
schema?: (prev: SchemaSpec) => SchemaSpec
plugins?: (prev: Plugin[], schema: Schema) => Plugin[]
nodeViews?: { [key: string]: NodeViewFn }
}
export type ProseMirrorState = EditorState | unknown
2022-10-09 00:00:13 +00:00
export type NodeViewFn = (
node: Node,
view: EditorView,
getPos: () => number,
decorations: Decoration[]
) => NodeView
2022-10-08 16:40:58 +00:00
export const isInitialized = (state: EditorState) => state !== undefined && state instanceof EditorState
2022-09-09 11:53:35 +00:00
2022-10-08 16:40:58 +00:00
export const isEmpty = (state: EditorState) =>
2022-09-09 11:53:35 +00:00
!isInitialized(state) ||
(state.doc.childCount === 1 &&
!state.doc.firstChild.type.spec.code &&
state.doc.firstChild.isTextblock &&
state.doc.firstChild.content.size === 0)
2022-10-09 00:00:13 +00:00
export const isText = (x) => x && x.doc && x.selection