webapp/src/components/Editor/store/state.ts

28 lines
908 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 type NodeViewFn = (
node: Node,
view: EditorView,
getPos: () => number,
decorations: Decoration[]
) => NodeView
export interface ProseMirrorExtension {
schema?: (prev: SchemaSpec) => SchemaSpec
plugins?: (prev: Plugin[], schema: Schema) => Plugin[]
nodeViews?: { [key: string]: NodeViewFn }
}
export type ProseMirrorState = EditorState | unknown
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)