@prosekit/core
Advanced tools
Comparing version 0.0.0-next-20240622120010 to 0.0.0-next-20240626133927
@@ -6,2 +6,3 @@ import { AllSelection } from '@prosekit/pm/state'; | ||
import type { ContentMatch } from '@prosekit/pm/model'; | ||
import { config as default_alias_1 } from '@prosekit/dev/config-vitest'; | ||
import type { DirectEditorProps } from '@prosekit/pm/view'; | ||
@@ -75,5 +76,12 @@ import type { DOMEventMap } from '@prosekit/pm/view'; | ||
export declare const applyMarkForTest: ApplyMarkFunction; | ||
/** | ||
* @internal | ||
*/ | ||
export declare type ApplyMarkFunction = (mark: Mark, children: ProseMirrorNode[]) => ProseMirrorNode[]; | ||
/** | ||
* @internal | ||
*/ | ||
export declare function arrayRemove<T>(array: T[], item: T): void; | ||
@@ -153,2 +161,4 @@ | ||
export declare function buildNode(type: NodeType, args: [Attrs | NodeChild | null | undefined, ...NodeChild[]], createNode: CreateNodeFunction): ProseMirrorNode; | ||
export declare function cache<T>(fn: () => T): () => T; | ||
@@ -214,6 +224,26 @@ | ||
export declare function createMarkBuilder(getState: () => EditorState | null | undefined, type: MarkType): MarkBuilder; | ||
/** | ||
* @internal | ||
*/ | ||
export declare function createMarkBuilders(schema: Schema, getState: () => EditorState | null | undefined, applyMark?: ApplyMarkFunction): Record<string, MarkBuilder>; | ||
export declare function createNodeBuilder(getState: () => EditorState | null | undefined, type: NodeType): NodeBuilder; | ||
/** | ||
* @internal | ||
*/ | ||
export declare function createNodeBuilders(schema: Schema, getState: () => EditorState | null | undefined, createNode?: CreateNodeFunction): Record<string, NodeBuilder>; | ||
export declare const createNodeForTest: CreateNodeFunction; | ||
/** | ||
* @internal | ||
*/ | ||
export declare type CreateNodeFunction = (type: NodeType, attrs: Attrs | null, children: ProseMirrorNode[]) => ProseMirrorNode; | ||
/** | ||
* @public | ||
*/ | ||
declare function createTestEditor<E extends Extension>(options: EditorOptions<E>): TestEditor<E>; | ||
export { createTestEditor } | ||
export { createTestEditor as createTestEditor_alias_1 } | ||
export declare function deepEquals<T>(a: T, b: T): boolean; | ||
@@ -223,7 +253,3 @@ | ||
export declare const default_alias_1: { | ||
test: { | ||
environment: "jsdom"; | ||
}; | ||
}; | ||
export { default_alias_1 } | ||
@@ -331,3 +357,3 @@ /** | ||
priority?: Priority; | ||
}): Extension<any>; | ||
}): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineBaseKeymap } | ||
@@ -341,3 +367,3 @@ export { defineBaseKeymap as defineBaseKeymap_alias_1 } | ||
*/ | ||
declare function defineClickHandler(handler: ClickHandler): Extension<any>; | ||
declare function defineClickHandler(handler: ClickHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineClickHandler } | ||
@@ -351,3 +377,3 @@ export { defineClickHandler as defineClickHandler_alias_1 } | ||
*/ | ||
declare function defineClickOnHandler(handler: ClickOnHandler): Extension<any>; | ||
declare function defineClickOnHandler(handler: ClickOnHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineClickOnHandler } | ||
@@ -386,3 +412,3 @@ export { defineClickOnHandler as defineClickOnHandler_alias_1 } | ||
*/ | ||
declare function defineDocChangeHandler(handler: DocChangeHandler): Extension<any>; | ||
declare function defineDocChangeHandler(handler: DocChangeHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineDocChangeHandler } | ||
@@ -396,3 +422,3 @@ export { defineDocChangeHandler as defineDocChangeHandler_alias_1 } | ||
*/ | ||
declare function defineDOMEventHandler<Event extends keyof DOMEventMap = string>(event: Event, handler: DOMEventHandler<Event>): Extension<any>; | ||
declare function defineDOMEventHandler<Event extends keyof DOMEventMap = string>(event: Event, handler: DOMEventHandler<Event>): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineDOMEventHandler } | ||
@@ -406,3 +432,3 @@ export { defineDOMEventHandler as defineDOMEventHandler_alias_1 } | ||
*/ | ||
declare function defineDoubleClickHandler(handler: DoubleClickHandler): Extension<any>; | ||
declare function defineDoubleClickHandler(handler: DoubleClickHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineDoubleClickHandler } | ||
@@ -416,3 +442,3 @@ export { defineDoubleClickHandler as defineDoubleClickHandler_alias_1 } | ||
*/ | ||
declare function defineDoubleClickOnHandler(handler: DoubleClickOnHandler): Extension<any>; | ||
declare function defineDoubleClickOnHandler(handler: DoubleClickOnHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineDoubleClickOnHandler } | ||
@@ -426,3 +452,3 @@ export { defineDoubleClickOnHandler as defineDoubleClickOnHandler_alias_1 } | ||
*/ | ||
declare function defineDropHandler(handler: DropHandler): Extension<any>; | ||
declare function defineDropHandler(handler: DropHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineDropHandler } | ||
@@ -470,3 +496,3 @@ export { defineDropHandler as defineDropHandler_alias_1 } | ||
*/ | ||
declare function defineFocusChangeHandler(handler: FocusChangeHandler): Extension<any>; | ||
declare function defineFocusChangeHandler(handler: FocusChangeHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineFocusChangeHandler } | ||
@@ -479,6 +505,7 @@ export { defineFocusChangeHandler as defineFocusChangeHandler_alias_1 } | ||
declare function defineHistory(): Extension< { | ||
Nodes: any; | ||
Marks: any; | ||
Nodes: never; | ||
Marks: never; | ||
Commands: { | ||
[x: string]: any; | ||
undo: []; | ||
redo: []; | ||
}; | ||
@@ -494,3 +521,3 @@ }>; | ||
*/ | ||
declare function defineKeyDownHandler(handler: KeyDownHandler): Extension<any>; | ||
declare function defineKeyDownHandler(handler: KeyDownHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineKeyDownHandler } | ||
@@ -511,3 +538,3 @@ export { defineKeyDownHandler as defineKeyDownHandler_alias_1 } | ||
*/ | ||
declare function defineKeyPressHandler(handler: KeyPressHandler): Extension<any>; | ||
declare function defineKeyPressHandler(handler: KeyPressHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineKeyPressHandler } | ||
@@ -539,3 +566,3 @@ export { defineKeyPressHandler as defineKeyPressHandler_alias_1 } | ||
*/ | ||
declare function defineMountHandler(handler: MountHandler): Extension<any>; | ||
declare function defineMountHandler(handler: MountHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineMountHandler } | ||
@@ -606,3 +633,3 @@ export { defineMountHandler as defineMountHandler_alias_1 } | ||
*/ | ||
declare function definePasteHandler(handler: PasteHandler): Extension<any>; | ||
declare function definePasteHandler(handler: PasteHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { definePasteHandler } | ||
@@ -630,3 +657,3 @@ export { definePasteHandler as definePasteHandler_alias_1 } | ||
*/ | ||
declare function defineScrollToSelectionHandler(handler: ScrollToSelectionHandler): Extension<any>; | ||
declare function defineScrollToSelectionHandler(handler: ScrollToSelectionHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineScrollToSelectionHandler } | ||
@@ -636,2 +663,62 @@ export { defineScrollToSelectionHandler as defineScrollToSelectionHandler_alias_1 } | ||
/** | ||
* @internal | ||
*/ | ||
export declare function defineTestExtension(): Extension< { | ||
Nodes: "text" | "doc" | "paragraph"; | ||
Marks: "bold"; | ||
Commands: { | ||
undo: []; | ||
redo: []; | ||
insertText: [{ | ||
text: string; | ||
from?: number; | ||
to?: number; | ||
}]; | ||
insertNode: [options: { | ||
node: Node_2; | ||
pos?: number; | ||
type?: undefined; | ||
attrs?: undefined; | ||
} | { | ||
node?: undefined; | ||
pos?: number; | ||
type: string; | ||
attrs?: Attrs_2; | ||
}]; | ||
removeNode: [options: { | ||
type: string | NodeType_2; | ||
pos?: number; | ||
}]; | ||
wrap: [{ | ||
nodeType: NodeType_2; | ||
attrs?: Attrs_2 | null; | ||
}]; | ||
setBlockType: [options: { | ||
type: NodeType_2 | string; | ||
attrs?: Attrs_2 | null; | ||
from?: number; | ||
to?: number; | ||
}]; | ||
setNodeAttrs: [options: { | ||
type: string | NodeType_2 | string[] | NodeType_2[]; | ||
attrs: Attrs_2; | ||
pos?: number; | ||
}]; | ||
selectAll: []; | ||
addMark: [options: { | ||
type: string | MarkType_2; | ||
attrs?: Attrs_2 | null; | ||
from?: number; | ||
to?: number; | ||
}]; | ||
removeMark: [options: { | ||
type: string | MarkType_2; | ||
attrs?: Attrs_2 | null; | ||
from?: number; | ||
to?: number; | ||
}]; | ||
}; | ||
}>; | ||
/** | ||
* @public | ||
@@ -652,3 +739,3 @@ */ | ||
*/ | ||
declare function defineTextInputHandler(handler: TextInputHandler): Extension<any>; | ||
declare function defineTextInputHandler(handler: TextInputHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineTextInputHandler } | ||
@@ -662,3 +749,3 @@ export { defineTextInputHandler as defineTextInputHandler_alias_1 } | ||
*/ | ||
declare function defineTripleClickHandler(handler: TripleClickHandler): Extension<any>; | ||
declare function defineTripleClickHandler(handler: TripleClickHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineTripleClickHandler } | ||
@@ -672,3 +759,3 @@ export { defineTripleClickHandler as defineTripleClickHandler_alias_1 } | ||
*/ | ||
declare function defineTripleClickOnHandler(handler: TripleClickOnHandler): Extension<any>; | ||
declare function defineTripleClickOnHandler(handler: TripleClickOnHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineTripleClickOnHandler } | ||
@@ -682,3 +769,3 @@ export { defineTripleClickOnHandler as defineTripleClickOnHandler_alias_1 } | ||
*/ | ||
declare function defineUnmountHandler(handler: UnmountHandler): Extension<any>; | ||
declare function defineUnmountHandler(handler: UnmountHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineUnmountHandler } | ||
@@ -692,3 +779,3 @@ export { defineUnmountHandler as defineUnmountHandler_alias_1 } | ||
*/ | ||
declare function defineUpdateHandler(handler: UpdateHandler): Extension<any>; | ||
declare function defineUpdateHandler(handler: UpdateHandler): Extension<ExtensionTyping<any, any, any>>; | ||
export { defineUpdateHandler } | ||
@@ -778,3 +865,2 @@ export { defineUpdateHandler as defineUpdateHandler_alias_1 } | ||
private instance; | ||
private constructor(); | ||
private afterMounted; | ||
@@ -784,2 +870,6 @@ /** | ||
*/ | ||
constructor(instance: EditorInstance); | ||
/** | ||
* @internal | ||
*/ | ||
static create(instance: any): Editor<any>; | ||
@@ -798,2 +888,5 @@ /** | ||
get schema(): Schema<ExtractNodes<E>, ExtractMarks<E>>; | ||
/** | ||
* All commands defined by the editor. | ||
*/ | ||
get commands(): ExtractCommandAppliers<E>; | ||
@@ -821,4 +914,20 @@ /** | ||
blur(): void; | ||
/** | ||
* Register an extension to the editor. Return a function to unregister the | ||
* extension. | ||
*/ | ||
use(extension: Extension): VoidFunction; | ||
/** | ||
* The editor's current state. | ||
*/ | ||
get state(): EditorState; | ||
/** | ||
* Update the editor's state. | ||
* | ||
* @remarks | ||
* | ||
* This is an advanced method. Use it only if you have a specific reason to | ||
* directly manipulate the editor's state. | ||
*/ | ||
updateState(state: EditorState): void; | ||
get nodes(): Record<ExtractNodes<E>, NodeBuilder>; | ||
@@ -833,2 +942,27 @@ get marks(): Record<ExtractMarks<E>, MarkBuilder>; | ||
*/ | ||
export declare class EditorInstance { | ||
view: EditorView | null; | ||
schema: Schema; | ||
nodeBuilders: Record<string, NodeBuilder>; | ||
markBuilders: Record<string, MarkBuilder>; | ||
commandAppliers: Record<string, CommandApplier>; | ||
private tree; | ||
private directEditorProps; | ||
constructor(extension: Extension); | ||
getState: () => EditorState; | ||
updateState(state: EditorState): void; | ||
updateExtension(extension: Extension, add: boolean): void; | ||
mount(place: HTMLElement): void; | ||
unmount(): void; | ||
get mounted(): boolean; | ||
get assertView(): EditorView; | ||
definePlugins(plugins: readonly Plugin_2[]): void; | ||
removePlugins(plugins: readonly Plugin_2[]): void; | ||
defineCommand<Args extends any[] = any[]>(name: string, commandCreator: CommandCreator<Args>): void; | ||
removeCommand(name: string): void; | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
declare class EditorNotFoundError extends ProseKitError { | ||
@@ -909,3 +1043,3 @@ constructor(); | ||
*/ | ||
declare interface Extension<T extends ExtensionTyping<any, any, any> = any> { | ||
declare interface Extension<T extends ExtensionTyping<any, any, any> = ExtensionTyping<any, any, any>> { | ||
extension: Extension | Extension[]; | ||
@@ -968,6 +1102,8 @@ priority?: Priority; | ||
export declare function extractTags(node: TaggedProseMirrorNode): Record<string, number>; | ||
/** | ||
* @internal | ||
*/ | ||
export declare type ExtractTyping<E extends Extension> = E extends Extension<infer T> ? T : never; | ||
export declare type ExtractTyping<E extends Extension> = E extends Extension<ExtensionTyping<infer N, infer M, infer C>> ? ExtensionTyping<PickStringLiteral<N>, PickStringLiteral<M>, PickKnownCommandTyping<C>> : never; | ||
@@ -1346,7 +1482,12 @@ /** | ||
export declare interface MarkBuilder { | ||
/** | ||
* @public | ||
*/ | ||
declare interface MarkBuilder { | ||
(attrs: Attrs | null, ...children: NodeChild[]): ProseMirrorNode[]; | ||
(...children: NodeChild[]): ProseMirrorNode[]; | ||
isActive(attrs?: Attrs): boolean; | ||
isActive: (attrs?: Attrs) => boolean; | ||
} | ||
export { MarkBuilder } | ||
export { MarkBuilder as MarkBuilder_alias_1 } | ||
@@ -1421,8 +1562,16 @@ /** | ||
export declare interface NodeBuilder { | ||
/** | ||
* @public | ||
*/ | ||
declare interface NodeBuilder { | ||
(attrs: Attrs | null, ...children: NodeChild[]): ProseMirrorNode; | ||
(...children: NodeChild[]): ProseMirrorNode; | ||
isActive(attrs?: Attrs): boolean; | ||
isActive: (attrs?: Attrs) => boolean; | ||
} | ||
export { NodeBuilder } | ||
export { NodeBuilder as NodeBuilder_alias_1 } | ||
/** | ||
* @public | ||
*/ | ||
export declare type NodeChild = ProseMirrorNode | string | NodeChild[]; | ||
@@ -1542,2 +1691,14 @@ | ||
*/ | ||
export declare type PickKnownCommandTyping<T extends CommandTyping> = [ | ||
CommandTyping | ||
] extends [T] ? never : T; | ||
/** | ||
* @internal | ||
*/ | ||
export declare type PickStringLiteral<T extends string> = [string] extends [T] ? never : T; | ||
/** | ||
* @internal | ||
*/ | ||
declare const pluginFacet: Facet<PluginPayload, StatePayload>; | ||
@@ -1721,2 +1882,7 @@ export { pluginFacet } | ||
*/ | ||
export declare function setupEditorExtension<E extends Extension>(options: EditorOptions<E>): E; | ||
/** | ||
* @internal | ||
*/ | ||
declare type SimplifyUnion<T> = Simplify<UnionToIntersection<T>>; | ||
@@ -1769,2 +1935,33 @@ export { SimplifyUnion } | ||
export declare type TaggedProseMirrorNode = ProseMirrorNode & { | ||
tags?: Tags; | ||
}; | ||
declare type Tags = Record<string, number>; | ||
/** | ||
* An editor for testing purposes. | ||
* @public | ||
*/ | ||
declare class TestEditor<E extends Extension = Extension> extends Editor<E> { | ||
constructor(instance: EditorInstance); | ||
/** | ||
* Set the editor state to the given document. You can use special tokens | ||
* `<a>` and `<b>` to set the anchor and head positions of the selection. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const editor = createTestEditor({ extension }) | ||
* const n = editor.nodes | ||
* const doc = n.doc(n.paragraph('<a>Hello<b> world!')) | ||
* editor.set(doc) // "Hello" is selected. | ||
* ``` | ||
*/ | ||
set(doc: ProseMirrorNode): void; | ||
dispatchEvent(event: Event): void; | ||
} | ||
export { TestEditor } | ||
export { TestEditor as TestEditor_alias_1 } | ||
declare type TextInputHandler = (view: EditorView, from: number, to: number, text: string) => boolean | void; | ||
@@ -1829,3 +2026,3 @@ export { TextInputHandler } | ||
*/ | ||
declare function union<E extends Extension | Extension[]>(extension: E): UnionExtension<E>; | ||
declare function union<const E extends Extension | readonly Extension[]>(extension: E): UnionExtension<E>; | ||
export { union } | ||
@@ -1837,3 +2034,3 @@ export { union as union_alias_1 } | ||
*/ | ||
declare type UnionExtension<E extends Extension | Extension[]> = E extends Extension[] ? Extension<{ | ||
declare type UnionExtension<E extends Extension | readonly Extension[]> = E extends readonly Extension[] ? Extension<{ | ||
Nodes: ExtractNodes<E[number]>; | ||
@@ -1840,0 +2037,0 @@ Marks: ExtractMarks<E[number]>; |
export { addMark } from './_tsup-dts-rollup'; | ||
export { expandMark } from './_tsup-dts-rollup'; | ||
export { insertNode } from './_tsup-dts-rollup'; | ||
export { removeMark } from './_tsup-dts-rollup'; | ||
export { removeNode } from './_tsup-dts-rollup'; | ||
export { removeMark } from './_tsup-dts-rollup'; | ||
export { setBlockType } from './_tsup-dts-rollup'; | ||
@@ -10,2 +10,4 @@ export { setNodeAttrs } from './_tsup-dts-rollup'; | ||
export { toggleNode } from './_tsup-dts-rollup'; | ||
export { MarkBuilder } from './_tsup-dts-rollup'; | ||
export { NodeBuilder } from './_tsup-dts-rollup'; | ||
export { Editor } from './_tsup-dts-rollup'; | ||
@@ -12,0 +14,0 @@ export { createEditor } from './_tsup-dts-rollup'; |
@@ -0,36 +1,43 @@ | ||
import { | ||
Editor, | ||
EditorNotFoundError, | ||
Priority, | ||
ProseKitError, | ||
assert, | ||
createEditor, | ||
defineDefaultState, | ||
defineFacet, | ||
defineFacetPayload, | ||
elementFromJSON, | ||
elementFromNode, | ||
getMarkType, | ||
getNodeType, | ||
htmlFromJSON, | ||
htmlFromNode, | ||
isAllSelection, | ||
isMark, | ||
isMarkAbsent, | ||
isMarkActive, | ||
isNodeActive, | ||
isNodeSelection, | ||
isNotNull, | ||
isProseMirrorNode, | ||
isTextSelection, | ||
jsonFromHTML, | ||
jsonFromNode, | ||
jsonFromState, | ||
nodeFromElement, | ||
nodeFromHTML, | ||
nodeFromJSON, | ||
rootFacet, | ||
schemaFacet, | ||
stateFacet, | ||
stateFromJSON, | ||
toReversed, | ||
union | ||
} from "./chunk-W4AZKLNQ.js"; | ||
// src/commands/add-mark.ts | ||
import "@prosekit/pm/model"; | ||
import "@prosekit/pm/state"; | ||
// src/utils/get-mark-type.ts | ||
import "@prosekit/pm/model"; | ||
// src/error.ts | ||
var ProseKitError = class extends Error { | ||
}, EditorNotFoundError = class extends ProseKitError { | ||
constructor() { | ||
super( | ||
"Unable to find editor. Pass it as an argument or call this function inside a ProseKit component." | ||
); | ||
} | ||
}, DOMDocumentNotFoundError = class extends ProseKitError { | ||
constructor() { | ||
super( | ||
"Unable to find browser Document. When not in the browser environment, you need to pass a DOM Document." | ||
); | ||
} | ||
}; | ||
// src/utils/get-mark-type.ts | ||
function getMarkType(schema, type) { | ||
if (typeof type == "string") { | ||
let markType = schema.marks[type]; | ||
if (!markType) | ||
throw new ProseKitError(`Cannot find mark type "${type}"`); | ||
return markType; | ||
} | ||
return type; | ||
} | ||
// src/commands/add-mark.ts | ||
function addMark(options) { | ||
@@ -75,20 +82,2 @@ return (state, dispatch) => { | ||
// src/utils/assert.ts | ||
function assert(condition, message = "Assertion failed") { | ||
if (!condition) | ||
throw new ProseKitError(message); | ||
} | ||
// src/utils/get-node-type.ts | ||
import "@prosekit/pm/model"; | ||
function getNodeType(schema, type) { | ||
if (typeof type == "string") { | ||
let nodeType = schema.nodes[type]; | ||
if (!nodeType) | ||
throw new ProseKitError(`Cannot find ProseMirror node type "${type}"`); | ||
return nodeType; | ||
} | ||
return type; | ||
} | ||
// src/utils/set-selection-around.ts | ||
@@ -121,2 +110,13 @@ import { TextSelection as TextSelection2 } from "@prosekit/pm/state"; | ||
// src/commands/remove-mark.ts | ||
import "@prosekit/pm/model"; | ||
import "@prosekit/pm/state"; | ||
function removeMark(options) { | ||
return (state, dispatch) => { | ||
var _a, _b; | ||
let markType = getMarkType(state.schema, options.type), mark = options.attrs ? markType.create(options.attrs) : markType, from = (_a = options.from) != null ? _a : state.selection.from, to = (_b = options.to) != null ? _b : state.selection.to; | ||
return from > to ? !1 : (dispatch == null || dispatch(state.tr.removeMark(from, to, mark)), !0); | ||
}; | ||
} | ||
// src/utils/find-parent-node.ts | ||
@@ -146,13 +146,2 @@ function findParentNode(nodeType, $pos) { | ||
// src/commands/remove-mark.ts | ||
import "@prosekit/pm/model"; | ||
import "@prosekit/pm/state"; | ||
function removeMark(options) { | ||
return (state, dispatch) => { | ||
var _a, _b; | ||
let markType = getMarkType(state.schema, options.type), mark = options.attrs ? markType.create(options.attrs) : markType, from = (_a = options.from) != null ? _a : state.selection.from, to = (_b = options.to) != null ? _b : state.selection.to; | ||
return from > to ? !1 : (dispatch == null || dispatch(state.tr.removeMark(from, to, mark)), !0); | ||
}; | ||
} | ||
// src/commands/set-block-type.ts | ||
@@ -298,24 +287,2 @@ import "@prosekit/pm/state"; | ||
import "@prosekit/pm/state"; | ||
// src/utils/attrs-match.ts | ||
function attrsMatch(nodeOrMark, attrs) { | ||
let currentAttrs = nodeOrMark.attrs; | ||
for (let [key, value] of Object.entries(attrs)) | ||
if (currentAttrs[key] !== value) | ||
return !1; | ||
return !0; | ||
} | ||
// src/utils/is-node-active.ts | ||
function isNodeActive(state, type, attrs) { | ||
let $pos = state.selection.$from, nodeType = getNodeType(state.schema, type); | ||
for (let depth = $pos.depth; depth >= 0; depth--) { | ||
let node = $pos.node(depth); | ||
if (node.type === nodeType && (!attrs || attrsMatch(node, attrs))) | ||
return !0; | ||
} | ||
return !1; | ||
} | ||
// src/commands/toggle-node.ts | ||
function toggleNode({ | ||
@@ -336,726 +303,2 @@ type, | ||
// src/editor/editor.ts | ||
import "@prosekit/pm/model"; | ||
import { EditorState as EditorState2 } from "@prosekit/pm/state"; | ||
import { EditorView } from "@prosekit/pm/view"; | ||
// src/extensions/default-state.ts | ||
import { Selection } from "@prosekit/pm/state"; | ||
// src/types/priority.ts | ||
var Priority = /* @__PURE__ */ ((Priority2) => (Priority2[Priority2.lowest = 0] = "lowest", Priority2[Priority2.low = 1] = "low", Priority2[Priority2.default = 2] = "default", Priority2[Priority2.high = 3] = "high", Priority2[Priority2.highest = 4] = "highest", Priority2))(Priority || {}); | ||
// src/facets/base-extension.ts | ||
import "@prosekit/pm/model"; | ||
// src/utils/array.ts | ||
function uniqPush(prev, next) { | ||
let result = [...prev]; | ||
for (let item of next) | ||
result.includes(item) || result.push(item); | ||
return result; | ||
} | ||
function arraySubstract(a, b) { | ||
return a.filter((x) => !b.includes(x)); | ||
} | ||
function toReversed(arr) { | ||
var _a, _b; | ||
return (_b = (_a = arr.toReversed) == null ? void 0 : _a.call(arr)) != null ? _b : [...arr].reverse(); | ||
} | ||
// src/utils/is-not-null.ts | ||
function isNotNull(value) { | ||
return value != null; | ||
} | ||
// src/facets/facet-node.ts | ||
function zip5(a, b, mapper) { | ||
return [ | ||
mapper(a[0], b[0]), | ||
mapper(a[1], b[1]), | ||
mapper(a[2], b[2]), | ||
mapper(a[3], b[3]), | ||
mapper(a[4], b[4]) | ||
]; | ||
} | ||
function unionInput(a, b) { | ||
return !a && !b ? null : uniqPush(a != null ? a : [], b != null ? b : []); | ||
} | ||
function subtractInput(a, b) { | ||
return a ? b ? arraySubstract(a, b) : [...a] : null; | ||
} | ||
function unionChildren(a, b) { | ||
let merged = new Map(a); | ||
for (let [key, valueB] of b.entries()) { | ||
let valueA = a.get(key); | ||
merged.set(key, valueA ? unionFacetNode(valueA, valueB) : valueB); | ||
} | ||
return merged; | ||
} | ||
function subtractChildren(a, b) { | ||
let merged = new Map(a); | ||
for (let [key, valueB] of b.entries()) { | ||
let valueA = a.get(key); | ||
valueA && merged.set(key, subtractFacetNode(valueA, valueB)); | ||
} | ||
return merged; | ||
} | ||
function unionFacetNode(a, b) { | ||
return assert(a.facet === b.facet), new FacetNode( | ||
a.facet, | ||
zip5(a.inputs, b.inputs, unionInput), | ||
unionChildren(a.children, b.children), | ||
a.reducers | ||
); | ||
} | ||
function subtractFacetNode(a, b) { | ||
return assert(a.facet === b.facet), new FacetNode( | ||
a.facet, | ||
zip5(a.inputs, b.inputs, subtractInput), | ||
subtractChildren(a.children, b.children), | ||
a.reducers | ||
); | ||
} | ||
var FacetNode = class { | ||
constructor(facet, inputs = [null, null, null, null, null], children = /* @__PURE__ */ new Map(), reducers = [ | ||
null, | ||
null, | ||
null, | ||
null, | ||
null | ||
]) { | ||
this.facet = facet; | ||
this.inputs = inputs; | ||
this.children = children; | ||
this.reducers = reducers; | ||
this.output = null; | ||
} | ||
calcOutput() { | ||
var _a, _b, _c; | ||
let inputs = [null, null, null, null, null], output = [null, null, null, null, null]; | ||
for (let pri = 0; pri < 5; pri++) { | ||
let input = this.inputs[pri]; | ||
input && (inputs[pri] = [...input]); | ||
} | ||
for (let child of this.children.values()) { | ||
let childOutput = child.getOutput(); | ||
for (let pri = 0; pri < 5; pri++) | ||
childOutput[pri] && (inputs[pri] || (inputs[pri] = [])).push(childOutput[pri]); | ||
} | ||
if (this.facet.singleton) { | ||
let reducer = (_a = this.reducers)[_b = 2 /* default */] || (_a[_b] = this.facet.reducer), input = inputs.filter(isNotNull).flat(); | ||
output[2 /* default */] = reducer(input); | ||
} else | ||
for (let pri = 0; pri < 5; pri++) { | ||
let input = inputs[pri]; | ||
if (input) { | ||
let reducer = (_c = this.reducers)[pri] || (_c[pri] = this.facet.reducer); | ||
output[pri] = reducer(input); | ||
} | ||
} | ||
return output; | ||
} | ||
getOutput() { | ||
return this.output || (this.output = this.calcOutput()), this.output; | ||
} | ||
getSingletonOutput() { | ||
return assert(this.facet.singleton), this.getOutput()[2 /* default */]; | ||
} | ||
getRootOutput() { | ||
assert(this.isRoot()); | ||
let output = this.getSingletonOutput(); | ||
return assert(output), output; | ||
} | ||
isRoot() { | ||
return !this.facet.parent; | ||
} | ||
}; | ||
// src/facets/schema.ts | ||
import { Schema as Schema4 } from "@prosekit/pm/model"; | ||
// src/facets/facet.ts | ||
var facetCount = 0, Facet = class { | ||
/** | ||
* @internal | ||
*/ | ||
constructor(parent, singleton, _reducer, _reduce) { | ||
this._reducer = _reducer; | ||
this._reduce = _reduce; | ||
/** | ||
* @internal | ||
*/ | ||
this.index = facetCount++; | ||
assert((_reduce || _reducer) && !(_reduce && _reducer)), this.parent = parent, this.singleton = singleton, this.path = parent ? [...parent.path, this.index] : []; | ||
} | ||
get reducer() { | ||
var _a, _b; | ||
return (_b = this._reducer) != null ? _b : (_a = this._reduce) == null ? void 0 : _a.call(this); | ||
} | ||
}; | ||
function defineFacet(options) { | ||
var _a; | ||
return new Facet( | ||
options.parent, | ||
(_a = options.singleton) != null ? _a : !1, | ||
options.reducer, | ||
options.reduce | ||
); | ||
} | ||
// src/facets/root.ts | ||
function rootReducer(inputs) { | ||
var _a; | ||
let schema, commands, stateFunc, view; | ||
for (let input of inputs) | ||
schema = input.schema || schema, commands = input.commands || commands, stateFunc = input.state || stateFunc, view = input.view || view; | ||
let state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema }); | ||
return { schema, state, commands, view }; | ||
} | ||
var rootFacet = new Facet( | ||
null, | ||
!0, | ||
rootReducer | ||
); | ||
// src/facets/schema.ts | ||
var schemaFacet = defineFacet({ | ||
reducer: (specs) => { | ||
assert(specs.length <= 1); | ||
let spec = specs[0]; | ||
return { schema: spec ? new Schema4(spec) : null }; | ||
}, | ||
parent: rootFacet, | ||
singleton: !0 | ||
}); | ||
// src/facets/base-extension.ts | ||
var BaseExtension = class { | ||
constructor() { | ||
this.extension = []; | ||
this.trees = [null, null, null, null, null]; | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
getTree(priority) { | ||
var _a, _b; | ||
let pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */; | ||
return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri)); | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
findFacetOutput(facet) { | ||
var _a; | ||
let node = this.getTree(); | ||
for (let index of facet.path) | ||
node = node == null ? void 0 : node.children.get(index); | ||
return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null; | ||
} | ||
get schema() { | ||
var _a, _b; | ||
let output = this.findFacetOutput(schemaFacet); | ||
return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null; | ||
} | ||
}; | ||
// src/facets/facet-extension.ts | ||
var FacetExtensionImpl = class extends BaseExtension { | ||
/** | ||
* @internal | ||
*/ | ||
constructor(facet, payloads) { | ||
super(); | ||
this.facet = facet; | ||
this.payloads = payloads; | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
createTree(priority) { | ||
var _a; | ||
let pri = (_a = this.priority) != null ? _a : priority, inputs = [null, null, null, null, null]; | ||
inputs[pri] = [...this.payloads]; | ||
let node = new FacetNode(this.facet, inputs); | ||
for (; node.facet.parent; ) { | ||
let children = /* @__PURE__ */ new Map([[node.facet.index, node]]); | ||
node = new FacetNode(node.facet.parent, void 0, children); | ||
} | ||
return node; | ||
} | ||
}; | ||
function defineFacetPayload(facet, payloads) { | ||
return new FacetExtensionImpl(facet, payloads); | ||
} | ||
// src/facets/state.ts | ||
var stateFacet = defineFacet({ | ||
reduce: () => { | ||
let callbacks = [], state = (ctx) => { | ||
var _a, _b, _c, _d, _e, _f; | ||
let configs = callbacks.map((cb) => cb(ctx)), config = { | ||
schema: ctx.schema, | ||
storedMarks: [], | ||
plugins: [] | ||
}; | ||
for (let c of configs) | ||
config.schema = (_a = config.schema) != null ? _a : c.schema, config.doc = (_b = config.doc) != null ? _b : c.doc, config.selection = (_c = config.selection) != null ? _c : c.selection, config.storedMarks = [...config.storedMarks, ...(_d = c.storedMarks) != null ? _d : []], config.plugins = uniqPush((_e = config.plugins) != null ? _e : [], (_f = c.plugins) != null ? _f : []); | ||
return assert( | ||
config.doc || config.schema, | ||
"Can't create state without a schema nor a document" | ||
), config.doc && (config.schema = void 0), config; | ||
}; | ||
return function(inputs) { | ||
return callbacks = inputs, { state }; | ||
}; | ||
}, | ||
singleton: !0, | ||
parent: rootFacet | ||
}); | ||
// src/utils/parse.ts | ||
import { DOMParser, DOMSerializer } from "@prosekit/pm/model"; | ||
import { EditorState } from "@prosekit/pm/state"; | ||
// src/utils/get-dom-api.ts | ||
function findGlobalBrowserDocument() { | ||
if (typeof document != "undefined") | ||
return document; | ||
if (typeof globalThis != "undefined" && globalThis.document) | ||
return globalThis.document; | ||
} | ||
function findGlobalBrowserWindow() { | ||
if (typeof window != "undefined") | ||
return window; | ||
if (typeof globalThis != "undefined" && globalThis.window) | ||
return globalThis.window; | ||
} | ||
function findBrowserDocument(options) { | ||
var _a, _b, _c; | ||
return (_c = (_a = options == null ? void 0 : options.document) != null ? _a : findGlobalBrowserDocument()) != null ? _c : (_b = findGlobalBrowserWindow()) == null ? void 0 : _b.document; | ||
} | ||
function findBrowserWindow(options) { | ||
var _a, _b, _c, _d; | ||
return (_d = (_b = (_a = options == null ? void 0 : options.document) == null ? void 0 : _a.defaultView) != null ? _b : findGlobalBrowserWindow()) != null ? _d : (_c = findBrowserDocument(options)) == null ? void 0 : _c.defaultView; | ||
} | ||
function getBrowserDocument(options) { | ||
let doc = findBrowserDocument(options); | ||
if (doc) return doc; | ||
throw new DOMDocumentNotFoundError(); | ||
} | ||
function getBrowserWindow(options) { | ||
let win = findBrowserWindow(options); | ||
if (win) return win; | ||
throw new DOMDocumentNotFoundError(); | ||
} | ||
// src/utils/parse.ts | ||
function jsonFromState(state) { | ||
return state.toJSON(); | ||
} | ||
function stateFromJSON(json, options) { | ||
return EditorState.fromJSON({ schema: options.schema }, json); | ||
} | ||
function jsonFromNode(node) { | ||
return node.toJSON(); | ||
} | ||
function nodeFromJSON(json, options) { | ||
return options.schema.nodeFromJSON(json); | ||
} | ||
function nodeFromElement(element, options) { | ||
let Parser = options.DOMParser || DOMParser, schema = options.schema; | ||
return Parser.fromSchema(schema).parse(element); | ||
} | ||
function elementFromNode(node, options) { | ||
let Serializer = (options == null ? void 0 : options.DOMSerializer) || DOMSerializer, document2 = getBrowserDocument(options), schema = node.type.schema, serializer = Serializer.fromSchema(schema); | ||
return schema.topNodeType !== node.type ? serializer.serializeNode(node, { document: document2 }) : serializer.serializeFragment( | ||
node.content, | ||
{ document: document2 }, | ||
document2.createElement("div") | ||
); | ||
} | ||
function elementFromHTML(html, options) { | ||
let win = getBrowserWindow(options); | ||
return new win.DOMParser().parseFromString(`<body><div>${html}</div></body>`, "text/html").body.firstElementChild; | ||
} | ||
function htmlFromElement(element) { | ||
return element.outerHTML; | ||
} | ||
function nodeFromHTML(html, options) { | ||
return nodeFromElement(elementFromHTML(html, options), options); | ||
} | ||
function htmlFromNode(node, options) { | ||
return elementFromNode(node, options).outerHTML; | ||
} | ||
function jsonFromElement(element, options) { | ||
return jsonFromNode(nodeFromElement(element, options)); | ||
} | ||
function elementFromJSON(json, options) { | ||
return elementFromNode(nodeFromJSON(json, options), options); | ||
} | ||
function jsonFromHTML(html, options) { | ||
return jsonFromElement(elementFromHTML(html, options), options); | ||
} | ||
function htmlFromJSON(json, options) { | ||
return htmlFromElement(elementFromJSON(json, options)); | ||
} | ||
// src/extensions/default-state.ts | ||
function defineDefaultState({ | ||
defaultDoc, | ||
defaultHTML, | ||
defaultSelection | ||
}) { | ||
if (defaultHTML && defaultDoc) | ||
throw new ProseKitError( | ||
"Only one of defaultHTML and defaultDoc can be provided" | ||
); | ||
return defineFacetPayload(stateFacet, [ | ||
({ schema }) => { | ||
let config = {}; | ||
return defaultHTML && (typeof defaultHTML == "string" ? defaultDoc = jsonFromHTML(defaultHTML, { schema }) : defaultDoc = jsonFromElement(defaultHTML, { schema })), defaultDoc && (config.doc = schema.nodeFromJSON(defaultDoc), defaultSelection && (config.selection = Selection.fromJSON(config.doc, defaultSelection))), config; | ||
} | ||
]); | ||
} | ||
// src/utils/deep-equals.ts | ||
import OrderedMap from "orderedmap"; | ||
function deepEquals(a, b) { | ||
if (a === b) | ||
return !0; | ||
if (!a || !b) | ||
return !1; | ||
if (Array.isArray(a) && Array.isArray(b)) | ||
return a.length === b.length && a.every((x, i) => deepEquals(x, b[i])); | ||
if (a instanceof OrderedMap && b instanceof OrderedMap) | ||
return a.size === b.size && deepEquals(a.toObject(), b.toObject()); | ||
if (typeof a == "object" && typeof b == "object") { | ||
let aKeys = Object.keys(a), bKeys = Object.keys(b); | ||
return aKeys.length === bKeys.length && aKeys.every((key) => deepEquals(a[key], b[key])); | ||
} | ||
return !1; | ||
} | ||
// src/editor/builder.ts | ||
import "@prosekit/pm/model"; | ||
// src/utils/is-mark-absent.ts | ||
function isMarkAbsent(node, from, to, markType, attrs) { | ||
let mark = attrs ? markType.create(attrs) : markType, missing = !1; | ||
return node.nodesBetween(from, to, (node2, pos, parent) => { | ||
if (missing) return !1; | ||
missing = !mark.isInSet(node2.marks) && !!parent && parent.type.allowsMarkType(markType); | ||
}), missing; | ||
} | ||
// src/utils/is-mark-active.ts | ||
function isMarkActive(state, type, attrs) { | ||
let { from, $from, to, empty } = state.selection, markType = getMarkType(state.schema, type); | ||
return empty ? !!(attrs ? markType.create(attrs) : markType).isInSet(state.storedMarks || $from.marks()) : !isMarkAbsent(state.doc, from, to, markType, attrs); | ||
} | ||
// src/utils/type-assertion.ts | ||
import { Mark, ProseMirrorNode as ProseMirrorNode2 } from "@prosekit/pm/model"; | ||
import { | ||
AllSelection, | ||
NodeSelection, | ||
TextSelection as TextSelection5 | ||
} from "@prosekit/pm/state"; | ||
function isProseMirrorNode(node) { | ||
return node instanceof ProseMirrorNode2; | ||
} | ||
function isMark(mark) { | ||
return mark instanceof Mark; | ||
} | ||
function isTextSelection(sel) { | ||
return sel instanceof TextSelection5; | ||
} | ||
function isNodeSelection(sel) { | ||
return sel instanceof NodeSelection; | ||
} | ||
function isAllSelection(sel) { | ||
return sel instanceof AllSelection; | ||
} | ||
// src/editor/builder.ts | ||
function createNodeBuilder(getState, type) { | ||
let builder = (...args) => buildNode(type, args); | ||
return builder.isActive = (attrs) => { | ||
let state = getState(); | ||
return state ? isNodeActive(state, type, attrs) : !1; | ||
}, builder; | ||
} | ||
function createMarkBuilder(getState, type) { | ||
let builder = (...args) => buildMark(type, args); | ||
return builder.isActive = (attrs) => { | ||
let state = getState(); | ||
return state ? isMarkActive(state, type, attrs) : !1; | ||
}, builder; | ||
} | ||
function buildMark(type, args) { | ||
let [attrs, children] = normalizeArgs(args); | ||
return flattenChildren(type.schema, children, type.create(attrs)); | ||
} | ||
function buildNode(type, args) { | ||
let [attrs, children] = normalizeArgs(args), node = type.createAndFill(attrs, flattenChildren(type.schema, children)); | ||
if (!node) | ||
throw new ProseKitError(`Couldn't create node ${type.name}`); | ||
return node; | ||
} | ||
function flattenChildren(schema, children, mark) { | ||
let nodes = []; | ||
for (let child of children) | ||
if (typeof child == "string") | ||
child && nodes.push(schema.text(child, mark ? [mark] : null)); | ||
else if (Array.isArray(child)) | ||
nodes.push(...flattenChildren(schema, child, mark)); | ||
else if (isProseMirrorNode(child)) | ||
nodes.push(mark ? child.mark(mark.addToSet(child.marks)) : child); | ||
else | ||
throw new ProseKitError(`Invalid node child: ${typeof child}`); | ||
return nodes; | ||
} | ||
function normalizeArgs(args) { | ||
let [attrs, ...children] = args; | ||
return isNodeChild(attrs) ? (children.unshift(attrs), [null, children]) : typeof attrs == "object" ? [attrs, children] : [null, children]; | ||
} | ||
function isNodeChild(value) { | ||
return value ? typeof value == "string" || Array.isArray(value) || isProseMirrorNode(value) : !1; | ||
} | ||
// src/facets/union-extension.ts | ||
var UnionExtensionImpl = class extends BaseExtension { | ||
/** | ||
* @internal | ||
*/ | ||
constructor(extension = []) { | ||
super(); | ||
this.extension = extension; | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
createTree(priority) { | ||
var _a; | ||
let pri = (_a = this.priority) != null ? _a : priority, extensions = [...this.extension]; | ||
extensions.sort((a, b) => { | ||
var _a2, _b; | ||
return ((_a2 = a.priority) != null ? _a2 : pri) - ((_b = b.priority) != null ? _b : pri); | ||
}); | ||
let children = extensions.map((ext) => ext.getTree(pri)); | ||
assert(children.length > 0); | ||
let node = children[0]; | ||
for (let i = 1; i < children.length; i++) | ||
node = unionFacetNode(node, children[i]); | ||
return node; | ||
} | ||
}; | ||
// src/editor/union.ts | ||
function union(extension) { | ||
let array = Array.isArray(extension) ? extension : [extension]; | ||
return assert(array.length > 0, "At least one extension is required"), new UnionExtensionImpl( | ||
array | ||
); | ||
} | ||
// src/editor/editor.ts | ||
function createEditor(options) { | ||
let { defaultDoc, defaultHTML, defaultSelection } = options, extension = options.extension; | ||
return (defaultDoc || defaultHTML) && (extension = union([ | ||
extension, | ||
defineDefaultState({ | ||
defaultDoc, | ||
defaultHTML, | ||
defaultSelection | ||
}) | ||
])), Editor.create(new EditorInstance(extension)); | ||
} | ||
var EditorInstance = class { | ||
constructor(extension) { | ||
this.view = null; | ||
this.commandAppliers = {}; | ||
this.mount = this.mount.bind(this), this.unmount = this.unmount.bind(this), this.tree = extension.getTree(); | ||
let payload = this.tree.getRootOutput(), schema = payload.schema, stateConfig = payload.state; | ||
assert(schema && stateConfig, "Schema must be defined"); | ||
let state = EditorState2.create(stateConfig); | ||
if (this.cachedState = state, payload.commands) | ||
for (let [name, commandCreator] of Object.entries(payload.commands)) | ||
this.defineCommand(name, commandCreator); | ||
this.directEditorProps = { state, ...payload.view }, this.schema = this.directEditorProps.state.schema; | ||
let getState = () => this.getState(); | ||
this.nodeBuilders = Object.fromEntries( | ||
Object.values(this.schema.nodes).map((type) => [ | ||
type.name, | ||
createNodeBuilder(getState, type) | ||
]) | ||
), this.markBuilders = Object.fromEntries( | ||
Object.values(this.schema.marks).map((type) => [ | ||
type.name, | ||
createMarkBuilder(getState, type) | ||
]) | ||
); | ||
} | ||
getState() { | ||
return this.view && (this.cachedState = this.view.state), this.cachedState; | ||
} | ||
updateExtension(extension, add) { | ||
var _a, _b, _c, _d; | ||
let view = this.view; | ||
if (!view || view.isDestroyed) | ||
return; | ||
let tree = extension.getTree(), payload = tree.getRootOutput(); | ||
if (payload != null && payload.schema) | ||
throw new ProseKitError("Schema cannot be changed"); | ||
if (payload != null && payload.view) | ||
throw new ProseKitError("View cannot be changed"); | ||
let oldPayload = this.tree.getRootOutput(), oldPlugins = [...(_b = (_a = view.state) == null ? void 0 : _a.plugins) != null ? _b : []]; | ||
this.tree = add ? unionFacetNode(this.tree, tree) : subtractFacetNode(this.tree, tree); | ||
let newPayload = this.tree.getRootOutput(), newPlugins = [...(_d = (_c = newPayload == null ? void 0 : newPayload.state) == null ? void 0 : _c.plugins) != null ? _d : []]; | ||
if (!deepEquals(oldPlugins, newPlugins)) { | ||
let state = view.state.reconfigure({ plugins: newPlugins }); | ||
view.updateState(state); | ||
} | ||
if (newPayload != null && newPayload.commands && !deepEquals(oldPayload == null ? void 0 : oldPayload.commands, newPayload == null ? void 0 : newPayload.commands)) { | ||
let commands = newPayload.commands, names = Object.keys(commands); | ||
for (let name of names) | ||
this.defineCommand(name, commands[name]); | ||
} | ||
} | ||
mount(place) { | ||
if (this.view) | ||
throw new ProseKitError("Editor is already mounted"); | ||
if (!place) | ||
throw new ProseKitError("Can't mount editor without a place"); | ||
this.view = new EditorView({ mount: place }, this.directEditorProps); | ||
} | ||
unmount() { | ||
if (!this.view) | ||
throw new ProseKitError("Editor is not mounted yet"); | ||
this.view.destroy(), this.view = null; | ||
} | ||
get mounted() { | ||
return !!this.view && !this.view.isDestroyed; | ||
} | ||
get assertView() { | ||
if (!this.view) | ||
throw new ProseKitError("Editor is not mounted"); | ||
return this.view; | ||
} | ||
definePlugins(plugins) { | ||
let view = this.assertView, state = view.state, newPlugins = [...plugins, ...state.plugins], newState = state.reconfigure({ plugins: newPlugins }); | ||
view.setProps({ state: newState }); | ||
} | ||
removePlugins(plugins) { | ||
let view = this.view; | ||
if (!view) return; | ||
let state = view.state, newPlugins = state.plugins.filter((p) => !plugins.includes(p)), newState = state.reconfigure({ plugins: newPlugins }); | ||
view.setProps({ state: newState }); | ||
} | ||
defineCommand(name, commandCreator) { | ||
let applier = (...args) => { | ||
let view = this.view; | ||
return view ? commandCreator(...args)(view.state, view.dispatch.bind(view), view) : !1; | ||
}; | ||
applier.canApply = (...args) => { | ||
let view = this.view; | ||
return view ? commandCreator(...args)(view.state, void 0, view) : !1; | ||
}, this.commandAppliers[name] = applier; | ||
} | ||
removeCommand(name) { | ||
delete this.commandAppliers[name]; | ||
} | ||
}, Editor = class _Editor { | ||
constructor(instance) { | ||
this.afterMounted = []; | ||
this.instance = instance, this.mount = this.mount.bind(this), this.unmount = this.unmount.bind(this), this.use = this.use.bind(this); | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
static create(instance) { | ||
if (!(instance instanceof EditorInstance)) | ||
throw new TypeError("Invalid EditorInstance"); | ||
return new _Editor(instance); | ||
} | ||
/** | ||
* Whether the editor is mounted. | ||
*/ | ||
get mounted() { | ||
return this.instance.mounted; | ||
} | ||
/** | ||
* The editor view. | ||
*/ | ||
get view() { | ||
return this.instance.assertView; | ||
} | ||
/** | ||
* The editor schema. | ||
*/ | ||
get schema() { | ||
return this.instance.schema; | ||
} | ||
get commands() { | ||
return this.instance.commandAppliers; | ||
} | ||
/** | ||
* Whether the editor is focused. | ||
*/ | ||
get focused() { | ||
var _a, _b; | ||
return (_b = (_a = this.instance.view) == null ? void 0 : _a.hasFocus()) != null ? _b : !1; | ||
} | ||
/** | ||
* Mount the editor to the given HTML element. | ||
* Pass `null` or `undefined` to unmount the editor. | ||
*/ | ||
mount(place) { | ||
if (!place) | ||
return this.unmount(); | ||
this.instance.mount(place), this.afterMounted.forEach((callback) => callback()); | ||
} | ||
/** | ||
* Unmount the editor. This is equivalent to `mount(null)`. | ||
*/ | ||
unmount() { | ||
this.mounted && this.instance.unmount(); | ||
} | ||
/** | ||
* Focus the editor. | ||
*/ | ||
focus() { | ||
var _a; | ||
(_a = this.instance.view) == null || _a.focus(); | ||
} | ||
/** | ||
* Blur the editor. | ||
*/ | ||
blur() { | ||
var _a; | ||
(_a = this.instance.view) == null || _a.dom.blur(); | ||
} | ||
use(extension) { | ||
if (!this.mounted) { | ||
let lazyRemove = null, lazyCreate = () => { | ||
lazyRemove = this.use(extension); | ||
}; | ||
return this.afterMounted.push(lazyCreate), () => { | ||
lazyRemove == null || lazyRemove(); | ||
}; | ||
} | ||
return this.instance.updateExtension(extension, !0), () => this.instance.updateExtension(extension, !1); | ||
} | ||
get state() { | ||
return this.instance.getState(); | ||
} | ||
get nodes() { | ||
return this.instance.nodeBuilders; | ||
} | ||
get marks() { | ||
return this.instance.markBuilders; | ||
} | ||
}; | ||
// src/editor/with-priority.ts | ||
@@ -1077,5 +320,5 @@ function withPriority(extension, priority) { | ||
// src/commands/select-all.ts | ||
import { AllSelection as AllSelection2 } from "@prosekit/pm/state"; | ||
import { AllSelection } from "@prosekit/pm/state"; | ||
function selectAll() { | ||
return (state, dispatch) => (dispatch == null || dispatch(state.tr.setSelection(new AllSelection2(state.doc))), !0); | ||
return (state, dispatch) => (dispatch == null || dispatch(state.tr.setSelection(new AllSelection(state.doc))), !0); | ||
} | ||
@@ -1125,11 +368,11 @@ | ||
// src/extensions/node-spec.ts | ||
import OrderedMap3 from "orderedmap"; | ||
import OrderedMap2 from "orderedmap"; | ||
// src/facets/schema-spec.ts | ||
import "@prosekit/pm/model"; | ||
import OrderedMap2 from "orderedmap"; | ||
import OrderedMap from "orderedmap"; | ||
var schemaSpecFacet = defineFacet({ | ||
reducer: (specs) => { | ||
var _a; | ||
let nodes = OrderedMap2.from({}), marks = OrderedMap2.from({}), topNode; | ||
let nodes = OrderedMap.from({}), marks = OrderedMap.from({}), topNode; | ||
for (let spec of specs) | ||
@@ -1158,3 +401,3 @@ nodes = nodes.append(spec.nodes), marks = marks.append((_a = spec.marks) != null ? _a : {}), topNode = topNode != null ? topNode : spec.topNode; | ||
reducer: (payloads) => { | ||
let nodes = OrderedMap3.from({}), topNodeName, specPayloads = payloads.map((input) => input[0]).filter(isNotNull), attrPayloads = payloads.map((input) => input[1]).filter(isNotNull); | ||
let nodes = OrderedMap2.from({}), topNodeName, specPayloads = payloads.map((input) => input[0]).filter(isNotNull), attrPayloads = payloads.map((input) => input[1]).filter(isNotNull); | ||
for (let { name, topNode, ...spec } of specPayloads) | ||
@@ -1237,7 +480,7 @@ assert(!nodes.get(name), `Node type ${name} can only be defined once`), topNode && (topNodeName = name), nodes = nodes.addToStart(name, spec); | ||
import "@prosekit/pm/model"; | ||
import { Plugin as Plugin2 } from "@prosekit/pm/state"; | ||
import { Plugin } from "@prosekit/pm/state"; | ||
function definePlugin(plugin) { | ||
if (plugin instanceof Plugin2) | ||
if (plugin instanceof Plugin) | ||
return defineFacetPayload(pluginFacet, [() => [plugin]]); | ||
if (Array.isArray(plugin) && plugin.every((p) => p instanceof Plugin2)) | ||
if (Array.isArray(plugin) && plugin.every((p) => p instanceof Plugin)) | ||
return defineFacetPayload(pluginFacet, [() => plugin]); | ||
@@ -1252,5 +495,5 @@ if (typeof plugin == "function") | ||
for (let payload of payloads) | ||
if (payload instanceof Plugin2) | ||
if (payload instanceof Plugin) | ||
plugins.push(payload); | ||
else if (Array.isArray(payload) && payload.every((p) => p instanceof Plugin2)) | ||
else if (Array.isArray(payload) && payload.every((p) => p instanceof Plugin)) | ||
plugins.push(...payload); | ||
@@ -1477,3 +720,3 @@ else if (typeof payload == "function") | ||
import { keydownHandler } from "@prosekit/pm/keymap"; | ||
import { Plugin as Plugin3, PluginKey as PluginKey4 } from "@prosekit/pm/state"; | ||
import { Plugin as Plugin2, PluginKey as PluginKey4 } from "@prosekit/pm/state"; | ||
import { splitSplittableBlock } from "prosemirror-splittable"; | ||
@@ -1499,3 +742,3 @@ var customBaseKeymap = { | ||
reduce: () => { | ||
let handler = null, handlerWrapper = (view, event) => handler ? handler(view, event) : !1, plugin = new Plugin3({ | ||
let handler = null, handlerWrapper = (view, event) => handler ? handler(view, event) : !1, plugin = new Plugin2({ | ||
key: keymapPluginKey, | ||
@@ -1502,0 +745,0 @@ props: { handleKeyDown: handlerWrapper } |
{ | ||
"name": "@prosekit/core", | ||
"type": "module", | ||
"version": "0.0.0-next-20240622120010", | ||
"version": "0.0.0-next-20240626133927", | ||
"private": false, | ||
@@ -32,2 +32,7 @@ "author": { | ||
"default": "./dist/prosekit-core.js" | ||
}, | ||
"./test": { | ||
"types": "./dist/prosekit-core-test.d.ts", | ||
"import": "./dist/prosekit-core-test.js", | ||
"default": "./dist/prosekit-core-test.js" | ||
} | ||
@@ -43,3 +48,3 @@ }, | ||
"type-fest": "^4.20.1", | ||
"@prosekit/pm": "^0.0.0-next-20240622120010" | ||
"@prosekit/pm": "^0.0.0-next-20240626133927" | ||
}, | ||
@@ -49,3 +54,3 @@ "devDependencies": { | ||
"typescript": "^5.5.2", | ||
"vitest": "^1.6.0", | ||
"vitest": "^2.0.0-beta.12", | ||
"@prosekit/dev": "0.0.0" | ||
@@ -62,2 +67,5 @@ }, | ||
"./dist/prosekit-core.d.ts" | ||
], | ||
"test": [ | ||
"./dist/prosekit-core-test.d.ts" | ||
] | ||
@@ -64,0 +72,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
133764
9
3926