@prosekit/extensions 0.16.0-beta.1 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"autocomplete.js","names":[],"sources":["../src/autocomplete/autocomplete-helpers.ts","../src/autocomplete/autocomplete-rule.ts","../src/autocomplete/autocomplete-plugin.ts","../src/autocomplete/autocomplete.ts"],"sourcesContent":["import type { ResolvedPos } from '@prosekit/pm/model'\nimport { PluginKey, type EditorState, type Transaction } from '@prosekit/pm/state'\n\nimport type { AutocompleteRule } from './autocomplete-rule.ts'\n\nexport function defaultCanMatch({ state }: { state: EditorState }): boolean {\n const $pos = state.selection.$from\n return !isInsideCodeBlock($pos) && !isInsideCodeMark($pos)\n}\n\nfunction isInsideCodeBlock($pos: ResolvedPos): boolean {\n for (let d = $pos.depth; d > 0; d--) {\n if ($pos.node(d).type.spec.code) {\n return true\n }\n }\n return false\n}\n\nfunction isInsideCodeMark($pos: ResolvedPos): boolean {\n for (const mark of $pos.marks()) {\n if (mark.type.spec.code) {\n return true\n }\n }\n return false\n}\n\n/**\n * @internal\n */\nexport interface PredictionPluginMatching {\n rule: AutocompleteRule\n from: number\n to: number\n match: RegExpExecArray\n}\n\n/**\n * @internal\n */\nexport interface PredictionPluginState {\n /**\n * The matching positions that should be ignored.\n */\n ignores: Array<number>\n\n /**\n * The current active matching.\n */\n matching: PredictionPluginMatching | null\n}\n\n/**\n * @internal\n */\nexport type PredictionTransactionMeta = {\n type: 'enter'\n matching: PredictionPluginMatching\n} | {\n type: 'leave'\n}\n\nexport function getPluginState(state: EditorState): PredictionPluginState | undefined {\n return pluginKey.getState(state)\n}\n\nexport function getTrMeta(tr: Transaction): PredictionTransactionMeta | undefined {\n return tr.getMeta(pluginKey) as PredictionTransactionMeta | undefined\n}\n\nexport function setTrMeta(\n tr: Transaction,\n meta: PredictionTransactionMeta,\n): Transaction {\n return tr.setMeta(pluginKey, meta)\n}\n\nexport const pluginKey: PluginKey<PredictionPluginState> = new PluginKey<PredictionPluginState>('prosekit-autocomplete')\n","import type { EditorState } from '@prosekit/pm/state'\n\nimport { defaultCanMatch } from './autocomplete-helpers.ts'\n\n/**\n * Options for the {@link MatchHandler} callback.\n */\nexport interface MatchHandlerOptions {\n /**\n * The editor state.\n */\n state: EditorState\n\n /**\n * The result of `RegExp.exec`.\n */\n match: RegExpExecArray\n\n /**\n * The start position of the matched text.\n */\n from: number\n\n /**\n * The end position of the matched text.\n */\n to: number\n\n /**\n * Call this function to ignore the match. You probably want to call this\n * function when the user presses the `Escape` key.\n */\n ignoreMatch: () => void\n\n /**\n * Call this function to delete the matched text. For example, in a slash\n * menu, you might want to delete the matched text first then do something\n * else when the user presses the `Enter` key.\n */\n deleteMatch: () => void\n}\n\n/**\n * A callback that is called when the rule starts to match, and also on\n * subsequent updates while the rule continues to match.\n */\nexport type MatchHandler = (options: MatchHandlerOptions) => void\n\n/**\n * Options for the {@link CanMatchPredicate} callback.\n */\nexport interface CanMatchOptions {\n /**\n * The editor state.\n */\n state: EditorState\n}\n\n/**\n * A predicate to determine if the rule can be applied in the current editor state.\n */\nexport type CanMatchPredicate = (options: CanMatchOptions) => boolean\n\n/**\n * Options for creating an {@link AutocompleteRule}\n */\nexport interface AutocompleteRuleOptions {\n /**\n * The regular expression to match against the text before the cursor. The\n * last match before the cursor is used.\n *\n * For a slash menu, you might use `/(?<!\\S)\\/(\\S.*)?$/u`.\n * For a mention, you might use `/@\\w*$/`\n */\n regex: RegExp\n\n /**\n * A callback that is called when the rule starts to match, and also on\n * subsequent updates while the rule continues to match.\n */\n onEnter: MatchHandler\n\n /**\n * A callback that is called when the rule stops matching.\n */\n onLeave?: VoidFunction\n\n /**\n * A predicate to determine if the rule can be applied in the current editor\n * state. If not provided, it defaults to only allowing matches that are not\n * inside a code block or code mark.\n */\n canMatch?: CanMatchPredicate\n}\n\n/**\n * An autocomplete rule that can be used to create an autocomplete extension.\n *\n * @public\n */\nexport class AutocompleteRule {\n /** @internal */\n readonly regex: RegExp\n /** @internal */\n readonly onMatch: MatchHandler\n /** @internal */\n readonly onLeave?: VoidFunction\n /** @internal */\n readonly canMatch: (options: { state: EditorState }) => boolean\n\n constructor(options: AutocompleteRuleOptions) {\n this.regex = options.regex\n this.onMatch = options.onEnter\n this.onLeave = options.onLeave\n this.canMatch = options.canMatch ?? defaultCanMatch\n }\n}\n","import { OBJECT_REPLACEMENT_CHARACTER } from '@prosekit/core'\nimport type { ProseMirrorNode, ResolvedPos } from '@prosekit/pm/model'\nimport { Plugin, type EditorState, type Transaction } from '@prosekit/pm/state'\nimport type { Mapping } from '@prosekit/pm/transform'\nimport type { EditorView } from '@prosekit/pm/view'\nimport { Decoration, DecorationSet } from '@prosekit/pm/view'\n\nimport {\n getPluginState,\n getTrMeta,\n pluginKey,\n setTrMeta,\n type PredictionPluginMatching,\n type PredictionPluginState,\n type PredictionTransactionMeta,\n} from './autocomplete-helpers.ts'\nimport type { AutocompleteRule } from './autocomplete-rule.ts'\n\n/**\n * Creates a plugin that handles autocomplete functionality.\n *\n * Workflow:\n *\n * 1. {@link handleTextInput}: called when text is going to be input, but the\n * transaction is not yet created. Injects a new matching as a transaction\n * meta if applicable. This is the only place to create a new matching if\n * there is no existing matching.\n * 2. {@link handleTransaction}: called when a transaction is going to be\n * applied. Updates the plugin state based on the transaction. This step\n * determines if a matching should be created, updated or removed.\n * 3. {@link handleUpdate}: called when the editor state is updated. This is the\n * place to call `onMatch` and register `deleteMatch` and `ignoreMatch`\n * callbacks.\n * 4. {@link getDecorations}: creates the decorations for the current matching.\n */\nexport function createAutocompletePlugin({\n getRules,\n}: {\n getRules: () => AutocompleteRule[]\n}): Plugin {\n return new Plugin<PredictionPluginState>({\n key: pluginKey,\n\n state: {\n init: (): PredictionPluginState => {\n return { ignores: [], matching: null }\n },\n apply: (tr, prevValue, oldState, newState): PredictionPluginState => {\n return handleTransaction(tr, prevValue, oldState, newState, getRules)\n },\n },\n\n view: () => ({\n update: handleUpdate,\n }),\n\n props: {\n handleTextInput: (view, from, to, textAdded, getTr) => {\n const meta = handleTextInput(view, from, to, textAdded, getRules)\n if (meta) {\n const tr = getTr()\n setTrMeta(tr, meta)\n view.dispatch(tr)\n return true\n }\n return false\n },\n decorations: getDecorations,\n },\n })\n}\n\nfunction handleTextInput(\n view: EditorView,\n from: number,\n to: number,\n textAdded: string,\n getRules: () => AutocompleteRule[],\n): PredictionTransactionMeta | undefined {\n // Only handle insertions\n if (from !== to) {\n return\n }\n\n const textBackward = getTextBackward(view.state.doc.resolve(from))\n const textFull = textBackward + textAdded\n const textTo = to + textAdded.length\n const textFrom = textTo - textFull.length\n\n const pluginState = getPluginState(view.state)\n const ignores = pluginState?.ignores ?? []\n\n const currMatching = matchRule(\n view.state,\n getRules(),\n textFull,\n textFrom,\n textTo,\n ignores,\n )\n\n if (currMatching) {\n return { type: 'enter', matching: currMatching }\n }\n}\n\nfunction handleTransaction(\n tr: Transaction,\n prevValue: PredictionPluginState,\n oldState: EditorState,\n newState: EditorState,\n getRules: () => AutocompleteRule[],\n): PredictionPluginState {\n const meta = getTrMeta(tr)\n\n if (\n !meta\n && !tr.docChanged\n && oldState.selection.eq(newState.selection)\n ) {\n // No changes\n return prevValue\n }\n\n // Handle position mapping changes\n const ignoreSet = new Set<number>()\n for (const ignore of prevValue.ignores) {\n const result = tr.mapping.mapResult(ignore)\n if (!result.deletedBefore && !result.deletedAfter) {\n ignoreSet.add(result.pos)\n }\n }\n const ignores = Array.from(ignoreSet)\n\n const prevMatching = prevValue.matching && mapMatching(prevValue.matching, tr.mapping)\n\n // If there is no new matching from `handleTextInput`\n if (!meta) {\n if (!prevMatching) {\n return { matching: null, ignores }\n }\n\n const { selection } = newState\n // If the text selection is before the matching or after the matching,\n // we leave the matching\n if (selection.to < prevMatching.from || selection.from > prevMatching.to) {\n ignores.push(prevMatching.from)\n return { matching: null, ignores }\n }\n\n // Get the text between the existing matching\n const text = getTextBetween(newState.doc, prevMatching.from, prevMatching.to)\n // Check the text again to see if it still matches the rule\n const currMatching = matchRule(\n newState,\n getRules(),\n text,\n prevMatching.from,\n prevMatching.to,\n ignores,\n )\n return { matching: currMatching ?? null, ignores }\n }\n\n // If a new matching is being entered from `handleTextInput`\n if (meta.type === 'enter') {\n // Ignore the previous matching if it is not the same as the new matching\n if (prevMatching && prevMatching.from !== meta.matching.from) {\n ignores.push(prevMatching.from)\n }\n\n // Return the new matching\n return { matching: meta.matching, ignores }\n }\n\n // If a matching is being exited\n if (meta.type === 'leave') {\n if (prevMatching) {\n ignores.push(prevMatching.from)\n }\n return { matching: null, ignores }\n }\n\n throw new Error(`Invalid transaction meta: ${meta satisfies never}`)\n}\n\nfunction handleUpdate(view: EditorView, prevState: EditorState): void {\n const prevValue = getPluginState(prevState)\n const currValue = getPluginState(view.state)\n\n if (!prevValue || !currValue) {\n // Should not happen\n return\n }\n\n const prevMatching = prevValue.matching\n const currMatching = currValue.matching\n\n // Deactivate the previous rule\n if (prevMatching && prevMatching.rule !== currMatching?.rule) {\n prevMatching.rule.onLeave?.()\n }\n\n // Activate the current rule\n if (currMatching) {\n const { from, to, match, rule } = currMatching\n\n const textSnapshot = getTextBetween(view.state.doc, from, to)\n\n const deleteMatch = () => {\n if (getTextBetween(view.state.doc, from, to) === textSnapshot) {\n view.dispatch(view.state.tr.delete(from, to))\n }\n }\n\n const ignoreMatch = () => {\n view.dispatch(\n setTrMeta(view.state.tr, { type: 'leave' }),\n )\n }\n\n rule.onMatch({\n state: view.state,\n match,\n from,\n to,\n deleteMatch,\n ignoreMatch,\n })\n }\n}\n\nfunction getDecorations(state: EditorState): DecorationSet | null {\n const pluginState = getPluginState(state)\n if (pluginState?.matching) {\n const { from, to, match } = pluginState.matching\n const deco = Decoration.inline(from, to, {\n 'class': 'prosekit-autocomplete-match',\n 'data-autocomplete-match-text': match[0],\n })\n return DecorationSet.create(state.doc, [deco])\n }\n return null\n}\n\nconst MAX_MATCH = 200\n\n/** Get the text before the given position at the current block. */\nfunction getTextBackward($pos: ResolvedPos): string {\n const parentOffset: number = $pos.parentOffset\n return getTextBetween(\n $pos.parent,\n Math.max(0, parentOffset - MAX_MATCH),\n parentOffset,\n )\n}\n\nfunction getTextBetween(node: ProseMirrorNode, from: number, to: number): string {\n return node.textBetween(\n from,\n to,\n null,\n OBJECT_REPLACEMENT_CHARACTER,\n )\n}\n\nfunction matchRule(\n state: EditorState,\n rules: AutocompleteRule[],\n text: string,\n textFrom: number,\n textTo: number,\n ignores: Array<number>,\n): PredictionPluginMatching | undefined {\n // Find the rightmost ignore point within the text range\n let maxIgnore = -1\n for (const ignore of ignores) {\n if (ignore >= textFrom && ignore < textTo && ignore > maxIgnore) {\n maxIgnore = ignore\n }\n }\n\n // If an ignore point is within the text range, we ignore the text to the left\n // of the ignore point (including the character right after the ignore point).\n if (maxIgnore >= 0) {\n const cut = maxIgnore + 1 - textFrom\n text = text.slice(cut)\n textFrom += cut\n }\n\n if (textFrom >= textTo || !text) {\n return\n }\n\n for (const rule of rules) {\n if (!rule.canMatch({ state })) {\n continue\n }\n\n rule.regex.lastIndex = 0\n const match = rule.regex.exec(text)\n if (!match) {\n continue\n }\n\n const matchTo = textTo\n const matchFrom = textFrom + match.index\n\n return { rule, match, from: matchFrom, to: matchTo }\n }\n}\n\nfunction mapMatching(matching: PredictionPluginMatching, mapping: Mapping): PredictionPluginMatching {\n return {\n rule: matching.rule,\n match: matching.match,\n from: mapping.map(matching.from),\n to: mapping.map(matching.to, -1),\n }\n}\n","import { defineFacet, defineFacetPayload, pluginFacet, type Extension, type PluginPayload } from '@prosekit/core'\n\nimport { createAutocompletePlugin } from './autocomplete-plugin.ts'\nimport type { AutocompleteRule } from './autocomplete-rule.ts'\n\n/**\n * Defines an autocomplete extension that executes logic when the text before\n * the cursor matches the given regular expression.\n *\n * When a match is found, an inline decoration is applied to the matched text\n * with the class `prosekit-autocomplete-match` and a `data-autocomplete-match-text`\n * attribute containing the full matched string.\n */\nexport function defineAutocomplete(rule: AutocompleteRule): Extension {\n return defineFacetPayload(autocompleteFacet, [rule])\n}\n\nconst autocompleteFacet = defineFacet<AutocompleteRule, PluginPayload>({\n reduce: () => {\n let rules: AutocompleteRule[] = []\n const getRules = () => rules\n const plugin = createAutocompletePlugin({ getRules })\n\n return function reducer(inputs) {\n rules = inputs\n return plugin\n }\n },\n parent: pluginFacet,\n singleton: true,\n})\n"],"mappings":";;;AAKA,SAAgB,gBAAgB,EAAE,SAA0C;CAC1E,MAAM,OAAO,MAAM,UAAU;AAC7B,QAAO,CAAC,kBAAkB,KAAK,IAAI,CAAC,iBAAiB,KAAK;;AAG5D,SAAS,kBAAkB,MAA4B;AACrD,MAAK,IAAI,IAAI,KAAK,OAAO,IAAI,GAAG,IAC9B,KAAI,KAAK,KAAK,EAAE,CAAC,KAAK,KAAK,KACzB,QAAO;AAGX,QAAO;;AAGT,SAAS,iBAAiB,MAA4B;AACpD,MAAK,MAAM,QAAQ,KAAK,OAAO,CAC7B,KAAI,KAAK,KAAK,KAAK,KACjB,QAAO;AAGX,QAAO;;AAsCT,SAAgB,eAAe,OAAuD;AACpF,QAAO,UAAU,SAAS,MAAM;;AAGlC,SAAgB,UAAU,IAAwD;AAChF,QAAO,GAAG,QAAQ,UAAU;;AAG9B,SAAgB,UACd,IACA,MACa;AACb,QAAO,GAAG,QAAQ,WAAW,KAAK;;AAGpC,MAAa,YAA8C,IAAI,UAAiC,wBAAwB;;;;;;ACsBxH,IAAa,mBAAb,MAA8B;CAU5B,YAAY,SAAkC;AAC5C,OAAK,QAAQ,QAAQ;AACrB,OAAK,UAAU,QAAQ;AACvB,OAAK,UAAU,QAAQ;AACvB,OAAK,WAAW,QAAQ,YAAY;;;;;;;;;;;;;;;;;;;;AC/ExC,SAAgB,yBAAyB,EACvC,YAGS;AACT,QAAO,IAAI,OAA8B;EACvC,KAAK;EAEL,OAAO;GACL,YAAmC;AACjC,WAAO;KAAE,SAAS,EAAE;KAAE,UAAU;KAAM;;GAExC,QAAQ,IAAI,WAAW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I,SAAS,KAAK;EAChC,IAAI,QAAQ,IAAI,SAAS,IAAI,GAAG;EACjC;;;;;;;;;;ACjTH,SAAgB,mBAAmB,MAAmC;AACpE,QAAO,mBAAmB,mBAAmB,CAAC,KAAK,CAAC;;AAGtD,MAAM,oBAAoB,YAA6C;CACrE,cAAc;EACZ,IAAI,QAA4B,EAAE;EAClC,MAAM,iBAAiB;EACvB,MAAM,SAAS,yBAAyB,EAAE,UAAU,CAAC;AAErD,SAAO,SAAS,QAAQ,QAAQ;AAC9B,WAAQ;AACR,UAAO;;;CAGX,QAAQ;CACR,WAAW;CACZ,CAAC"}
1
+ {"version":3,"file":"autocomplete.js","names":[],"sources":["../src/autocomplete/autocomplete-helpers.ts","../src/autocomplete/autocomplete-rule.ts","../src/autocomplete/autocomplete-plugin.ts","../src/autocomplete/autocomplete.ts"],"sourcesContent":["import type { ResolvedPos } from '@prosekit/pm/model'\nimport { PluginKey, type EditorState, type Transaction } from '@prosekit/pm/state'\n\nimport type { AutocompleteRule } from './autocomplete-rule.ts'\n\nexport function defaultCanMatch({ state }: { state: EditorState }): boolean {\n const $pos = state.selection.$from\n return !isInsideCodeBlock($pos) && !isInsideCodeMark($pos)\n}\n\nfunction isInsideCodeBlock($pos: ResolvedPos): boolean {\n for (let d = $pos.depth; d > 0; d--) {\n if ($pos.node(d).type.spec.code) {\n return true\n }\n }\n return false\n}\n\nfunction isInsideCodeMark($pos: ResolvedPos): boolean {\n for (const mark of $pos.marks()) {\n if (mark.type.spec.code) {\n return true\n }\n }\n return false\n}\n\n/**\n * @internal\n */\nexport interface PredictionPluginMatching {\n rule: AutocompleteRule\n from: number\n to: number\n match: RegExpExecArray\n}\n\n/**\n * @internal\n */\nexport interface PredictionPluginState {\n /**\n * The matching positions that should be ignored.\n */\n ignores: Array<number>\n\n /**\n * The current active matching.\n */\n matching: PredictionPluginMatching | null\n}\n\n/**\n * @internal\n */\nexport type PredictionTransactionMeta = {\n type: 'enter'\n matching: PredictionPluginMatching\n} | {\n type: 'leave'\n}\n\nexport function getPluginState(state: EditorState): PredictionPluginState | undefined {\n return pluginKey.getState(state)\n}\n\nexport function getTrMeta(tr: Transaction): PredictionTransactionMeta | undefined {\n return tr.getMeta(pluginKey) as PredictionTransactionMeta | undefined\n}\n\nexport function setTrMeta(\n tr: Transaction,\n meta: PredictionTransactionMeta,\n): Transaction {\n return tr.setMeta(pluginKey, meta)\n}\n\nexport const pluginKey: PluginKey<PredictionPluginState> = new PluginKey<PredictionPluginState>('prosekit-autocomplete')\n","import type { EditorState } from '@prosekit/pm/state'\n\nimport { defaultCanMatch } from './autocomplete-helpers.ts'\n\n/**\n * Options for the {@link MatchHandler} callback.\n */\nexport interface MatchHandlerOptions {\n /**\n * The editor state.\n */\n state: EditorState\n\n /**\n * The result of `RegExp.exec`.\n */\n match: RegExpExecArray\n\n /**\n * The start position of the matched text.\n */\n from: number\n\n /**\n * The end position of the matched text.\n */\n to: number\n\n /**\n * Call this function to ignore the match. You probably want to call this\n * function when the user presses the `Escape` key.\n */\n ignoreMatch: () => void\n\n /**\n * Call this function to delete the matched text. For example, in a slash\n * menu, you might want to delete the matched text first then do something\n * else when the user presses the `Enter` key.\n */\n deleteMatch: () => void\n}\n\n/**\n * A callback that is called when the rule starts to match, and also on\n * subsequent updates while the rule continues to match.\n */\nexport type MatchHandler = (options: MatchHandlerOptions) => void\n\n/**\n * Options for the {@link CanMatchPredicate} callback.\n */\nexport interface CanMatchOptions {\n /**\n * The editor state.\n */\n state: EditorState\n}\n\n/**\n * A predicate to determine if the rule can be applied in the current editor state.\n */\nexport type CanMatchPredicate = (options: CanMatchOptions) => boolean\n\n/**\n * Options for creating an {@link AutocompleteRule}\n */\nexport interface AutocompleteRuleOptions {\n /**\n * The regular expression to match against the text before the cursor. The\n * last match before the cursor is used.\n *\n * For a slash menu, you might use `/(?<!\\S)\\/(\\S.*)?$/u`.\n * For a mention, you might use `/@\\w*$/`\n */\n regex: RegExp\n\n /**\n * A callback that is called when the rule starts to match, and also on\n * subsequent updates while the rule continues to match.\n */\n onEnter: MatchHandler\n\n /**\n * A callback that is called when the rule stops matching.\n */\n onLeave?: VoidFunction\n\n /**\n * A predicate to determine if the rule can be applied in the current editor\n * state. If not provided, it defaults to only allowing matches that are not\n * inside a code block or code mark.\n */\n canMatch?: CanMatchPredicate\n}\n\n/**\n * An autocomplete rule that can be used to create an autocomplete extension.\n *\n * @public\n */\nexport class AutocompleteRule {\n /** @internal */\n readonly regex: RegExp\n /** @internal */\n readonly onMatch: MatchHandler\n /** @internal */\n readonly onLeave?: VoidFunction\n /** @internal */\n readonly canMatch: (options: { state: EditorState }) => boolean\n\n constructor(options: AutocompleteRuleOptions) {\n this.regex = options.regex\n this.onMatch = options.onEnter\n this.onLeave = options.onLeave\n this.canMatch = options.canMatch ?? defaultCanMatch\n }\n}\n","import { OBJECT_REPLACEMENT_CHARACTER } from '@prosekit/core'\nimport type { ProseMirrorNode, ResolvedPos } from '@prosekit/pm/model'\nimport { Plugin, type EditorState, type Transaction } from '@prosekit/pm/state'\nimport type { Mapping } from '@prosekit/pm/transform'\nimport type { EditorView } from '@prosekit/pm/view'\nimport { Decoration, DecorationSet } from '@prosekit/pm/view'\n\nimport {\n getPluginState,\n getTrMeta,\n pluginKey,\n setTrMeta,\n type PredictionPluginMatching,\n type PredictionPluginState,\n type PredictionTransactionMeta,\n} from './autocomplete-helpers.ts'\nimport type { AutocompleteRule } from './autocomplete-rule.ts'\n\n/**\n * Creates a plugin that handles autocomplete functionality.\n *\n * Workflow:\n *\n * 1. {@link handleTextInput}: called when text is going to be input, but the\n * transaction is not yet created. Injects a new matching as a transaction\n * meta if applicable. This is the only place to create a new matching if\n * there is no existing matching.\n * 2. {@link handleTransaction}: called when a transaction is going to be\n * applied. Updates the plugin state based on the transaction. This step\n * determines if a matching should be created, updated or removed.\n * 3. {@link handleUpdate}: called when the editor state is updated. This is the\n * place to call `onMatch` and register `deleteMatch` and `ignoreMatch`\n * callbacks.\n * 4. {@link getDecorations}: creates the decorations for the current matching.\n */\nexport function createAutocompletePlugin({\n getRules,\n}: {\n getRules: () => AutocompleteRule[]\n}): Plugin {\n return new Plugin<PredictionPluginState>({\n key: pluginKey,\n\n state: {\n init: (): PredictionPluginState => {\n return { ignores: [], matching: null }\n },\n apply: (tr, prevValue, oldState, newState): PredictionPluginState => {\n return handleTransaction(tr, prevValue, oldState, newState, getRules)\n },\n },\n\n view: () => ({\n update: handleUpdate,\n }),\n\n props: {\n handleTextInput: (view, from, to, textAdded, getTr) => {\n const meta = handleTextInput(view, from, to, textAdded, getRules)\n if (meta) {\n const tr = getTr()\n setTrMeta(tr, meta)\n view.dispatch(tr)\n return true\n }\n return false\n },\n decorations: getDecorations,\n },\n })\n}\n\nfunction handleTextInput(\n view: EditorView,\n from: number,\n to: number,\n textAdded: string,\n getRules: () => AutocompleteRule[],\n): PredictionTransactionMeta | undefined {\n // Only handle insertions\n if (from !== to) {\n return\n }\n\n const textBackward = getTextBackward(view.state.doc.resolve(from))\n const textFull = textBackward + textAdded\n const textTo = to + textAdded.length\n const textFrom = textTo - textFull.length\n\n const pluginState = getPluginState(view.state)\n const ignores = pluginState?.ignores ?? []\n\n const currMatching = matchRule(\n view.state,\n getRules(),\n textFull,\n textFrom,\n textTo,\n ignores,\n )\n\n if (currMatching) {\n return { type: 'enter', matching: currMatching }\n }\n}\n\nfunction handleTransaction(\n tr: Transaction,\n prevValue: PredictionPluginState,\n oldState: EditorState,\n newState: EditorState,\n getRules: () => AutocompleteRule[],\n): PredictionPluginState {\n const meta = getTrMeta(tr)\n\n if (\n !meta\n && !tr.docChanged\n && oldState.selection.eq(newState.selection)\n ) {\n // No changes\n return prevValue\n }\n\n // Handle position mapping changes\n const ignoreSet = new Set<number>()\n for (const ignore of prevValue.ignores) {\n const result = tr.mapping.mapResult(ignore)\n if (!result.deletedBefore && !result.deletedAfter) {\n ignoreSet.add(result.pos)\n }\n }\n const ignores = Array.from(ignoreSet)\n\n const prevMatching = prevValue.matching && mapMatching(prevValue.matching, tr.mapping)\n\n // If there is no new matching from `handleTextInput`\n if (!meta) {\n if (!prevMatching) {\n return { matching: null, ignores }\n }\n\n const { selection } = newState\n // If the text selection is before the matching or after the matching,\n // we leave the matching\n if (selection.to < prevMatching.from || selection.from > prevMatching.to) {\n ignores.push(prevMatching.from)\n return { matching: null, ignores }\n }\n\n // Get the text between the existing matching\n const text = getTextBetween(newState.doc, prevMatching.from, prevMatching.to)\n // Check the text again to see if it still matches the rule\n const currMatching = matchRule(\n newState,\n getRules(),\n text,\n prevMatching.from,\n prevMatching.to,\n ignores,\n )\n return { matching: currMatching ?? null, ignores }\n }\n\n // If a new matching is being entered from `handleTextInput`\n if (meta.type === 'enter') {\n // Ignore the previous matching if it is not the same as the new matching\n if (prevMatching && prevMatching.from !== meta.matching.from) {\n ignores.push(prevMatching.from)\n }\n\n // Return the new matching\n return { matching: meta.matching, ignores }\n }\n\n // If a matching is being exited\n if (meta.type === 'leave') {\n if (prevMatching) {\n ignores.push(prevMatching.from)\n }\n return { matching: null, ignores }\n }\n\n throw new Error(`Invalid transaction meta: ${meta satisfies never}`)\n}\n\nfunction handleUpdate(view: EditorView, prevState: EditorState): void {\n const prevValue = getPluginState(prevState)\n const currValue = getPluginState(view.state)\n\n if (!prevValue || !currValue) {\n // Should not happen\n return\n }\n\n const prevMatching = prevValue.matching\n const currMatching = currValue.matching\n\n // Deactivate the previous rule\n if (prevMatching && prevMatching.rule !== currMatching?.rule) {\n prevMatching.rule.onLeave?.()\n }\n\n // Activate the current rule\n if (currMatching) {\n const { from, to, match, rule } = currMatching\n\n const textSnapshot = getTextBetween(view.state.doc, from, to)\n\n const deleteMatch = () => {\n if (getTextBetween(view.state.doc, from, to) === textSnapshot) {\n view.dispatch(view.state.tr.delete(from, to))\n }\n }\n\n const ignoreMatch = () => {\n view.dispatch(\n setTrMeta(view.state.tr, { type: 'leave' }),\n )\n }\n\n rule.onMatch({\n state: view.state,\n match,\n from,\n to,\n deleteMatch,\n ignoreMatch,\n })\n }\n}\n\nfunction getDecorations(state: EditorState): DecorationSet | null {\n const pluginState = getPluginState(state)\n if (pluginState?.matching) {\n const { from, to, match } = pluginState.matching\n const deco = Decoration.inline(from, to, {\n 'class': 'prosekit-autocomplete-match',\n 'data-autocomplete-match-text': match[0],\n })\n return DecorationSet.create(state.doc, [deco])\n }\n return null\n}\n\nconst MAX_MATCH = 200\n\n/** Get the text before the given position at the current block. */\nfunction getTextBackward($pos: ResolvedPos): string {\n const parentOffset: number = $pos.parentOffset\n return getTextBetween(\n $pos.parent,\n Math.max(0, parentOffset - MAX_MATCH),\n parentOffset,\n )\n}\n\nfunction getTextBetween(node: ProseMirrorNode, from: number, to: number): string {\n return node.textBetween(\n from,\n to,\n null,\n OBJECT_REPLACEMENT_CHARACTER,\n )\n}\n\nfunction matchRule(\n state: EditorState,\n rules: AutocompleteRule[],\n text: string,\n textFrom: number,\n textTo: number,\n ignores: Array<number>,\n): PredictionPluginMatching | undefined {\n // Find the rightmost ignore point within the text range\n let maxIgnore = -1\n for (const ignore of ignores) {\n if (ignore >= textFrom && ignore < textTo && ignore > maxIgnore) {\n maxIgnore = ignore\n }\n }\n\n // If an ignore point is within the text range, we ignore the text to the left\n // of the ignore point (including the character right after the ignore point).\n if (maxIgnore >= 0) {\n const cut = maxIgnore + 1 - textFrom\n text = text.slice(cut)\n textFrom += cut\n }\n\n if (textFrom >= textTo || !text) {\n return\n }\n\n for (const rule of rules) {\n if (!rule.canMatch({ state })) {\n continue\n }\n\n rule.regex.lastIndex = 0\n const match = rule.regex.exec(text)\n if (!match) {\n continue\n }\n\n const matchTo = textTo\n const matchFrom = textFrom + match.index\n\n return { rule, match, from: matchFrom, to: matchTo }\n }\n}\n\nfunction mapMatching(matching: PredictionPluginMatching, mapping: Mapping): PredictionPluginMatching {\n return {\n rule: matching.rule,\n match: matching.match,\n from: mapping.map(matching.from),\n to: mapping.map(matching.to, -1),\n }\n}\n","import { defineFacet, defineFacetPayload, pluginFacet, type Extension, type PluginPayload } from '@prosekit/core'\n\nimport { createAutocompletePlugin } from './autocomplete-plugin.ts'\nimport type { AutocompleteRule } from './autocomplete-rule.ts'\n\n/**\n * Defines an autocomplete extension that executes logic when the text before\n * the cursor matches the given regular expression.\n *\n * When a match is found, an inline decoration is applied to the matched text\n * with the class `prosekit-autocomplete-match` and a `data-autocomplete-match-text`\n * attribute containing the full matched string.\n */\nexport function defineAutocomplete(rule: AutocompleteRule): Extension {\n return defineFacetPayload(autocompleteFacet, [rule])\n}\n\nconst autocompleteFacet = defineFacet<AutocompleteRule, PluginPayload>({\n reduce: () => {\n let rules: AutocompleteRule[] = []\n const getRules = () => rules\n const plugin = createAutocompletePlugin({ getRules })\n\n return function reducer(inputs) {\n rules = inputs\n return plugin\n }\n },\n parent: pluginFacet,\n singleton: true,\n})\n"],"mappings":";;;AAKA,SAAgB,gBAAgB,EAAE,SAA0C;CAC1E,MAAM,OAAO,MAAM,UAAU;AAC7B,QAAO,CAAC,kBAAkB,KAAK,IAAI,CAAC,iBAAiB,KAAK;;AAG5D,SAAS,kBAAkB,MAA4B;AACrD,MAAK,IAAI,IAAI,KAAK,OAAO,IAAI,GAAG,IAC9B,KAAI,KAAK,KAAK,EAAE,CAAC,KAAK,KAAK,KACzB,QAAO;AAGX,QAAO;;AAGT,SAAS,iBAAiB,MAA4B;AACpD,MAAK,MAAM,QAAQ,KAAK,OAAO,CAC7B,KAAI,KAAK,KAAK,KAAK,KACjB,QAAO;AAGX,QAAO;;AAsCT,SAAgB,eAAe,OAAuD;AACpF,QAAO,UAAU,SAAS,MAAM;;AAGlC,SAAgB,UAAU,IAAwD;AAChF,QAAO,GAAG,QAAQ,UAAU;;AAG9B,SAAgB,UACd,IACA,MACa;AACb,QAAO,GAAG,QAAQ,WAAW,KAAK;;AAGpC,MAAa,YAA8C,IAAI,UAAiC,wBAAwB;;;;;;ACsBxH,IAAa,mBAAb,MAA8B;CAU5B,YAAY,SAAkC;AAC5C,OAAK,QAAQ,QAAQ;AACrB,OAAK,UAAU,QAAQ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,MAAM,QAAQ,KAAK,MAAM,KAAK,KAAK;AACnC,MAAI,CAAC,MACH;EAGF,MAAM,UAAU;AAGhB,SAAO;GAAE;GAAM;GAAO,MAFJ,WAAW,MAAM;GAEI,IAAI;GAAS;;;AAIxD,SAAS,YAAY,UAAoC,SAA4C;AACnG,QAAO;EACL,MAAM,SAAS;EACf,OAAO,SAAS;EAChB,MAAM,QAAQ,IAAI,SAAS,KAAK;EAChC,IAAI,QAAQ,IAAI,SAAS,IAAI,GAAG;EACjC;;;;;;;;;;ACjTH,SAAgB,mBAAmB,MAAmC;AACpE,QAAO,mBAAmB,mBAAmB,CAAC,KAAK,CAAC;;AAGtD,MAAM,oBAAoB,YAA6C;CACrE,cAAc;EACZ,IAAI,QAA4B,EAAE;EAClC,MAAM,iBAAiB;EACvB,MAAM,SAAS,yBAAyB,EAAE,UAAU,CAAC;AAErD,SAAO,SAAS,QAAQ,QAAQ;AAC9B,WAAQ;AACR,UAAO;;;CAGX,QAAQ;CACR,WAAW;CACZ,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"blockquote.js","names":[],"sources":["../src/blockquote/blockquote-commands.ts","../src/blockquote/blockquote-input-rule.ts","../src/blockquote/blockquote-keymap.ts","../src/blockquote/blockquote-spec.ts","../src/blockquote/blockquote.ts"],"sourcesContent":["import { defineCommands, insertNode, toggleWrap, wrap, type Extension } from '@prosekit/core'\n\nexport type BlockquoteCommandsExtension = Extension<{\n Commands: {\n setBlockquote: []\n insertBlockquote: []\n toggleBlockquote: []\n }\n}>\n\n/**\n * @internal\n */\nexport function defineBlockquoteCommands(): BlockquoteCommandsExtension {\n return defineCommands({\n setBlockquote: () => {\n return wrap({ type: 'blockquote' })\n },\n insertBlockquote: () => {\n return insertNode({ type: 'blockquote' })\n },\n toggleBlockquote: () => {\n return toggleWrap({ type: 'blockquote' })\n },\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport { defineWrappingInputRule } from '../input-rule/index.ts'\n\n/**\n * Wraps the text block in a blockquote when `>` is typed at the start of a new\n * line followed by a space.\n */\nexport function defineBlockquoteInputRule(): PlainExtension {\n return defineWrappingInputRule({\n regex: /^>\\s/,\n type: 'blockquote',\n })\n}\n","import { defineKeymap, isAtBlockStart, toggleWrap, type PlainExtension } from '@prosekit/core'\nimport { joinBackward } from '@prosekit/pm/commands'\nimport type { Command } from '@prosekit/pm/state'\n\nfunction toggleBlockquoteKeybinding(): Command {\n return toggleWrap({ type: 'blockquote' })\n}\n\nconst backspaceUnsetBlockquoteCommand: Command = (state, dispatch, view): boolean => {\n const $pos = isAtBlockStart(state, view)\n if ($pos?.node(-1).type.name === 'blockquote') {\n return joinBackward(state, dispatch, view)\n }\n return false\n}\n\nfunction backspaceUnsetBlockquote(): Command {\n return backspaceUnsetBlockquoteCommand\n}\n/**\n * @internal\n */\nexport function defineBlockquoteKeymap(): PlainExtension {\n return defineKeymap({\n 'Mod-B': toggleBlockquoteKeybinding(),\n 'Backspace': backspaceUnsetBlockquote(),\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { Attrs } from '@prosekit/pm/model'\n\nexport type BlockquoteSpecExtension = Extension<{\n Nodes: {\n blockquote: Attrs\n }\n}>\n\nexport function defineBlockquoteSpec(): BlockquoteSpecExtension {\n return defineNodeSpec({\n name: 'blockquote',\n content: 'block+',\n group: 'block',\n defining: true,\n parseDOM: [{ tag: 'blockquote' }],\n toDOM() {\n return ['blockquote', 0]\n },\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineBlockquoteCommands, type BlockquoteCommandsExtension } from './blockquote-commands.ts'\nimport { defineBlockquoteInputRule } from './blockquote-input-rule.ts'\nimport { defineBlockquoteKeymap } from './blockquote-keymap.ts'\nimport { defineBlockquoteSpec, type BlockquoteSpecExtension } from './blockquote-spec.ts'\n\n/**\n * @internal\n */\nexport type BlockquoteExtension = Union<\n [BlockquoteSpecExtension, BlockquoteCommandsExtension]\n>\n\n/**\n * @public\n */\nexport function defineBlockquote(): BlockquoteExtension {\n return union(\n defineBlockquoteSpec(),\n defineBlockquoteInputRule(),\n defineBlockquoteCommands(),\n defineBlockquoteKeymap(),\n )\n}\n"],"mappings":";;;;;;AAaA,SAAgB,2BAAwD;AACtE,QAAO,eAAe;EACpB,qBAAqB;AACnB,UAAO,KAAK,EAAE,MAAM,cAAc,CAAC;;EAErC,wBAAwB;AACtB,UAAO,WAAW,EAAE,MAAM,cAAc,CAAC;;EAE3C,wBAAwB;AACtB,UAAO,WAAW,EAAE,MAAM,cAAc,CAAC;;EAE5C,CAAC;;;;;;AChBJ,SAAgB,4BAA4C;AAC1D,QAAO,wBAAwB;EAC7B,OAAO;EACP,MAAM;EACP,CAAC;;ACRJ,SAAS,6BAAsC;AAC7C,QAAO,WAAW,EAAE,MAAM,cAAc,CAAC;;AAG3C,MAAM,mCAA4C,OAAO,UAAU,SAAkB;AAEnF,KADa,eAAe,OAAO,KAAK,EAC9B,KAAK,GAAG,CAAC,KAAK,SAAS,aAC/B,QAAO,aAAa,OAAO,UAAU,KAAK;AAE5C,QAAO;;AAGT,SAAS,2BAAoC;AAC3C,QAAO;;;;;AAKT,SAAgB,yBAAyC;AACvD,QAAO,aAAa;EAClB,SAAS,4BAA4B;EACrC,aAAa,0BAA0B;EACxC,CAAC;;ACjBJ,SAAgB,uBAAgD;AAC9D,QAAO,eAAe;EACpB,MAAM;EACN,SAAS;EACT,OAAO;EACP,UAAU;EACV,UAAU,CAAC,EAAE,KAAK,cAAc,CAAC;EACjC,QAAQ;AACN,UAAO,CAAC,cAAc,EAAE;;EAE3B,CAAC;;;;;ACFJ,SAAgB,mBAAwC;AACtD,QAAO,MACL,sBAAsB,EACtB,2BAA2B,EAC3B,0BAA0B,EAC1B,wBAAwB,CACzB"}
1
+ {"version":3,"file":"blockquote.js","names":[],"sources":["../src/blockquote/blockquote-commands.ts","../src/blockquote/blockquote-input-rule.ts","../src/blockquote/blockquote-keymap.ts","../src/blockquote/blockquote-spec.ts","../src/blockquote/blockquote.ts"],"sourcesContent":["import { defineCommands, insertNode, toggleWrap, wrap, type Extension } from '@prosekit/core'\n\nexport type BlockquoteCommandsExtension = Extension<{\n Commands: {\n setBlockquote: []\n insertBlockquote: []\n toggleBlockquote: []\n }\n}>\n\n/**\n * @internal\n */\nexport function defineBlockquoteCommands(): BlockquoteCommandsExtension {\n return defineCommands({\n setBlockquote: () => {\n return wrap({ type: 'blockquote' })\n },\n insertBlockquote: () => {\n return insertNode({ type: 'blockquote' })\n },\n toggleBlockquote: () => {\n return toggleWrap({ type: 'blockquote' })\n },\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport { defineWrappingInputRule } from '../input-rule/index.ts'\n\n/**\n * Wraps the text block in a blockquote when `>` is typed at the start of a new\n * line followed by a space.\n */\nexport function defineBlockquoteInputRule(): PlainExtension {\n return defineWrappingInputRule({\n regex: /^>\\s/,\n type: 'blockquote',\n })\n}\n","import { defineKeymap, isAtBlockStart, toggleWrap, type PlainExtension } from '@prosekit/core'\nimport { joinBackward } from '@prosekit/pm/commands'\nimport type { Command } from '@prosekit/pm/state'\n\nfunction toggleBlockquoteKeybinding(): Command {\n return toggleWrap({ type: 'blockquote' })\n}\n\nconst backspaceUnsetBlockquoteCommand: Command = (state, dispatch, view): boolean => {\n const $pos = isAtBlockStart(state, view)\n if ($pos?.node(-1).type.name === 'blockquote') {\n return joinBackward(state, dispatch, view)\n }\n return false\n}\n\nfunction backspaceUnsetBlockquote(): Command {\n return backspaceUnsetBlockquoteCommand\n}\n/**\n * @internal\n */\nexport function defineBlockquoteKeymap(): PlainExtension {\n return defineKeymap({\n 'Mod-B': toggleBlockquoteKeybinding(),\n 'Backspace': backspaceUnsetBlockquote(),\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { Attrs } from '@prosekit/pm/model'\n\nexport type BlockquoteSpecExtension = Extension<{\n Nodes: {\n blockquote: Attrs\n }\n}>\n\nexport function defineBlockquoteSpec(): BlockquoteSpecExtension {\n return defineNodeSpec({\n name: 'blockquote',\n content: 'block+',\n group: 'block',\n defining: true,\n parseDOM: [{ tag: 'blockquote' }],\n toDOM() {\n return ['blockquote', 0]\n },\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineBlockquoteCommands, type BlockquoteCommandsExtension } from './blockquote-commands.ts'\nimport { defineBlockquoteInputRule } from './blockquote-input-rule.ts'\nimport { defineBlockquoteKeymap } from './blockquote-keymap.ts'\nimport { defineBlockquoteSpec, type BlockquoteSpecExtension } from './blockquote-spec.ts'\n\n/**\n * @internal\n */\nexport type BlockquoteExtension = Union<\n [BlockquoteSpecExtension, BlockquoteCommandsExtension]\n>\n\n/**\n * @public\n */\nexport function defineBlockquote(): BlockquoteExtension {\n return union(\n defineBlockquoteSpec(),\n defineBlockquoteInputRule(),\n defineBlockquoteCommands(),\n defineBlockquoteKeymap(),\n )\n}\n"],"mappings":";;;;;;AAaA,SAAgB,2BAAwD;AACtE,QAAO,eAAe;EACpB,qBAAqB;AACnB,UAAO,KAAK,EAAE,MAAM,cAAc,CAAC;;EAErC,wBAAwB;AACtB,UAAO,WAAW,EAAE,MAAM,cAAc,CAAC;;EAE3C,wBAAwB;AACtB,UAAO,WAAW,EAAE,MAAM,cAAc,CAAC;;EAE5C,CAAC;;;;;;AChBJ,SAAgB,4BAA4C;AAC1D,QAAO,wBAAwB;EAC7B,OAAO;EACP,MAAM;EACP,CAAC;;ACRJ,SAAS,6BAAsC;AAC7C,QAAO,WAAW,EAAE,MAAM,cAAc,CAAC;;AAG3C,MAAM,mCAA4C,OAAO,UAAU,SAAkB;AAEnF,KADa,eAAe,OAAO,KAC3B,EAAE,KAAK,GAAG,CAAC,KAAK,SAAS,aAC/B,QAAO,aAAa,OAAO,UAAU,KAAK;AAE5C,QAAO;;AAGT,SAAS,2BAAoC;AAC3C,QAAO;;;;;AAKT,SAAgB,yBAAyC;AACvD,QAAO,aAAa;EAClB,SAAS,4BAA4B;EACrC,aAAa,0BAA0B;EACxC,CAAC;;ACjBJ,SAAgB,uBAAgD;AAC9D,QAAO,eAAe;EACpB,MAAM;EACN,SAAS;EACT,OAAO;EACP,UAAU;EACV,UAAU,CAAC,EAAE,KAAK,cAAc,CAAC;EACjC,QAAQ;AACN,UAAO,CAAC,cAAc,EAAE;;EAE3B,CAAC;;;;;ACFJ,SAAgB,mBAAwC;AACtD,QAAO,MACL,sBAAsB,EACtB,2BAA2B,EAC3B,0BAA0B,EAC1B,wBAAwB,CACzB"}
@@ -1 +1 @@
1
- {"version":3,"file":"code-block.js","names":[],"sources":["../src/code-block/code-block-commands.ts","../src/code-block/code-block-highlight.ts","../src/code-block/code-block-input-rule.ts","../src/code-block/code-block-keymap.ts","../src/code-block/shiki-highlighter.ts","../src/code-block/shiki-parser.ts","../src/code-block/code-block-shiki.ts","../src/code-block/code-block-spec.ts","../src/code-block/code-block.ts"],"sourcesContent":["import { defineCommands, insertNode, setBlockType, setNodeAttrs, toggleNode, type Extension } from '@prosekit/core'\n\nimport type { CodeBlockAttrs } from './code-block-types.ts'\n\n/**\n * @internal\n */\nexport type CodeBlockCommandsExtension = Extension<{\n Commands: {\n setCodeBlock: [attrs?: CodeBlockAttrs]\n insertCodeBlock: [attrs?: CodeBlockAttrs]\n toggleCodeBlock: [attrs?: CodeBlockAttrs]\n setCodeBlockAttrs: [attrs: CodeBlockAttrs]\n }\n}>\n\n/**\n * Adds commands for working with `codeBlock` nodes.\n *\n * @public\n */\nexport function defineCodeBlockCommands(): CodeBlockCommandsExtension {\n return defineCommands({\n setCodeBlock: (attrs?: CodeBlockAttrs) => {\n return setBlockType({ type: 'codeBlock', attrs })\n },\n insertCodeBlock: (attrs?: CodeBlockAttrs) => {\n return insertNode({ type: 'codeBlock', attrs })\n },\n toggleCodeBlock: (attrs?: CodeBlockAttrs) => {\n return toggleNode({ type: 'codeBlock', attrs })\n },\n setCodeBlockAttrs: (attrs: CodeBlockAttrs) => {\n return setNodeAttrs({ type: 'codeBlock', attrs })\n },\n })\n}\n","import { definePlugin, type Extension } from '@prosekit/core'\nimport { createHighlightPlugin, type Parser } from 'prosemirror-highlight'\n\n/**\n * @public\n *\n * An alias for the `Parser` type from the `prosemirror-highlight` package.\n */\nexport type HighlightParser = Parser\n\n/**\n * @public\n */\nexport type CodeBlockHighlightOptions = {\n /**\n * A parser instance from the `prosemirror-highlight` package.\n */\n parser: HighlightParser\n /**\n * ProseMirror node types to highlight.\n *\n * @default ['codeBlock', 'mathBlock']\n */\n nodeTypes?: string[]\n}\n\n/**\n * Adds syntax highlighting to code blocks. This function requires a `Parser`\n * instance from the `prosemirror-highlight` package. See the\n * [documentation](https://github.com/ocavue/prosemirror-highlight) for more\n * information.\n *\n * @param options\n *\n * @public\n */\nexport function defineCodeBlockHighlight({\n parser,\n nodeTypes = ['codeBlock', 'mathBlock'],\n}: CodeBlockHighlightOptions): Extension {\n return definePlugin(\n createHighlightPlugin({ parser, nodeTypes }),\n )\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport { defineTextBlockEnterRule } from '../enter-rule/index.ts'\nimport { defineTextBlockInputRule } from '../input-rule/index.ts'\n\nimport type { CodeBlockAttrs } from './code-block-types.ts'\n\n/**\n * Adds input rules for `codeBlock` nodes.\n *\n * @public\n */\nexport function defineCodeBlockInputRule(): PlainExtension {\n return defineTextBlockInputRule({\n regex: /^```(\\S*)\\s$/,\n type: 'codeBlock',\n attrs: getAttrs,\n })\n}\n\n/**\n * Adds enter rules for `codeBlock` nodes.\n *\n * @public\n */\nexport function defineCodeBlockEnterRule(): PlainExtension {\n return defineTextBlockEnterRule({\n regex: /^```(\\S*)$/,\n type: 'codeBlock',\n attrs: getAttrs,\n })\n}\n\nfunction getAttrs(match: RegExpMatchArray): CodeBlockAttrs {\n return { language: match[1] || '' }\n}\n","import { defaultBlockAt, defineKeymap, type PlainExtension } from '@prosekit/core'\nimport { TextSelection, type Command } from '@prosekit/pm/state'\n\n/**\n * Defines the keymap for code blocks.\n */\nexport function defineCodeBlockKeymap(): PlainExtension {\n return defineKeymap({\n Enter: existCodeBlock,\n })\n}\n\n/**\n * Exit a code block and insert a default block below if the cursor is at the\n * end of the code block and the code block is ended with two new lines.\n */\nconst existCodeBlock: Command = (state, dispatch) => {\n if (!state.selection.empty) {\n return false\n }\n\n const { $head } = state.selection\n const parent = $head.parent\n if (\n parent.isTextblock\n && parent.type.spec.code\n && $head.parentOffset === parent.content.size\n && parent.textContent.endsWith('\\n\\n')\n ) {\n const grandParent = $head.node(-1)\n const insertIndex = $head.indexAfter(-1)\n const type = defaultBlockAt(grandParent.contentMatchAt(insertIndex))\n\n if (!type || !grandParent.canReplaceWith(insertIndex, insertIndex, type)) {\n return false\n }\n\n if (dispatch) {\n const { tr } = state\n tr.delete($head.pos - 2, $head.pos)\n const pos = tr.selection.$head.after()\n const node = type.createAndFill()\n if (node) {\n tr.replaceWith(pos, pos, node)\n tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1))\n dispatch(tr.scrollIntoView())\n }\n }\n\n return true\n }\n\n return false\n}\n","import type { HighlighterOptions, HighlighterResult } from './shiki-highlighter-chunk.ts'\n\nlet loaded: ((options: HighlighterOptions) => HighlighterResult) | undefined\n\nasync function load() {\n const { createOrGetHighlighter } = await import('./shiki-highlighter-chunk.ts')\n loaded = createOrGetHighlighter\n}\n\nexport function createOrGetHighlighter(\n options: HighlighterOptions,\n): HighlighterResult {\n if (!loaded) {\n return { promise: load() }\n }\n return loaded(options)\n}\n\nexport type { HighlighterOptions }\n","import type { Parser } from 'prosemirror-highlight'\nimport { createParser } from 'prosemirror-highlight/shiki'\n\nimport type { ShikiBundledLanguage } from './shiki-bundle.ts'\nimport { createOrGetHighlighter, type HighlighterOptions } from './shiki-highlighter.ts'\n\n/**\n * @internal\n */\nexport function createLazyParser(\n highlighterOptions: HighlighterOptions,\n): Parser {\n let parser: Parser | undefined\n\n return function lazyParser(options) {\n const language = (options.language || '') as ShikiBundledLanguage\n const { highlighter, promise } = createOrGetHighlighter({\n ...highlighterOptions,\n langs: [language],\n })\n\n if (!highlighter) {\n return promise\n }\n\n if (!parser) {\n parser = createParser(highlighter, {\n theme: highlighterOptions.themes[0],\n })\n }\n return parser(options)\n }\n}\n","import type { Extension } from '@prosekit/core'\nimport type { SpecialLanguage } from 'shiki'\n\nimport { defineCodeBlockHighlight } from './code-block-highlight.ts'\nimport type { ShikiBundledLanguage, ShikiBundledTheme } from './shiki-bundle.ts'\nimport type { ShikiHighlighterOptions } from './shiki-highlighter-chunk.ts'\nimport { createLazyParser } from './shiki-parser.ts'\n\n/**\n * The options to configure the Shiki highlighter.\n *\n * @public\n */\nexport interface CodeBlockShikiOptions extends Omit<ShikiHighlighterOptions, 'themes' | 'langs' | 'engine'> {\n /**\n * ProseMirror node types to highlight.\n *\n * @default ['codeBlock', 'mathBlock']\n */\n nodeTypes?: string[]\n\n /**\n * A list of Shiki themes to pre-load. The first theme in the list will be\n * used to render the code block.\n *\n * @default ['one-dark-pro']\n */\n themes?: ShikiBundledTheme[]\n\n /**\n * A list of Shiki languages to pre-load.\n *\n * @default ['text']\n */\n langs?: (ShikiBundledLanguage | SpecialLanguage)[]\n\n /**\n * The RegExp engine to use. By default, the JavaScript engine is used.\n */\n engine?: ShikiHighlighterOptions['engine']\n}\n\n/**\n * Adds syntax highlighting to code blocks using the [Shiki](https://github.com/shikijs/shiki) package.\n *\n * It will set two CSS variables on the code block elements:\n *\n * - `--prosemirror-highlight`: sets text color\n * - `--prosemirror-highlight-bg`: sets background color\n *\n * @param options - The options to configure the Shiki highlighter.\n *\n * @public\n */\nexport function defineCodeBlockShiki({\n nodeTypes,\n themes = ['one-dark-pro'],\n langs = ['text'],\n ...rest\n}: CodeBlockShikiOptions = {}): Extension {\n const parser = createLazyParser({ themes, langs, ...rest })\n return defineCodeBlockHighlight({ parser, nodeTypes })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\n\nimport type { CodeBlockAttrs } from './code-block-types.ts'\n\n/**\n * @internal\n */\nexport type CodeBlockSpecExtension = Extension<{\n Nodes: {\n codeBlock: CodeBlockAttrs\n }\n}>\n\n/**\n * Defines the `codeBlock` node spec.\n *\n * @public\n */\nexport function defineCodeBlockSpec(): CodeBlockSpecExtension {\n return defineNodeSpec({\n name: 'codeBlock',\n content: 'text*',\n group: 'block',\n code: true,\n defining: true,\n marks: '',\n attrs: { language: { default: '', validate: 'string' } },\n parseDOM: [\n {\n tag: 'pre',\n preserveWhitespace: 'full',\n getAttrs: (node): CodeBlockAttrs => {\n const language = extractLanguageFromElement(node)\n || extractLanguageFromElement(node.querySelector('code'))\n return { language }\n },\n },\n ],\n toDOM(node) {\n const { language } = node.attrs as CodeBlockAttrs\n return [\n 'pre',\n { 'data-language': language || undefined },\n // `class: language-${language}` is used by remark-rehype to highlight the code block\n ['code', { class: language ? `language-${language}` : undefined }, 0],\n ]\n },\n })\n}\n\nfunction extractLanguageFromElement(element: HTMLElement | null | undefined): string {\n if (!element) {\n return ''\n }\n\n const attr = element.getAttribute('data-language')\n if (attr) {\n return attr\n }\n\n const className = element.className\n const match = className.match(/language-(\\w+)/)\n if (match) {\n return match[1]\n }\n\n return ''\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineCodeBlockCommands, type CodeBlockCommandsExtension } from './code-block-commands.ts'\nimport { defineCodeBlockEnterRule, defineCodeBlockInputRule } from './code-block-input-rule.ts'\nimport { defineCodeBlockKeymap } from './code-block-keymap.ts'\nimport { defineCodeBlockSpec, type CodeBlockSpecExtension } from './code-block-spec.ts'\n\n/**\n * @internal\n */\nexport type CodeBlockExtension = Union<\n [CodeBlockSpecExtension, CodeBlockCommandsExtension]\n>\n\n/**\n * Adds `codeBlock` nodes to the editor. This includes the following extensions:\n *\n * - {@link defineCodeBlockSpec}\n * - {@link defineCodeBlockInputRule}\n * - {@link defineCodeBlockEnterRule}\n * - {@link defineCodeBlockKeymap}\n * - {@link defineCodeBlockCommands}.\n *\n * @public\n */\nexport function defineCodeBlock(): CodeBlockExtension {\n return union(\n defineCodeBlockSpec(),\n defineCodeBlockInputRule(),\n defineCodeBlock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e;EACpB,MAAM;EACN,SAAS;EACT,OAAO;EACP,MAAM;EACN,UAAU;EACV,OAAO;EACP,OAAO,EAAE,UAAU;GAAE,SAAS;GAAI,UAAU;GAAU,EAAE;EACxD,UAAU,CACR;GACE,KAAK;GACL,oBAAoB;GACpB,WAAW,SAAyB;AAGlC,WAAO,EAAE,UAFQ,2BAA2B,KAAK,IAC5C,2BAA2B,KAAK,cAAc,OAAO,CAAC,EACxC;;GAEtB,CACF;EACD,MAAM,MAAM;GACV,MAAM,EAAE,aAAa,KAAK;AAC1B,UAAO;IACL;IACA,EAAE,iBAAiB,YAAY,KAAA,GAAW;IAE1C;KAAC;KAAQ,EAAE,OAAO,WAAW,YAAY,aAAa,KAAA,GAAW;KAAE;KAAE;IACtE;;EAEJ,CAAC;;AAGJ,SAAS,2BAA2B,SAAiD;AACnF,KAAI,CAAC,QACH,QAAO;CAGT,MAAM,OAAO,QAAQ,aAAa,gBAAgB;AAClD,KAAI,KACF,QAAO;CAIT,MAAM,QADY,QAAQ,UACF,MAAM,iBAAiB;AAC/C,KAAI,MACF,QAAO,MAAM;AAGf,QAAO;;;;;;;;;;;;;ACzCT,SAAgB,kBAAsC;AACpD,QAAO,MACL,qBAAqB,EACrB,0BAA0B,EAC1B,0BAA0B,EAC1B,uBAAuB,EACvB,yBAAyB,CAC1B"}
1
+ {"version":3,"file":"code-block.js","names":[],"sources":["../src/code-block/code-block-commands.ts","../src/code-block/code-block-highlight.ts","../src/code-block/code-block-input-rule.ts","../src/code-block/code-block-keymap.ts","../src/code-block/shiki-highlighter.ts","../src/code-block/shiki-parser.ts","../src/code-block/code-block-shiki.ts","../src/code-block/code-block-spec.ts","../src/code-block/code-block.ts"],"sourcesContent":["import { defineCommands, insertNode, setBlockType, setNodeAttrs, toggleNode, type Extension } from '@prosekit/core'\n\nimport type { CodeBlockAttrs } from './code-block-types.ts'\n\n/**\n * @internal\n */\nexport type CodeBlockCommandsExtension = Extension<{\n Commands: {\n setCodeBlock: [attrs?: CodeBlockAttrs]\n insertCodeBlock: [attrs?: CodeBlockAttrs]\n toggleCodeBlock: [attrs?: CodeBlockAttrs]\n setCodeBlockAttrs: [attrs: CodeBlockAttrs]\n }\n}>\n\n/**\n * Adds commands for working with `codeBlock` nodes.\n *\n * @public\n */\nexport function defineCodeBlockCommands(): CodeBlockCommandsExtension {\n return defineCommands({\n setCodeBlock: (attrs?: CodeBlockAttrs) => {\n return setBlockType({ type: 'codeBlock', attrs })\n },\n insertCodeBlock: (attrs?: CodeBlockAttrs) => {\n return insertNode({ type: 'codeBlock', attrs })\n },\n toggleCodeBlock: (attrs?: CodeBlockAttrs) => {\n return toggleNode({ type: 'codeBlock', attrs })\n },\n setCodeBlockAttrs: (attrs: CodeBlockAttrs) => {\n return setNodeAttrs({ type: 'codeBlock', attrs })\n },\n })\n}\n","import { definePlugin, type Extension } from '@prosekit/core'\nimport { createHighlightPlugin, type Parser } from 'prosemirror-highlight'\n\n/**\n * @public\n *\n * An alias for the `Parser` type from the `prosemirror-highlight` package.\n */\nexport type HighlightParser = Parser\n\n/**\n * @public\n */\nexport type CodeBlockHighlightOptions = {\n /**\n * A parser instance from the `prosemirror-highlight` package.\n */\n parser: HighlightParser\n /**\n * ProseMirror node types to highlight.\n *\n * @default ['codeBlock', 'mathBlock']\n */\n nodeTypes?: string[]\n}\n\n/**\n * Adds syntax highlighting to code blocks. This function requires a `Parser`\n * instance from the `prosemirror-highlight` package. See the\n * [documentation](https://github.com/ocavue/prosemirror-highlight) for more\n * information.\n *\n * @param options\n *\n * @public\n */\nexport function defineCodeBlockHighlight({\n parser,\n nodeTypes = ['codeBlock', 'mathBlock'],\n}: CodeBlockHighlightOptions): Extension {\n return definePlugin(\n createHighlightPlugin({ parser, nodeTypes }),\n )\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport { defineTextBlockEnterRule } from '../enter-rule/index.ts'\nimport { defineTextBlockInputRule } from '../input-rule/index.ts'\n\nimport type { CodeBlockAttrs } from './code-block-types.ts'\n\n/**\n * Adds input rules for `codeBlock` nodes.\n *\n * @public\n */\nexport function defineCodeBlockInputRule(): PlainExtension {\n return defineTextBlockInputRule({\n regex: /^```(\\S*)\\s$/,\n type: 'codeBlock',\n attrs: getAttrs,\n })\n}\n\n/**\n * Adds enter rules for `codeBlock` nodes.\n *\n * @public\n */\nexport function defineCodeBlockEnterRule(): PlainExtension {\n return defineTextBlockEnterRule({\n regex: /^```(\\S*)$/,\n type: 'codeBlock',\n attrs: getAttrs,\n })\n}\n\nfunction getAttrs(match: RegExpMatchArray): CodeBlockAttrs {\n return { language: match[1] || '' }\n}\n","import { defaultBlockAt, defineKeymap, type PlainExtension } from '@prosekit/core'\nimport { TextSelection, type Command } from '@prosekit/pm/state'\n\n/**\n * Defines the keymap for code blocks.\n */\nexport function defineCodeBlockKeymap(): PlainExtension {\n return defineKeymap({\n Enter: existCodeBlock,\n })\n}\n\n/**\n * Exit a code block and insert a default block below if the cursor is at the\n * end of the code block and the code block is ended with two new lines.\n */\nconst existCodeBlock: Command = (state, dispatch) => {\n if (!state.selection.empty) {\n return false\n }\n\n const { $head } = state.selection\n const parent = $head.parent\n if (\n parent.isTextblock\n && parent.type.spec.code\n && $head.parentOffset === parent.content.size\n && parent.textContent.endsWith('\\n\\n')\n ) {\n const grandParent = $head.node(-1)\n const insertIndex = $head.indexAfter(-1)\n const type = defaultBlockAt(grandParent.contentMatchAt(insertIndex))\n\n if (!type || !grandParent.canReplaceWith(insertIndex, insertIndex, type)) {\n return false\n }\n\n if (dispatch) {\n const { tr } = state\n tr.delete($head.pos - 2, $head.pos)\n const pos = tr.selection.$head.after()\n const node = type.createAndFill()\n if (node) {\n tr.replaceWith(pos, pos, node)\n tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1))\n dispatch(tr.scrollIntoView())\n }\n }\n\n return true\n }\n\n return false\n}\n","import type { HighlighterOptions, HighlighterResult } from './shiki-highlighter-chunk.ts'\n\nlet loaded: ((options: HighlighterOptions) => HighlighterResult) | undefined\n\nasync function load() {\n const { createOrGetHighlighter } = await import('./shiki-highlighter-chunk.ts')\n loaded = createOrGetHighlighter\n}\n\nexport function createOrGetHighlighter(\n options: HighlighterOptions,\n): HighlighterResult {\n if (!loaded) {\n return { promise: load() }\n }\n return loaded(options)\n}\n\nexport type { HighlighterOptions }\n","import type { Parser } from 'prosemirror-highlight'\nimport { createParser } from 'prosemirror-highlight/shiki'\n\nimport type { ShikiBundledLanguage } from './shiki-bundle.ts'\nimport { createOrGetHighlighter, type HighlighterOptions } from './shiki-highlighter.ts'\n\n/**\n * @internal\n */\nexport function createLazyParser(\n highlighterOptions: HighlighterOptions,\n): Parser {\n let parser: Parser | undefined\n\n return function lazyParser(options) {\n const language = (options.language || '') as ShikiBundledLanguage\n const { highlighter, promise } = createOrGetHighlighter({\n ...highlighterOptions,\n langs: [language],\n })\n\n if (!highlighter) {\n return promise\n }\n\n if (!parser) {\n parser = createParser(highlighter, {\n theme: highlighterOptions.themes[0],\n })\n }\n return parser(options)\n }\n}\n","import type { Extension } from '@prosekit/core'\nimport type { SpecialLanguage } from 'shiki'\n\nimport { defineCodeBlockHighlight } from './code-block-highlight.ts'\nimport type { ShikiBundledLanguage, ShikiBundledTheme } from './shiki-bundle.ts'\nimport type { ShikiHighlighterOptions } from './shiki-highlighter-chunk.ts'\nimport { createLazyParser } from './shiki-parser.ts'\n\n/**\n * The options to configure the Shiki highlighter.\n *\n * @public\n */\nexport interface CodeBlockShikiOptions extends Omit<ShikiHighlighterOptions, 'themes' | 'langs' | 'engine'> {\n /**\n * ProseMirror node types to highlight.\n *\n * @default ['codeBlock', 'mathBlock']\n */\n nodeTypes?: string[]\n\n /**\n * A list of Shiki themes to pre-load. The first theme in the list will be\n * used to render the code block.\n *\n * @default ['one-dark-pro']\n */\n themes?: ShikiBundledTheme[]\n\n /**\n * A list of Shiki languages to pre-load.\n *\n * @default ['text']\n */\n langs?: (ShikiBundledLanguage | SpecialLanguage)[]\n\n /**\n * The RegExp engine to use. By default, the JavaScript engine is used.\n */\n engine?: ShikiHighlighterOptions['engine']\n}\n\n/**\n * Adds syntax highlighting to code blocks using the [Shiki](https://github.com/shikijs/shiki) package.\n *\n * It will set two CSS variables on the code block elements:\n *\n * - `--prosemirror-highlight`: sets text color\n * - `--prosemirror-highlight-bg`: sets background color\n *\n * @param options - The options to configure the Shiki highlighter.\n *\n * @public\n */\nexport function defineCodeBlockShiki({\n nodeTypes,\n themes = ['one-dark-pro'],\n langs = ['text'],\n ...rest\n}: CodeBlockShikiOptions = {}): Extension {\n const parser = createLazyParser({ themes, langs, ...rest })\n return defineCodeBlockHighlight({ parser, nodeTypes })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\n\nimport type { CodeBlockAttrs } from './code-block-types.ts'\n\n/**\n * @internal\n */\nexport type CodeBlockSpecExtension = Extension<{\n Nodes: {\n codeBlock: CodeBlockAttrs\n }\n}>\n\n/**\n * Defines the `codeBlock` node spec.\n *\n * @public\n */\nexport function defineCodeBlockSpec(): CodeBlockSpecExtension {\n return defineNodeSpec({\n name: 'codeBlock',\n content: 'text*',\n group: 'block',\n code: true,\n defining: true,\n marks: '',\n attrs: { language: { default: '', validate: 'string' } },\n parseDOM: [\n {\n tag: 'pre',\n preserveWhitespace: 'full',\n getAttrs: (node): CodeBlockAttrs => {\n const language = extractLanguageFromElement(node)\n || extractLanguageFromElement(node.querySelector('code'))\n return { language }\n },\n },\n ],\n toDOM(node) {\n const { language } = node.attrs as CodeBlockAttrs\n return [\n 'pre',\n { 'data-language': language || undefined },\n // `class: language-${language}` is used by remark-rehype to highlight the code block\n ['code', { class: language ? `language-${language}` : undefined }, 0],\n ]\n },\n })\n}\n\nfunction extractLanguageFromElement(element: HTMLElement | null | undefined): string {\n if (!element) {\n return ''\n }\n\n const attr = element.getAttribute('data-language')\n if (attr) {\n return attr\n }\n\n const className = element.className\n const match = className.match(/language-(\\w+)/)\n if (match) {\n return match[1]\n }\n\n return ''\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineCodeBlockCommands, type CodeBlockCommandsExtension } from './code-block-commands.ts'\nimport { defineCodeBlockEnterRule, defineCodeBlockInputRule } from './code-block-input-rule.ts'\nimport { defineCodeBlockKeymap } from './code-block-keymap.ts'\nimport { defineCodeBlockSpec, type CodeBlockSpecExtension } from './code-block-spec.ts'\n\n/**\n * @internal\n */\nexport type CodeBlockExtension = Union<\n [CodeBlockSpecExtension, CodeBlockCommandsExtension]\n>\n\n/**\n * Adds `codeBlock` nodes to the editor. This includes the following extensions:\n *\n * - {@link defineCodeBlockSpec}\n * - {@link defineCodeBlockInputRule}\n * - {@link defineCodeBlockEnterRule}\n * - {@link defineCodeBlockKeymap}\n * - {@link defineCodeBlockCommands}.\n *\n * @public\n */\nexport function defineCodeBlock(): CodeBlockExtension {\n return union(\n defineCodeBlockSpec(),\n defineCodeBlockInputRule(),\n defineCodeBlockEnterRule(),\n defineCodeBlockKeymap(),\n defineCodeBlockCommands(),\n )\n}\n"],"mappings":";;;;;;;;;;;;AAqBA,SAAgB,0BAAsD;AACpE,QAAO,eAAe;EACpB,eAAe,UAA2B;AACxC,UAAO,aAAa;IAAE,MAAM;IAAa;IAAO,CAAC;;EAEnD,kBAAkB,UAA2B;AAC3C,UAAO,WAAW;IAAE,MAAM;IAAa;IAAO,CAAC;;EAEjD,kBAAkB,UAA2B;AAC3C,UAAO,WAAW;IAAE,MAAM;IAAa;IAAO,CAAC;;EAEjD,oBAAoB,UAA0B;AAC5C,UAAO,aAAa;IAAE,MAAM;IAAa;IAAO,CAAC;;EAEpD,CAAC;;;;;;;;;;;;ACCJ,SAAgB,yBAAyB,EACvC,QACA,YAAY,CAAC,aAAa,YAAY,IACC;AACvC,QAAO,aACL,sBAAsB;EAAE;EAAQ;EAAW,CAAC,CAC7C;;;;;;;AC9BH,SAAgB,2BAA2C;AACzD,QAAO,yBAAyB;EAC9B,OAAO;EACP,MAAM;EACN,OAAO;EACR,CAAC;;;;;;;AAQJ,SAAgB,2BAA2C;AACzD,QAAO,yBAAyB;E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;;ACzCT,SAAgB,kBAAsC;AACpD,QAAO,MACL,qBAAqB,EACrB,0BAA0B,EAC1B,0BAA0B,EAC1B,uBAAuB,EACvB,yBAAyB,CAC1B"}
@@ -1 +1 @@
1
- {"version":3,"file":"commit.js","names":[],"sources":["../src/commit/index.ts"],"sourcesContent":["import { defineDefaultState, definePlugin, jsonFromNode, union, type NodeJSON, type PlainExtension, type StepJSON } from '@prosekit/core'\nimport { DOMSerializer, Fragment, Slice, type ProseMirrorNode } from '@prosekit/pm/model'\nimport { PluginKey, ProseMirrorPlugin, type Transaction } from '@prosekit/pm/state'\nimport { Step } from '@prosekit/pm/transform'\nimport { Decoration, DecorationSet, type EditorView } from '@prosekit/pm/view'\nimport { ChangeSet, type Change } from 'prosemirror-changeset'\n\n/**\n * A JSON representation of a commit.\n */\ninterface Commit {\n /**\n * The current doc node in the JSON format\n */\n doc: NodeJSON\n /**\n * The parent node in the JSON format\n */\n parent: NodeJSON\n /**\n * An array of steps in the JSON format that transform the parent node to the\n * current doc node.\n */\n steps: StepJSON[]\n}\n\nfunction getChanges(\n doc: ProseMirrorNode,\n parent: ProseMirrorNode,\n steps: Step[],\n): readonly Change[] {\n const initSet = ChangeSet.create(parent)\n const currSet = initSet.addSteps(\n doc,\n steps.map((step) => step.getMap()),\n null,\n )\n return currSet.changes\n}\n\nfunction renderDivWeight(view: EditorView): HTMLElement {\n const document = view.dom.ownerDocument\n return document.createElement('div')\n}\n\nfunction decorateDeletionSlice(\n slice: Slice,\n): Array<(view: EditorView) => HTMLElement> {\n // Get the fragment of the deleted content\n let { openStart, openEnd, content } = slice\n\n while (openStart > 0 && openEnd > 0 && content.childCount === 1) {\n openStart--\n openEnd--\n content = content.child(0).content\n }\n\n // Nothing to render\n if (content.childCount === 0) {\n return []\n }\n\n // For example, if the slice is\n // {\n // openStart: 1,\n // openEnd: 1,\n // content: <p>Hello</p><p>World</p>\n // }\n // We should render the following decorations:\n // <span>Hello</span>\n // <div></div>\n // <span>World</span>\n if (openStart > 0 && openEnd > 0 && content.childCount === 2) {\n const head = Fragment.from([content.child(0)])\n const tail = Fragment.from([content.child(1)])\n return [\n ...decorateDeletionSlice(new Slice(head, openStart, openStart)),\n renderDivWeight,\n ...decorateDeletionSlice(new Slice(tail, openEnd, openEnd)),\n ]\n }\n\n // For example, if the slice is\n // {\n // openStart: 1,\n // openEnd: 0,\n // content: <p>Hello</p><p>World</p>\n // }\n // We should render the following decorations:\n // <span>Hello</span>\n // <div><p>World</p></div>\n if (openStart > 0 && content.childCount >= 2) {\n const nodes = content.content\n const head = Fragment.from(nodes.slice(0, 1))\n const body = Fragment.from(nodes.slice(1))\n\n return [\n ...decorateDeletionSlice(new Slice(head, openStart, openStart)),\n ...decorateDeletionSlice(new Slice(body, 0, openEnd)),\n ]\n }\n\n // For example, if the slice is\n // {\n // openStart: 0,\n // openEnd: 1,\n // content: <p>Hello</p><p>World</p>\n // }\n // We should render the following decorations:\n // <div><p>Hello</p></div>\n // <span>World</span>\n if (openEnd > 0 && content.childCount >= 2) {\n const nodes = content.content\n const body = Fragment.from(nodes.slice(0, -1))\n const tail = Fragment.from(nodes.slice(-1))\n return [\n ...decorateDeletionSlice(new Slice(body, openStart, 0)),\n ...decorateDeletionSlice(new Slice(tail, openEnd, openEnd)),\n ]\n }\n\n const schema = content.child(0).type.schema\n const isInline = content.child(0).isInline\n\n const render = (view: EditorView): HTMLElement => {\n const document = view.dom.ownerDocument\n\n // Render the fragment to HTML\n const element = document.createElement(isInline ? 'span' : 'div')\n const serializer = DOMSerializer.fromSchema(schema)\n serializer.serializeFragment(content, { document }, element)\n\n // Add the class to the element\n element.classList.add('prosekit-commit-deletion')\n return element\n }\n\n return [render]\n}\n\nfunction decorateDeletion(\n /** The doc node before the deletion */\n doc: ProseMirrorNode,\n /** The start position of the deleted text in the doc node */\n from: number,\n /** The end position of the deleted text in the doc node */\n to: number,\n /** The insert position of the decoration in the doc node after the change */\n pos: number,\n): Decoration[] {\n const slice = doc.slice(from, to)\n\n const renders = decorateDeletionSlice(slice)\n const count = renders.length\n\n return renders.map((render, index) =>\n Decoration.widget(pos, render, {\n side: -20 - count + index,\n // Ensure the text in the decoration is able to be selected.\n ignoreSelection: true,\n })\n )\n}\n\nfunction decorateAddition(\n /** The start position of the inserted text in the doc node */\n from: number,\n /** The end position of the inserted text in the doc node */\n to: number,\n): Decoration {\n return Decoration.inline(from, to, { class: 'prosekit-commit-addition' })\n}\n\nfunction decorateChange(prev: ProseMirrorNode, change: Change): Decoration[] {\n const { fromA, toA, fromB, toB } = change\n const decorations: Decoration[] = []\n\n if (fromA < toA) {\n decorations.push(...decorateDeletion(prev, fromA, toA, fromB))\n }\n if (fromB < toB) {\n decorations.push(decorateAddition(fromB, toB))\n }\n\n return decorations\n}\n\nfunction decorateCommit(\n doc: ProseMirrorNode,\n parent: ProseMirrorNode,\n steps: Step[],\n): DecorationSet {\n const changes = getChanges(doc, parent, steps)\n const decorations = changes.flatMap((change) => decorateChange(parent, change))\n return DecorationSet.create(doc, decorations)\n}\n\nfunction defineCommitDecoration(commit: Commit): PlainExtension {\n const key = new PluginKey<DecorationSet>('prosekit-commit-decoration')\n\n return definePlugin(({ schema }): ProseMirrorPlugin => {\n const parent = schema.nodeFromJSON(commit.parent)\n const steps = commit.steps.map((step) => Step.fromJSON(schema, step))\n\n return new ProseMirrorPlugin({\n key,\n state: {\n init: (_, instance): DecorationSet => {\n return decorateCommit(instance.doc, parent, steps)\n },\n apply: (tr, deco: DecorationSet): DecorationSet => {\n return deco.map(tr.mapping, tr.doc)\n },\n },\n props: {\n decorations: (state): DecorationSet | undefined => {\n return key.getState(state)\n },\n },\n })\n })\n}\n\n/**\n * Define an extension to display the changes from the given commit in the editor.\n */\nfunction defineCommitViewer(commit: Commit): PlainExtension {\n return union(\n defineDefaultState({ defaultContent: commit.doc }),\n defineCommitDecoration(commit),\n )\n}\n\nclass CommitRecorder {\n private parent: ProseMirrorNode | null = null\n private doc: ProseMirrorNode | null = null\n private steps: Step[] = []\n\n /**\n * Return a commit object including all changes since the last commit. `null`\n * will be returned if there is no change.\n */\n commit(): Commit | null {\n if (\n !this.parent\n || !this.doc\n || this.steps.length === 0\n || this.parent.eq(this.doc)\n ) {\n return null\n }\n\n const commit: Commit = {\n doc: jsonFromNode(this.doc),\n parent: jsonFromNode(this.parent),\n steps: this.steps.map((step) => step.toJSON() as StepJSON),\n }\n this.init(this.doc)\n return commit\n }\n\n /**\n * @internal\n */\n init(doc: ProseMirrorNode): void {\n this.doc = doc\n this.parent = doc\n this.steps = []\n }\n\n /**\n * @internal\n */\n apply(tr: Transaction): void {\n this.steps.push(...tr.steps)\n this.doc = tr.doc\n }\n}\n\n/**\n * Define an extension that can record the changes in the editor.\n */\nfunction defineCommitRecorder(commitRecorder: CommitRecorder): PlainExtension {\n const key = new PluginKey<DecorationSet>('prosekit-commit-recorder')\n\n return definePlugin(\n new ProseMirrorPlugin({\n key,\n state: {\n init: (_, state): void => {\n commitRecorder.init(state.doc)\n },\n apply: (tr): void => {\n commitRecorder.apply(tr)\n },\n },\n }),\n )\n}\n\nexport { CommitRecorder, defineCommitRecorder, defineCommitViewer, type Commit }\n"],"mappings":";;;;;;AA0BA,SAAS,WACP,KACA,QACA,OACmB;AAOnB,QANgB,UAAU,OAAO,OAAO,CAChB,SACtB,KACA,MAAM,KAAK,SAAS,KAAK,QAAQ,CAAC,EAClC,KACD,CACc;;AAGjB,SAAS,gBAAgB,MAA+B;AAEtD,QADiB,KAAK,IAAI,cACV,cAAc,MAAM;;AAGtC,SAAS,sBACP,OAC0C;CAE1C,IAAI,EAAE,WAAW,SAAS,YAAY;AAEtC,QAAO,YAAY,KAAK,UAAU,KAAK,QAAQ,eAAe,GAAG;AAC/D;AACA;AACA,YAAU,QAAQ,MAAM,EAAE,CAAC;;AAI7B,KAAI,QAAQ,eAAe,EACzB,QAAO,EAAE;AAaX,KAAI,YAAY,KAAK,UAAU,KAAK,QAAQ,eAAe,GAAG;EAC5D,MAAM,OAAO,SAAS,KAAK,CAAC,Q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;MAE7B;GACF,CAAC;GACF;;;;;AAMJ,SAAS,mBAAmB,QAAgC;AAC1D,QAAO,MACL,mBAAmB,EAAE,gBAAgB,OAAO,KAAK,CAAC,EAClD,uBAAuB,OAAO,CAC/B;;AAGH,IAAM,iBAAN,MAAqB;;gBACsB;aACH;eACd,EAAE;;;;;;CAM1B,SAAwB;AACtB,MACE,CAAC,KAAK,UACH,CAAC,KAAK,OACN,KAAK,MAAM,WAAW,KACtB,KAAK,OAAO,GAAG,KAAK,IAAI,CAE3B,QAAO;EAGT,MAAM,SAAiB;GACrB,KAAK,aAAa,KAAK,IAAI;GAC3B,QAAQ,aAAa,KAAK,OAAO;GACjC,OAAO,KAAK,MAAM,KAAK,SAAS,KAAK,QAAQ,CAAa;GAC3D;AACD,OAAK,KAAK,KAAK,IAAI;AACnB,SAAO;;;;;CAMT,KAAK,KAA4B;AAC/B,OAAK,MAAM;AACX,OAAK,SAAS;AACd,OAAK,QAAQ,EAAE;;;;;CAMjB,MAAM,IAAuB;AAC3B,OAAK,MAAM,KAAK,GAAG,GAAG,MAAM;AAC5B,OAAK,MAAM,GAAG;;;;;;AAOlB,SAAS,qBAAqB,gBAAgD;AAG5E,QAAO,aACL,IAAI,kBAAkB;EACpB,KAJQ,IAAI,UAAyB,2BAA2B;EAKhE,OAAO;GACL,OAAO,GAAG,UAAgB;AACxB,mBAAe,KAAK,MAAM,IAAI;;GAEhC,QAAQ,OAAa;AACnB,mBAAe,MAAM,GAAG;;GAE3B;EACF,CAAC,CACH"}
1
+ {"version":3,"file":"commit.js","names":[],"sources":["../src/commit/index.ts"],"sourcesContent":["import { defineDefaultState, definePlugin, jsonFromNode, union, type NodeJSON, type PlainExtension, type StepJSON } from '@prosekit/core'\nimport { DOMSerializer, Fragment, Slice, type ProseMirrorNode } from '@prosekit/pm/model'\nimport { PluginKey, ProseMirrorPlugin, type Transaction } from '@prosekit/pm/state'\nimport { Step } from '@prosekit/pm/transform'\nimport { Decoration, DecorationSet, type EditorView } from '@prosekit/pm/view'\nimport { ChangeSet, type Change } from 'prosemirror-changeset'\n\n/**\n * A JSON representation of a commit.\n */\ninterface Commit {\n /**\n * The current doc node in the JSON format\n */\n doc: NodeJSON\n /**\n * The parent node in the JSON format\n */\n parent: NodeJSON\n /**\n * An array of steps in the JSON format that transform the parent node to the\n * current doc node.\n */\n steps: StepJSON[]\n}\n\nfunction getChanges(\n doc: ProseMirrorNode,\n parent: ProseMirrorNode,\n steps: Step[],\n): readonly Change[] {\n const initSet = ChangeSet.create(parent)\n const currSet = initSet.addSteps(\n doc,\n steps.map((step) => step.getMap()),\n null,\n )\n return currSet.changes\n}\n\nfunction renderDivWeight(view: EditorView): HTMLElement {\n const document = view.dom.ownerDocument\n return document.createElement('div')\n}\n\nfunction decorateDeletionSlice(\n slice: Slice,\n): Array<(view: EditorView) => HTMLElement> {\n // Get the fragment of the deleted content\n let { openStart, openEnd, content } = slice\n\n while (openStart > 0 && openEnd > 0 && content.childCount === 1) {\n openStart--\n openEnd--\n content = content.child(0).content\n }\n\n // Nothing to render\n if (content.childCount === 0) {\n return []\n }\n\n // For example, if the slice is\n // {\n // openStart: 1,\n // openEnd: 1,\n // content: <p>Hello</p><p>World</p>\n // }\n // We should render the following decorations:\n // <span>Hello</span>\n // <div></div>\n // <span>World</span>\n if (openStart > 0 && openEnd > 0 && content.childCount === 2) {\n const head = Fragment.from([content.child(0)])\n const tail = Fragment.from([content.child(1)])\n return [\n ...decorateDeletionSlice(new Slice(head, openStart, openStart)),\n renderDivWeight,\n ...decorateDeletionSlice(new Slice(tail, openEnd, openEnd)),\n ]\n }\n\n // For example, if the slice is\n // {\n // openStart: 1,\n // openEnd: 0,\n // content: <p>Hello</p><p>World</p>\n // }\n // We should render the following decorations:\n // <span>Hello</span>\n // <div><p>World</p></div>\n if (openStart > 0 && content.childCount >= 2) {\n const nodes = content.content\n const head = Fragment.from(nodes.slice(0, 1))\n const body = Fragment.from(nodes.slice(1))\n\n return [\n ...decorateDeletionSlice(new Slice(head, openStart, openStart)),\n ...decorateDeletionSlice(new Slice(body, 0, openEnd)),\n ]\n }\n\n // For example, if the slice is\n // {\n // openStart: 0,\n // openEnd: 1,\n // content: <p>Hello</p><p>World</p>\n // }\n // We should render the following decorations:\n // <div><p>Hello</p></div>\n // <span>World</span>\n if (openEnd > 0 && content.childCount >= 2) {\n const nodes = content.content\n const body = Fragment.from(nodes.slice(0, -1))\n const tail = Fragment.from(nodes.slice(-1))\n return [\n ...decorateDeletionSlice(new Slice(body, openStart, 0)),\n ...decorateDeletionSlice(new Slice(tail, openEnd, openEnd)),\n ]\n }\n\n const schema = content.child(0).type.schema\n const isInline = content.child(0).isInline\n\n const render = (view: EditorView): HTMLElement => {\n const document = view.dom.ownerDocument\n\n // Render the fragment to HTML\n const element = document.createElement(isInline ? 'span' : 'div')\n const serializer = DOMSerializer.fromSchema(schema)\n serializer.serializeFragment(content, { document }, element)\n\n // Add the class to the element\n element.classList.add('prosekit-commit-deletion')\n return element\n }\n\n return [render]\n}\n\nfunction decorateDeletion(\n /** The doc node before the deletion */\n doc: ProseMirrorNode,\n /** The start position of the deleted text in the doc node */\n from: number,\n /** The end position of the deleted text in the doc node */\n to: number,\n /** The insert position of the decoration in the doc node after the change */\n pos: number,\n): Decoration[] {\n const slice = doc.slice(from, to)\n\n const renders = decorateDeletionSlice(slice)\n const count = renders.length\n\n return renders.map((render, index) =>\n Decoration.widget(pos, render, {\n side: -20 - count + index,\n // Ensure the text in the decoration is able to be selected.\n ignoreSelection: true,\n })\n )\n}\n\nfunction decorateAddition(\n /** The start position of the inserted text in the doc node */\n from: number,\n /** The end position of the inserted text in the doc node */\n to: number,\n): Decoration {\n return Decoration.inline(from, to, { class: 'prosekit-commit-addition' })\n}\n\nfunction decorateChange(prev: ProseMirrorNode, change: Change): Decoration[] {\n const { fromA, toA, fromB, toB } = change\n const decorations: Decoration[] = []\n\n if (fromA < toA) {\n decorations.push(...decorateDeletion(prev, fromA, toA, fromB))\n }\n if (fromB < toB) {\n decorations.push(decorateAddition(fromB, toB))\n }\n\n return decorations\n}\n\nfunction decorateCommit(\n doc: ProseMirrorNode,\n parent: ProseMirrorNode,\n steps: Step[],\n): DecorationSet {\n const changes = getChanges(doc, parent, steps)\n const decorations = changes.flatMap((change) => decorateChange(parent, change))\n return DecorationSet.create(doc, decorations)\n}\n\nfunction defineCommitDecoration(commit: Commit): PlainExtension {\n const key = new PluginKey<DecorationSet>('prosekit-commit-decoration')\n\n return definePlugin(({ schema }): ProseMirrorPlugin => {\n const parent = schema.nodeFromJSON(commit.parent)\n const steps = commit.steps.map((step) => Step.fromJSON(schema, step))\n\n return new ProseMirrorPlugin({\n key,\n state: {\n init: (_, instance): DecorationSet => {\n return decorateCommit(instance.doc, parent, steps)\n },\n apply: (tr, deco: DecorationSet): DecorationSet => {\n return deco.map(tr.mapping, tr.doc)\n },\n },\n props: {\n decorations: (state): DecorationSet | undefined => {\n return key.getState(state)\n },\n },\n })\n })\n}\n\n/**\n * Define an extension to display the changes from the given commit in the editor.\n */\nfunction defineCommitViewer(commit: Commit): PlainExtension {\n return union(\n defineDefaultState({ defaultContent: commit.doc }),\n defineCommitDecoration(commit),\n )\n}\n\nclass CommitRecorder {\n private parent: ProseMirrorNode | null = null\n private doc: ProseMirrorNode | null = null\n private steps: Step[] = []\n\n /**\n * Return a commit object including all changes since the last commit. `null`\n * will be returned if there is no change.\n */\n commit(): Commit | null {\n if (\n !this.parent\n || !this.doc\n || this.steps.length === 0\n || this.parent.eq(this.doc)\n ) {\n return null\n }\n\n const commit: Commit = {\n doc: jsonFromNode(this.doc),\n parent: jsonFromNode(this.parent),\n steps: this.steps.map((step) => step.toJSON() as StepJSON),\n }\n this.init(this.doc)\n return commit\n }\n\n /**\n * @internal\n */\n init(doc: ProseMirrorNode): void {\n this.doc = doc\n this.parent = doc\n this.steps = []\n }\n\n /**\n * @internal\n */\n apply(tr: Transaction): void {\n this.steps.push(...tr.steps)\n this.doc = tr.doc\n }\n}\n\n/**\n * Define an extension that can record the changes in the editor.\n */\nfunction defineCommitRecorder(commitRecorder: CommitRecorder): PlainExtension {\n const key = new PluginKey<DecorationSet>('prosekit-commit-recorder')\n\n return definePlugin(\n new ProseMirrorPlugin({\n key,\n state: {\n init: (_, state): void => {\n commitRecorder.init(state.doc)\n },\n apply: (tr): void => {\n commitRecorder.apply(tr)\n },\n },\n }),\n )\n}\n\nexport { CommitRecorder, defineCommitRecorder, defineCommitViewer, type Commit }\n"],"mappings":";;;;;;AA0BA,SAAS,WACP,KACA,QACA,OACmB;AAOnB,QANgB,UAAU,OAAO,OACV,CAAC,SACtB,KACA,MAAM,KAAK,SAAS,KAAK,QAAQ,CAAC,EAClC,KAEY,CAAC;;AAGjB,SAAS,gBAAgB,MAA+B;AAEtD,QADiB,KAAK,IAAI,cACV,cAAc,MAAM;;AAGtC,SAAS,sBACP,OAC0C;CAE1C,IAAI,EAAE,WAAW,SAAS,YAAY;AAEtC,QAAO,YAAY,KAAK,UAAU,KAAK,QAAQ,eAAe,GAAG;AAC/D;AACA;AACA,YAAU,QAAQ,MAAM,EAAE,CAAC;;AAI7B,KAAI,QAAQ,eAAe,EACzB,QAAO,EAAE;AAaX,KAAI,YAAY,KAAK,UAAU,KAAK,QAAQ,eAAe,GAAG;EAC5D,MAAM,OAAO,SAAS,KAAK,CAAC,QAAQ,MAAM,EAAE,CAAC,CAAC;EAC9C,MAAM,OAAO,SAAS,KAAK,CAAC,QAAQ,MAAM,EAAE,CAAC,CAAC;AAC9C,SAAO;GACL,GAAG,sBAAsB,IAAI,MAAM,MAAM,WAAW,UAAU,CAAC;GAC/D;GACA,GAAG,sBAAsB,IAAI,MAAM,MAAM,SAAS,QAAQ,CAAC;GAC5D;;AAYH,KAAI,YAAY,KAAK,QAAQ,cAAc,GAAG;EAC5C,MAAM,QAAQ,QAAQ;EACtB,MAAM,OAAO,SAAS,KAAK,MAAM,MAAM,GAAG,EAAE,CAAC;EAC7C,MAAM,OAAO,SAAS,KAAK,MAAM,MAAM,EAAE,CAAC;A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MAAM,KAAK,SAAS,KAAK,QAAQ,CAAa;GAC3D;AACD,OAAK,KAAK,KAAK,IAAI;AACnB,SAAO;;;;;CAMT,KAAK,KAA4B;AAC/B,OAAK,MAAM;AACX,OAAK,SAAS;AACd,OAAK,QAAQ,EAAE;;;;;CAMjB,MAAM,IAAuB;AAC3B,OAAK,MAAM,KAAK,GAAG,GAAG,MAAM;AAC5B,OAAK,MAAM,GAAG;;;;;;AAOlB,SAAS,qBAAqB,gBAAgD;AAG5E,QAAO,aACL,IAAI,kBAAkB;EACpB,KAAA,IAJY,UAAyB,2BAIlC;EACH,OAAO;GACL,OAAO,GAAG,UAAgB;AACxB,mBAAe,KAAK,MAAM,IAAI;;GAEhC,QAAQ,OAAa;AACnB,mBAAe,MAAM,GAAG;;GAE3B;EACF,CAAC,CACH"}
@@ -1 +1 @@
1
- {"version":3,"file":"heading.js","names":[],"sources":["../src/heading/heading-commands.ts","../src/heading/heading-input-rule.ts","../src/heading/heading-keymap.ts","../src/heading/heading-spec.ts","../src/heading/heading.ts"],"sourcesContent":["import { defineCommands, insertNode, setBlockType, toggleNode, type Extension } from '@prosekit/core'\n\nimport type { HeadingAttrs } from './heading-types.ts'\n\n/**\n * @internal\n */\nexport type HeadingCommandsExtension = Extension<{\n Commands: {\n setHeading: [attrs?: HeadingAttrs | undefined]\n insertHeading: [attrs?: HeadingAttrs | undefined]\n toggleHeading: [attrs?: HeadingAttrs | undefined]\n }\n}>\n\n/**\n * @internal\n */\nexport function defineHeadingCommands(): HeadingCommandsExtension {\n return defineCommands({\n setHeading: (attrs?: HeadingAttrs) => {\n return setBlockType({ type: 'heading', attrs })\n },\n insertHeading: (attrs?: HeadingAttrs) => {\n return insertNode({ type: 'heading', attrs })\n },\n toggleHeading: (attrs?: HeadingAttrs) => {\n return toggleNode({ type: 'heading', attrs })\n },\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport { defineTextBlockInputRule } from '../input-rule/index.ts'\n\nimport type { HeadingAttrs } from './heading-types.ts'\n\n/**\n * Converts the text block to a heading when `#` is typed at the start of a new\n * line followed by a space.\n *\n * @internal\n */\nexport function defineHeadingInputRule(): PlainExtension {\n return defineTextBlockInputRule({\n regex: /^(#{1,6})\\s$/,\n type: 'heading',\n attrs: (match) => {\n const level: number = match[1]?.length ?? 1\n return { level } satisfies HeadingAttrs\n },\n })\n}\n","import { defineKeymap, isAtBlockStart, toggleNode, unsetBlockType, withSkipCodeBlock, type PlainExtension } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\n\nfunction toggleHeadingKeybinding(level: number): Command {\n return withSkipCodeBlock(toggleNode({ type: 'heading', attrs: { level } }))\n}\n\n/**\n * Set the block type to default (usually `paragraph`) when pressing Backspace at\n * the start of a heading block.\n */\nconst backspaceUnsetHeading: Command = (state, dispatch, view) => {\n const $pos = isAtBlockStart(state, view)\n if ($pos?.parent.type.name === 'heading') {\n return unsetBlockType()(state, dispatch, view)\n }\n return false\n}\n\n/**\n * @internal\n */\nexport function defineHeadingKeymap(): PlainExtension {\n return defineKeymap({\n 'Mod-Alt-1': toggleHeadingKeybinding(1),\n 'Mod-Alt-2': toggleHeadingKeybinding(2),\n 'Mod-Alt-3': toggleHeadingKeybinding(3),\n 'Mod-Alt-4': toggleHeadingKeybinding(4),\n 'Mod-Alt-5': toggleHeadingKeybinding(5),\n 'Mod-Alt-6': toggleHeadingKeybinding(6),\n 'Backspace': backspaceUnsetHeading,\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\n\nimport type { HeadingAttrs } from './heading-types.ts'\n\n/**\n * @internal\n */\nexport type HeadingSpecExtension = Extension<{\n Nodes: {\n heading: HeadingAttrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineHeadingSpec(): HeadingSpecExtension {\n return defineNodeSpec({\n name: 'heading',\n attrs: { level: { default: 1, validate: 'number' } },\n content: 'inline*',\n group: 'block',\n defining: true,\n parseDOM: [\n { tag: 'h1', attrs: { level: 1 } },\n { tag: 'h2', attrs: { level: 2 } },\n { tag: 'h3', attrs: { level: 3 } },\n { tag: 'h4', attrs: { level: 4 } },\n { tag: 'h5', attrs: { level: 5 } },\n { tag: 'h6', attrs: { level: 6 } },\n ],\n toDOM(node) {\n return [`h${node.attrs.level}`, 0]\n },\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineHeadingCommands, type HeadingCommandsExtension } from './heading-commands.ts'\nimport { defineHeadingInputRule } from './heading-input-rule.ts'\nimport { defineHeadingKeymap } from './heading-keymap.ts'\nimport { defineHeadingSpec, type HeadingSpecExtension } from './heading-spec.ts'\n\n/**\n * @internal\n */\nexport type HeadingExtension = Union<\n [HeadingSpecExtension, HeadingCommandsExtension]\n>\n\n/**\n * @public\n */\nexport function defineHeading(): HeadingExtension {\n return union(\n defineHeadingSpec(),\n defineHeadingInputRule(),\n defineHeadingKeymap(),\n defineHeadingCommands(),\n )\n}\n"],"mappings":";;;;;AAkBA,SAAgB,wBAAkD;AAChE,QAAO,eAAe;EACpB,aAAa,UAAyB;AACpC,UAAO,aAAa;IAAE,MAAM;IAAW;IAAO,CAAC;;EAEjD,gBAAgB,UAAyB;AACvC,UAAO,WAAW;IAAE,MAAM;IAAW;IAAO,CAAC;;EAE/C,gBAAgB,UAAyB;AACvC,UAAO,WAAW;IAAE,MAAM;IAAW;IAAO,CAAC;;EAEhD,CAAC;;;;;;;;ACjBJ,SAAgB,yBAAyC;AACvD,QAAO,yBAAyB;EAC9B,OAAO;EACP,MAAM;EACN,QAAQ,UAAU;AAEhB,UAAO,EAAE,OADa,MAAM,IAAI,UAAU,GAC1B;;EAEnB,CAAC;;ACjBJ,SAAS,wBAAwB,OAAwB;AACvD,QAAO,kBAAkB,WAAW;EAAE,MAAM;EAAW,OAAO,EAAE,OAAO;EAAE,CAAC,CAAC;;;;;;AAO7E,MAAM,yBAAkC,OAAO,UAAU,SAAS;AAEhE,KADa,eAAe,OAAO,KAAK,EAC9B,OAAO,KAAK,SAAS,UAC7B,QAAO,gBAAgB,CAAC,OAAO,UAAU,KAAK;AAEhD,QAAO;;;;;AAMT,SAAgB,sBAAsC;AACpD,QAAO,aAAa;EAClB,aAAa,wBAAwB,EAAE;EACvC,aAAa,wBAAwB,EAAE;EACvC,aAAa,wBAAwB,EAAE;EACvC,aAAa,wBAAwB,EAAE;EACvC,aAAa,wBAAwB,EAAE;EACvC,aAAa,wBAAwB,EAAE;EACvC,aAAa;EACd,CAAC;;;;;ACfJ,SAAgB,oBAA0C;AACxD,QAAO,eAAe;EACpB,MAAM;EACN,OAAO,EAAE,OAAO;GAAE,SAAS;GAAG,UAAU;GAAU,EAAE;EACpD,SAAS;EACT,OAAO;EACP,UAAU;EACV,UAAU;GACR;IAAE,KAAK;IAAM,OAAO,EAAE,OAAO,GAAG;IAAE;GAClC;IAAE,KAAK;IAAM,OAAO,EAAE,OAAO,GAAG;IAAE;GAClC;IAAE,KAAK;IAAM,OAAO,EAAE,OAAO,GAAG;IAAE;GAClC;IAAE,KAAK;IAAM,OAAO,EAAE,OAAO,GAAG;IAAE;GAClC;IAAE,KAAK;IAAM,OAAO,EAAE,OAAO,GAAG;IAAE;GAClC;IAAE,KAAK;IAAM,OAAO,EAAE,OAAO,GAAG;IAAE;GACnC;EACD,MAAM,MAAM;AACV,UAAO,CAAC,IAAI,KAAK,MAAM,SAAS,EAAE;;EAErC,CAAC;;;;;ACjBJ,SAAgB,gBAAkC;AAChD,QAAO,MACL,mBAAmB,EACnB,wBAAwB,EACxB,qBAAqB,EACrB,uBAAuB,CACxB"}
1
+ {"version":3,"file":"heading.js","names":[],"sources":["../src/heading/heading-commands.ts","../src/heading/heading-input-rule.ts","../src/heading/heading-keymap.ts","../src/heading/heading-spec.ts","../src/heading/heading.ts"],"sourcesContent":["import { defineCommands, insertNode, setBlockType, toggleNode, type Extension } from '@prosekit/core'\n\nimport type { HeadingAttrs } from './heading-types.ts'\n\n/**\n * @internal\n */\nexport type HeadingCommandsExtension = Extension<{\n Commands: {\n setHeading: [attrs?: HeadingAttrs | undefined]\n insertHeading: [attrs?: HeadingAttrs | undefined]\n toggleHeading: [attrs?: HeadingAttrs | undefined]\n }\n}>\n\n/**\n * @internal\n */\nexport function defineHeadingCommands(): HeadingCommandsExtension {\n return defineCommands({\n setHeading: (attrs?: HeadingAttrs) => {\n return setBlockType({ type: 'heading', attrs })\n },\n insertHeading: (attrs?: HeadingAttrs) => {\n return insertNode({ type: 'heading', attrs })\n },\n toggleHeading: (attrs?: HeadingAttrs) => {\n return toggleNode({ type: 'heading', attrs })\n },\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport { defineTextBlockInputRule } from '../input-rule/index.ts'\n\nimport type { HeadingAttrs } from './heading-types.ts'\n\n/**\n * Converts the text block to a heading when `#` is typed at the start of a new\n * line followed by a space.\n *\n * @internal\n */\nexport function defineHeadingInputRule(): PlainExtension {\n return defineTextBlockInputRule({\n regex: /^(#{1,6})\\s$/,\n type: 'heading',\n attrs: (match) => {\n const level: number = match[1]?.length ?? 1\n return { level } satisfies HeadingAttrs\n },\n })\n}\n","import { defineKeymap, isAtBlockStart, toggleNode, unsetBlockType, withSkipCodeBlock, type PlainExtension } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\n\nfunction toggleHeadingKeybinding(level: number): Command {\n return withSkipCodeBlock(toggleNode({ type: 'heading', attrs: { level } }))\n}\n\n/**\n * Set the block type to default (usually `paragraph`) when pressing Backspace at\n * the start of a heading block.\n */\nconst backspaceUnsetHeading: Command = (state, dispatch, view) => {\n const $pos = isAtBlockStart(state, view)\n if ($pos?.parent.type.name === 'heading') {\n return unsetBlockType()(state, dispatch, view)\n }\n return false\n}\n\n/**\n * @internal\n */\nexport function defineHeadingKeymap(): PlainExtension {\n return defineKeymap({\n 'Mod-Alt-1': toggleHeadingKeybinding(1),\n 'Mod-Alt-2': toggleHeadingKeybinding(2),\n 'Mod-Alt-3': toggleHeadingKeybinding(3),\n 'Mod-Alt-4': toggleHeadingKeybinding(4),\n 'Mod-Alt-5': toggleHeadingKeybinding(5),\n 'Mod-Alt-6': toggleHeadingKeybinding(6),\n 'Backspace': backspaceUnsetHeading,\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\n\nimport type { HeadingAttrs } from './heading-types.ts'\n\n/**\n * @internal\n */\nexport type HeadingSpecExtension = Extension<{\n Nodes: {\n heading: HeadingAttrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineHeadingSpec(): HeadingSpecExtension {\n return defineNodeSpec({\n name: 'heading',\n attrs: { level: { default: 1, validate: 'number' } },\n content: 'inline*',\n group: 'block',\n defining: true,\n parseDOM: [\n { tag: 'h1', attrs: { level: 1 } },\n { tag: 'h2', attrs: { level: 2 } },\n { tag: 'h3', attrs: { level: 3 } },\n { tag: 'h4', attrs: { level: 4 } },\n { tag: 'h5', attrs: { level: 5 } },\n { tag: 'h6', attrs: { level: 6 } },\n ],\n toDOM(node) {\n return [`h${node.attrs.level}`, 0]\n },\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineHeadingCommands, type HeadingCommandsExtension } from './heading-commands.ts'\nimport { defineHeadingInputRule } from './heading-input-rule.ts'\nimport { defineHeadingKeymap } from './heading-keymap.ts'\nimport { defineHeadingSpec, type HeadingSpecExtension } from './heading-spec.ts'\n\n/**\n * @internal\n */\nexport type HeadingExtension = Union<\n [HeadingSpecExtension, HeadingCommandsExtension]\n>\n\n/**\n * @public\n */\nexport function defineHeading(): HeadingExtension {\n return union(\n defineHeadingSpec(),\n defineHeadingInputRule(),\n defineHeadingKeymap(),\n defineHeadingCommands(),\n )\n}\n"],"mappings":";;;;;AAkBA,SAAgB,wBAAkD;AAChE,QAAO,eAAe;EACpB,aAAa,UAAyB;AACpC,UAAO,aAAa;IAAE,MAAM;IAAW;IAAO,CAAC;;EAEjD,gBAAgB,UAAyB;AACvC,UAAO,WAAW;IAAE,MAAM;IAAW;IAAO,CAAC;;EAE/C,gBAAgB,UAAyB;AACvC,UAAO,WAAW;IAAE,MAAM;IAAW;IAAO,CAAC;;EAEh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;;;;;ACjBJ,SAAgB,gBAAkC;AAChD,QAAO,MACL,mBAAmB,EACnB,wBAAwB,EACxB,qBAAqB,EACrB,uBAAuB,CACxB"}
@@ -1 +1 @@
1
- {"version":3,"file":"horizontal-rule.js","names":[],"sources":["../src/horizontal-rule/horizontal-rule-commands.ts","../src/horizontal-rule/horizontal-rule-input-rule.ts","../src/horizontal-rule/horizontal-rule-spec.ts","../src/horizontal-rule/horizontal-rule.ts"],"sourcesContent":["import { defineCommands, getNodeType, type Extension } from '@prosekit/core'\nimport { Fragment, Slice } from '@prosekit/pm/model'\nimport type { Command } from '@prosekit/pm/state'\n\nexport type HorizontalRuleCommandsExtension = Extension<{\n Commands: {\n insertHorizontalRule: []\n }\n}>\n\nconst insertHorizontalRuleCommand: Command = (state, dispatch): boolean => {\n if (!dispatch) return true\n\n const { schema, tr } = state\n const type = getNodeType(schema, 'horizontalRule')\n const node = type.createChecked()\n const pos = tr.selection.anchor\n const slice = new Slice(Fragment.from(node), 0, 0)\n tr.replaceRange(pos, pos, slice).scrollIntoView()\n dispatch(tr)\n return true\n}\n\n/**\n * Returns a command that inserts a horizontal rule at the current selection.\n */\nexport function insertHorizontalRule(): Command {\n return insertHorizontalRuleCommand\n}\n\nexport function defineHorizontalRuleCommands(): HorizontalRuleCommandsExtension {\n return defineCommands({ insertHorizontalRule })\n}\n","import { getNodeType, union, type PlainExtension } from '@prosekit/core'\nimport { InputRule } from '@prosekit/pm/inputrules'\n\nimport { defineInputRule } from '../input-rule/index.ts'\n\n/**\n * @public\n */\nexport function defineHorizontalRuleInputRule(): PlainExtension {\n return union(\n defineInputRule(\n new InputRule(/^---$/, (state, match, start, end) => {\n const { schema } = state\n const { tr } = state\n const type = getNodeType(schema, 'horizontalRule')\n const node = type.createChecked()\n tr.delete(start, end).insert(start - 1, node)\n return tr.scrollIntoView()\n }),\n ),\n )\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { Attrs } from '@prosekit/pm/model'\n\nexport type HorizontalRuleSpecExtension = Extension<{\n Nodes: {\n horizontalRule: Attrs\n }\n}>\n\nexport function defineHorizontalRuleSpec(): HorizontalRuleSpecExtension {\n return defineNodeSpec({\n name: 'horizontalRule',\n group: 'block',\n parseDOM: [{ tag: 'hr' }],\n // Wrap the `<hr>` in a `<div>` so that we can make it taller and easier to click.\n toDOM: () => ['div', { class: 'prosekit-horizontal-rule' }, ['hr']],\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineHorizontalRuleCommands, type HorizontalRuleCommandsExtension } from './horizontal-rule-commands.ts'\nimport { defineHorizontalRuleInputRule } from './horizontal-rule-input-rule.ts'\nimport { defineHorizontalRuleSpec, type HorizontalRuleSpecExtension } from './horizontal-rule-spec.ts'\n\nexport type HorizontalRuleExtension = Union<\n [HorizontalRuleSpecExtension, HorizontalRuleCommandsExtension]\n>\n\n/**\n * @public\n */\nexport function defineHorizontalRule(): HorizontalRuleExtension {\n return union(\n defineHorizontalRuleSpec(),\n defineHorizontalRuleInputRule(),\n defineHorizontalRuleCommands(),\n )\n}\n"],"mappings":";;;;AAUA,MAAM,+BAAwC,OAAO,aAAsB;AACzE,KAAI,CAAC,SAAU,QAAO;CAEtB,MAAM,EAAE,QAAQ,OAAO;CAEvB,MAAM,OADO,YAAY,QAAQ,iBAAiB,CAChC,eAAe;CACjC,MAAM,MAAM,GAAG,UAAU;CACzB,MAAM,QAAQ,IAAI,MAAM,SAAS,KAAK,KAAK,EAAE,GAAG,EAAE;AAClD,IAAG,aAAa,KAAK,KAAK,MAAM,CAAC,gBAAgB;AACjD,UAAS,GAAG;AACZ,QAAO;;;;;AAMT,SAAgB,uBAAgC;AAC9C,QAAO;;AAGT,SAAgB,+BAAgE;AAC9E,QAAO,eAAe,EAAE,sBAAsB,CAAC;;;;;ACvBjD,SAAgB,gCAAgD;AAC9D,QAAO,MACL,gBACE,IAAI,UAAU,UAAU,OAAO,OAAO,OAAO,QAAQ;EACnD,MAAM,EAAE,WAAW;EACnB,MAAM,EAAE,OAAO;EAEf,MAAM,OADO,YAAY,QAAQ,iBAAiB,CAChC,eAAe;AACjC,KAAG,OAAO,OAAO,IAAI,CAAC,OAAO,QAAQ,GAAG,KAAK;AAC7C,SAAO,GAAG,gBAAgB;GAC1B,CACH,CACF;;ACXH,SAAgB,2BAAwD;AACtE,QAAO,eAAe;EACpB,MAAM;EACN,OAAO;EACP,UAAU,CAAC,EAAE,KAAK,MAAM,CAAC;EAEzB,aAAa;GAAC;GAAO,EAAE,OAAO,4BAA4B;GAAE,CAAC,KAAK;GAAC;EACpE,CAAC;;;;;ACHJ,SAAgB,uBAAgD;AAC9D,QAAO,MACL,0BAA0B,EAC1B,+BAA+B,EAC/B,8BAA8B,CAC/B"}
1
+ {"version":3,"file":"horizontal-rule.js","names":[],"sources":["../src/horizontal-rule/horizontal-rule-commands.ts","../src/horizontal-rule/horizontal-rule-input-rule.ts","../src/horizontal-rule/horizontal-rule-spec.ts","../src/horizontal-rule/horizontal-rule.ts"],"sourcesContent":["import { defineCommands, getNodeType, type Extension } from '@prosekit/core'\nimport { Fragment, Slice } from '@prosekit/pm/model'\nimport type { Command } from '@prosekit/pm/state'\n\nexport type HorizontalRuleCommandsExtension = Extension<{\n Commands: {\n insertHorizontalRule: []\n }\n}>\n\nconst insertHorizontalRuleCommand: Command = (state, dispatch): boolean => {\n if (!dispatch) return true\n\n const { schema, tr } = state\n const type = getNodeType(schema, 'horizontalRule')\n const node = type.createChecked()\n const pos = tr.selection.anchor\n const slice = new Slice(Fragment.from(node), 0, 0)\n tr.replaceRange(pos, pos, slice).scrollIntoView()\n dispatch(tr)\n return true\n}\n\n/**\n * Returns a command that inserts a horizontal rule at the current selection.\n */\nexport function insertHorizontalRule(): Command {\n return insertHorizontalRuleCommand\n}\n\nexport function defineHorizontalRuleCommands(): HorizontalRuleCommandsExtension {\n return defineCommands({ insertHorizontalRule })\n}\n","import { getNodeType, union, type PlainExtension } from '@prosekit/core'\nimport { InputRule } from '@prosekit/pm/inputrules'\n\nimport { defineInputRule } from '../input-rule/index.ts'\n\n/**\n * @public\n */\nexport function defineHorizontalRuleInputRule(): PlainExtension {\n return union(\n defineInputRule(\n new InputRule(/^---$/, (state, match, start, end) => {\n const { schema } = state\n const { tr } = state\n const type = getNodeType(schema, 'horizontalRule')\n const node = type.createChecked()\n tr.delete(start, end).insert(start - 1, node)\n return tr.scrollIntoView()\n }),\n ),\n )\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { Attrs } from '@prosekit/pm/model'\n\nexport type HorizontalRuleSpecExtension = Extension<{\n Nodes: {\n horizontalRule: Attrs\n }\n}>\n\nexport function defineHorizontalRuleSpec(): HorizontalRuleSpecExtension {\n return defineNodeSpec({\n name: 'horizontalRule',\n group: 'block',\n parseDOM: [{ tag: 'hr' }],\n // Wrap the `<hr>` in a `<div>` so that we can make it taller and easier to click.\n toDOM: () => ['div', { class: 'prosekit-horizontal-rule' }, ['hr']],\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineHorizontalRuleCommands, type HorizontalRuleCommandsExtension } from './horizontal-rule-commands.ts'\nimport { defineHorizontalRuleInputRule } from './horizontal-rule-input-rule.ts'\nimport { defineHorizontalRuleSpec, type HorizontalRuleSpecExtension } from './horizontal-rule-spec.ts'\n\nexport type HorizontalRuleExtension = Union<\n [HorizontalRuleSpecExtension, HorizontalRuleCommandsExtension]\n>\n\n/**\n * @public\n */\nexport function defineHorizontalRule(): HorizontalRuleExtension {\n return union(\n defineHorizontalRuleSpec(),\n defineHorizontalRuleInputRule(),\n defineHorizontalRuleCommands(),\n )\n}\n"],"mappings":";;;;AAUA,MAAM,+BAAwC,OAAO,aAAsB;AACzE,KAAI,CAAC,SAAU,QAAO;CAEtB,MAAM,EAAE,QAAQ,OAAO;CAEvB,MAAM,OADO,YAAY,QAAQ,iBAChB,CAAC,eAAe;CACjC,MAAM,MAAM,GAAG,UAAU;CACzB,MAAM,QAAQ,IAAI,MAAM,SAAS,KAAK,KAAK,EAAE,GAAG,EAAE;AAClD,IAAG,aAAa,KAAK,KAAK,MAAM,CAAC,gBAAgB;AACjD,UAAS,GAAG;AACZ,QAAO;;;;;AAMT,SAAgB,uBAAgC;AAC9C,QAAO;;AAGT,SAAgB,+BAAgE;AAC9E,QAAO,eAAe,EAAE,sBAAsB,CAAC;;;;;ACvBjD,SAAgB,gCAAgD;AAC9D,QAAO,MACL,gBACE,IAAI,UAAU,UAAU,OAAO,OAAO,OAAO,QAAQ;EACnD,MAAM,EAAE,WAAW;EACnB,MAAM,EAAE,OAAO;EAEf,MAAM,OADO,YAAY,QAAQ,iBAChB,CAAC,eAAe;AACjC,KAAG,OAAO,OAAO,IAAI,CAAC,OAAO,QAAQ,GAAG,KAAK;AAC7C,SAAO,GAAG,gBAAgB;GAC1B,CACH,CACF;;ACXH,SAAgB,2BAAwD;AACtE,QAAO,eAAe;EACpB,MAAM;EACN,OAAO;EACP,UAAU,CAAC,EAAE,KAAK,MAAM,CAAC;EAEzB,aAAa;GAAC;GAAO,EAAE,OAAO,4BAA4B;GAAE,CAAC,KAAK;GAAC;EACpE,CAAC;;;;;ACHJ,SAAgB,uBAAgD;AAC9D,QAAO,MACL,0BAA0B,EAC1B,+BAA+B,EAC/B,8BAA8B,CAC/B"}
package/dist/image.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"image.js","names":[],"sources":["../src/image/image-commands/insert-image.ts","../src/image/image-commands/upload-image.ts","../src/image/image-commands.ts","../src/image/image-spec.ts","../src/image/image-upload-handler.ts","../src/image/image.ts"],"sourcesContent":["import { insertNode } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\n\nimport type { ImageAttrs } from '../image-spec.ts'\n\n/**\n * Returns a command that inserts an image node with the given attributes at the\n * current selection position.\n *\n * @public\n */\nexport function insertImage(attrs?: ImageAttrs): Command {\n return insertNode({ type: 'image', attrs })\n}\n","import { insertNode, ProseKitError } from '@prosekit/core'\nimport type { Command, EditorState, Transaction } from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { UploadTask, type Uploader } from '../../file/index.ts'\nimport type { ImageAttrs } from '../image-spec.ts'\n\n/**\n * Options for {@link uploadImage}.\n *\n * @public\n */\nexport interface UploadImageOptions {\n /**\n * The uploader used to upload the file. It should return a promise that\n * resolves to the URL of the uploaded image.\n */\n uploader: Uploader<string>\n /**\n * The file that will be uploaded.\n */\n file: File\n /**\n * The position where the image should be inserted. If not provided, the\n * image is inserted at the current selection.\n */\n pos?: number\n /**\n * If the image should replace the existing image at the given position. This\n * is only used if `pos` is provided.\n *\n * @default false\n */\n replace?: boolean\n /**\n * A handler to be called when an error occurs during the upload.\n */\n onError?: ImageUploadErrorHandler\n}\n\n/**\n * Options for the {@link ImageUploadErrorHandler} callback.\n *\n * @public\n */\nexport interface ImageUploadErrorHandlerOptions {\n /**\n * The file that was uploaded.\n */\n file: File\n /**\n * The error that occurred during the upload.\n */\n error: unknown\n /**\n * The upload task that was used to upload the file.\n */\n uploadTask: UploadTask<string>\n}\n\n/**\n * A handler to be called when an error occurs during the upload.\n *\n * @public\n */\nexport type ImageUploadErrorHandler = (options: ImageUploadErrorHandlerOptions) => void\n\n/**\n * Returns a command that uploads an image file and inserts an image node with a\n * temporary URL which is replaced once the upload completes.\n *\n * @param options\n *\n * @public\n */\nexport function uploadImage({\n uploader,\n file,\n pos,\n replace = false,\n onError,\n}: UploadImageOptions): Command {\n return (state, dispatch, view) => {\n const uploadTask = new UploadTask({ file, uploader })\n const objectURL = uploadTask.objectURL\n\n uploadTask.finished\n .then((resultURL) => {\n if (view && view.isDestroyed) {\n return\n } else if (typeof resultURL !== 'string') {\n const error = new ProseKitError(\n `Unexpected upload result. Expected a string but got ${typeof resultURL}`,\n )\n onError?.({ file, error, uploadTask })\n } else if (!view) {\n const error = new ProseKitError(\n 'View must be available to replace the image URL',\n )\n onError?.({ file, error, uploadTask })\n } else {\n replaceImageURL(view, objectURL, resultURL)\n UploadTask.delete(objectURL)\n }\n })\n .catch((error) => {\n onError?.({ file, error, uploadTask })\n })\n\n if (replace && pos != null) {\n if (replaceExistingImageURL(state, pos, objectURL, dispatch)) {\n return true\n }\n }\n\n const attrs: ImageAttrs = { src: objectURL }\n return insertNode({ type: 'image', attrs, pos })(state, dispatch, view)\n }\n}\n\nfunction replaceExistingImageURL(\n state: EditorState,\n pos: number,\n imageURL: string,\n dispatch?: (tr: Transaction) => void,\n): boolean {\n const node = state.doc.nodeAt(pos)\n if (!node || node.type.name !== 'image') {\n return false\n }\n const attrs = node.attrs as ImageAttrs\n if (attrs.src === imageURL) {\n return true\n }\n if (dispatch) {\n const tr = state.tr\n tr.setNodeAttribute(pos, 'src', imageURL)\n dispatch(tr)\n }\n return true\n}\n\n/**\n * Replaces the temporary image URL with the final uploaded URL.\n *\n * @internal\n */\nexport function replaceImageURL(\n view: EditorView,\n oldURL: string,\n newURL: string,\n): void {\n const positions: number[] = []\n view.state.doc.descendants((node, pos) => {\n if (node.type.name === 'image') {\n const attrs = node.attrs as ImageAttrs\n if (attrs.src === oldURL) {\n positions.push(pos)\n }\n }\n })\n\n if (positions.length === 0) {\n return\n }\n\n const tr = view.state.tr\n for (const pos of positions) {\n tr.setNodeAttribute(pos, 'src', newURL)\n }\n view.dispatch(tr)\n}\n","import { defineCommands, type Extension } from '@prosekit/core'\n\nimport { insertImage } from './image-commands/insert-image.ts'\nimport { uploadImage, type UploadImageOptions } from './image-commands/upload-image.ts'\nimport type { ImageAttrs } from './image-spec.ts'\n\n/**\n * @internal\n */\nexport type ImageCommandsExtension = Extension<{\n Commands: {\n insertImage: [attrs?: ImageAttrs]\n uploadImage: [options: UploadImageOptions]\n }\n}>\n\n/**\n * @internal\n */\nexport function defineImageCommands(): ImageCommandsExtension {\n return defineCommands({\n insertImage,\n uploadImage,\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\n\n/**\n * @public\n */\nexport interface ImageAttrs {\n src?: string | null\n width?: number | null\n height?: number | null\n}\n\n/**\n * @internal\n */\nexport type ImageSpecExtension = Extension<{\n Nodes: {\n image: ImageAttrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineImageSpec(): ImageSpecExtension {\n return defineNodeSpec({\n name: 'image',\n attrs: {\n src: { default: null, validate: 'string|null' },\n width: { default: null, validate: 'number|null' },\n height: { default: null, validate: 'number|null' },\n },\n group: 'block',\n defining: true,\n draggable: true,\n parseDOM: [\n {\n tag: 'img[src]',\n getAttrs: (element): ImageAttrs => {\n if (typeof element === 'string') {\n return { src: null }\n }\n\n const src = element.getAttribute('src') || null\n\n let width: number | null = null\n let height: number | null = null\n\n const rect = element.getBoundingClientRect()\n if (rect.width > 0 && rect.height > 0) {\n width = rect.width\n height = rect.height\n } else if (\n element instanceof HTMLImageElement\n && element.naturalWidth > 0\n && element.naturalHeight > 0\n ) {\n width = element.naturalWidth\n height = element.naturalHeight\n }\n return { src, width, height }\n },\n },\n ],\n toDOM(node) {\n const attrs = node.attrs as ImageAttrs\n return ['img', attrs]\n },\n })\n}\n","import { union, type PlainExtension } from '@prosekit/core'\n\nimport {\n defineFileDropHandler,\n defineFilePasteHandler,\n type FileDropHandler,\n type FileDropHandlerOptions,\n type FilePasteHandler,\n type FilePasteHandlerOptions,\n type Uploader,\n} from '../file/index.ts'\n\nimport { uploadImage, type ImageUploadErrorHandler } from './image-commands/upload-image.ts'\n\n/**\n * A predicate to determine if the pasted file should be uploaded and inserted as an image.\n */\nexport type ImageCanPastePredicate = (options: FilePasteHandlerOptions) => boolean\n\n/**\n * A predicate to determine if the dropped file should be uploaded and inserted as an image.\n */\nexport type ImageCanDropPredicate = (options: FileDropHandlerOptions) => boolean\n\n/**\n * A handler to be called when an error occurs during the upload.\n */\nexport interface ImageUploadHandlerOptions {\n /**\n * The uploader used to upload the file. It should return a promise that\n * resolves to the URL of the uploaded image.\n */\n uploader: Uploader<string>\n /**\n * A predicate to determine if the pasted file should be uploaded and inserted as an image.\n * If not provided, it defaults to only allowing paste of files with a content type starting with `image/`.\n */\n canPaste?: ImageCanPastePredicate\n /**\n * A predicate to determine if the dropped file should be uploaded and inserted as an image.\n * If not provided, it defaults to only allowing drop of files with a content type starting with `image/`.\n */\n canDrop?: ImageCanDropPredicate\n /**\n * A handler to be called when an error occurs during the upload.\n * If not provided, it defaults to logging the error to the console.\n */\n onError?: ImageUploadErrorHandler\n}\n\nfunction defaultCanUpload({ file }: { file: File }): boolean {\n // Only handle image files by default\n return file.type.startsWith('image/')\n}\n\nconst defaultOnError: ImageUploadErrorHandler = ({ error }) => {\n console.error('[prosekit] Failed to upload image:', error)\n}\n\n/**\n * Returns an extension that handles image file uploads when pasting or dropping\n * images into the editor.\n *\n * @param options\n */\nexport function defineImageUploadHandler({\n uploader,\n canPaste = defaultCanUpload,\n canDrop = defaultCanUpload,\n onError = defaultOnError,\n}: ImageUploadHandlerOptions): PlainExtension {\n const handlePaste: FilePasteHandler = (options) => {\n if (!canPaste(options)) return false\n const { view, file } = options\n const command = uploadImage({ uploader, file, onError })\n return command(view.state, view.dispatch, view)\n }\n\n const handleDrop: FileDropHandler = (options) => {\n if (!canDrop(options)) return false\n const { view, file, pos } = options\n const command = uploadImage({ uploader, file, onError, pos })\n return command(view.state, view.dispatch, view)\n }\n\n return union(\n defineFilePasteHandler(handlePaste),\n defineFileDropHandler(handleDrop),\n )\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineImageCommands, type ImageCommandsExtension } from './image-commands.ts'\nimport { defineImageSpec, type ImageSpecExtension } from './image-spec.ts'\n\n/**\n * @internal\n */\nexport type ImageExtension = Union<[ImageSpecExtension, ImageCommandsExtension]>\n\n/**\n * @public\n */\nexport function defineImage(): ImageExtension {\n return union(defineImageSpec(), defineImageCommands())\n}\n"],"mappings":";;;;;;;;AAWA,SAAgB,YAAY,OAA6B;AACvD,QAAO,WAAW;EAAE,MAAM;EAAS;EAAO,CAAC;;;;;;;;;;AC+D7C,SAAgB,YAAY,EAC1B,UACA,MACA,KACA,UAAU,OACV,WAC8B;AAC9B,SAAQ,OAAO,UAAU,SAAS;E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,YAAY;GAAE;GAAU;GAAM;GAAS,CAAC,CACzC,KAAK,OAAO,KAAK,UAAU,KAAK;;CAGjD,MAAM,cAA+B,YAAY;AAC/C,MAAI,CAAC,QAAQ,QAAQ,CAAE,QAAO;EAC9B,MAAM,EAAE,MAAM,MAAM,QAAQ;AAE5B,SADgB,YAAY;GAAE;GAAU;GAAM;GAAS;GAAK,CAAC,CAC9C,KAAK,OAAO,KAAK,UAAU,KAAK;;AAGjD,QAAO,MACL,uBAAuB,YAAY,EACnC,sBAAsB,WAAW,CAClC;;;;;AC3EH,SAAgB,cAA8B;AAC5C,QAAO,MAAM,iBAAiB,EAAE,qBAAqB,CAAC"}
1
+ {"version":3,"file":"image.js","names":[],"sources":["../src/image/image-commands/insert-image.ts","../src/image/image-commands/upload-image.ts","../src/image/image-commands.ts","../src/image/image-spec.ts","../src/image/image-upload-handler.ts","../src/image/image.ts"],"sourcesContent":["import { insertNode } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\n\nimport type { ImageAttrs } from '../image-spec.ts'\n\n/**\n * Returns a command that inserts an image node with the given attributes at the\n * current selection position.\n *\n * @public\n */\nexport function insertImage(attrs?: ImageAttrs): Command {\n return insertNode({ type: 'image', attrs })\n}\n","import { insertNode, ProseKitError } from '@prosekit/core'\nimport type { Command, EditorState, Transaction } from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { UploadTask, type Uploader } from '../../file/index.ts'\nimport type { ImageAttrs } from '../image-spec.ts'\n\n/**\n * Options for {@link uploadImage}.\n *\n * @public\n */\nexport interface UploadImageOptions {\n /**\n * The uploader used to upload the file. It should return a promise that\n * resolves to the URL of the uploaded image.\n */\n uploader: Uploader<string>\n /**\n * The file that will be uploaded.\n */\n file: File\n /**\n * The position where the image should be inserted. If not provided, the\n * image is inserted at the current selection.\n */\n pos?: number\n /**\n * If the image should replace the existing image at the given position. This\n * is only used if `pos` is provided.\n *\n * @default false\n */\n replace?: boolean\n /**\n * A handler to be called when an error occurs during the upload.\n */\n onError?: ImageUploadErrorHandler\n}\n\n/**\n * Options for the {@link ImageUploadErrorHandler} callback.\n *\n * @public\n */\nexport interface ImageUploadErrorHandlerOptions {\n /**\n * The file that was uploaded.\n */\n file: File\n /**\n * The error that occurred during the upload.\n */\n error: unknown\n /**\n * The upload task that was used to upload the file.\n */\n uploadTask: UploadTask<string>\n}\n\n/**\n * A handler to be called when an error occurs during the upload.\n *\n * @public\n */\nexport type ImageUploadErrorHandler = (options: ImageUploadErrorHandlerOptions) => void\n\n/**\n * Returns a command that uploads an image file and inserts an image node with a\n * temporary URL which is replaced once the upload completes.\n *\n * @param options\n *\n * @public\n */\nexport function uploadImage({\n uploader,\n file,\n pos,\n replace = false,\n onError,\n}: UploadImageOptions): Command {\n return (state, dispatch, view) => {\n const uploadTask = new UploadTask({ file, uploader })\n const objectURL = uploadTask.objectURL\n\n uploadTask.finished\n .then((resultURL) => {\n if (view && view.isDestroyed) {\n return\n } else if (typeof resultURL !== 'string') {\n const error = new ProseKitError(\n `Unexpected upload result. Expected a string but got ${typeof resultURL}`,\n )\n onError?.({ file, error, uploadTask })\n } else if (!view) {\n const error = new ProseKitError(\n 'View must be available to replace the image URL',\n )\n onError?.({ file, error, uploadTask })\n } else {\n replaceImageURL(view, objectURL, resultURL)\n UploadTask.delete(objectURL)\n }\n })\n .catch((error) => {\n onError?.({ file, error, uploadTask })\n })\n\n if (replace && pos != null) {\n if (replaceExistingImageURL(state, pos, objectURL, dispatch)) {\n return true\n }\n }\n\n const attrs: ImageAttrs = { src: objectURL }\n return insertNode({ type: 'image', attrs, pos })(state, dispatch, view)\n }\n}\n\nfunction replaceExistingImageURL(\n state: EditorState,\n pos: number,\n imageURL: string,\n dispatch?: (tr: Transaction) => void,\n): boolean {\n const node = state.doc.nodeAt(pos)\n if (!node || node.type.name !== 'image') {\n return false\n }\n const attrs = node.attrs as ImageAttrs\n if (attrs.src === imageURL) {\n return true\n }\n if (dispatch) {\n const tr = state.tr\n tr.setNodeAttribute(pos, 'src', imageURL)\n dispatch(tr)\n }\n return true\n}\n\n/**\n * Replaces the temporary image URL with the final uploaded URL.\n *\n * @internal\n */\nexport function replaceImageURL(\n view: EditorView,\n oldURL: string,\n newURL: string,\n): void {\n const positions: number[] = []\n view.state.doc.descendants((node, pos) => {\n if (node.type.name === 'image') {\n const attrs = node.attrs as ImageAttrs\n if (attrs.src === oldURL) {\n positions.push(pos)\n }\n }\n })\n\n if (positions.length === 0) {\n return\n }\n\n const tr = view.state.tr\n for (const pos of positions) {\n tr.setNodeAttribute(pos, 'src', newURL)\n }\n view.dispatch(tr)\n}\n","import { defineCommands, type Extension } from '@prosekit/core'\n\nimport { insertImage } from './image-commands/insert-image.ts'\nimport { uploadImage, type UploadImageOptions } from './image-commands/upload-image.ts'\nimport type { ImageAttrs } from './image-spec.ts'\n\n/**\n * @internal\n */\nexport type ImageCommandsExtension = Extension<{\n Commands: {\n insertImage: [attrs?: ImageAttrs]\n uploadImage: [options: UploadImageOptions]\n }\n}>\n\n/**\n * @internal\n */\nexport function defineImageCommands(): ImageCommandsExtension {\n return defineCommands({\n insertImage,\n uploadImage,\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\n\n/**\n * @public\n */\nexport interface ImageAttrs {\n src?: string | null\n width?: number | null\n height?: number | null\n}\n\n/**\n * @internal\n */\nexport type ImageSpecExtension = Extension<{\n Nodes: {\n image: ImageAttrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineImageSpec(): ImageSpecExtension {\n return defineNodeSpec({\n name: 'image',\n attrs: {\n src: { default: null, validate: 'string|null' },\n width: { default: null, validate: 'number|null' },\n height: { default: null, validate: 'number|null' },\n },\n group: 'block',\n defining: true,\n draggable: true,\n parseDOM: [\n {\n tag: 'img[src]',\n getAttrs: (element): ImageAttrs => {\n if (typeof element === 'string') {\n return { src: null }\n }\n\n const src = element.getAttribute('src') || null\n\n let width: number | null = null\n let height: number | null = null\n\n const rect = element.getBoundingClientRect()\n if (rect.width > 0 && rect.height > 0) {\n width = rect.width\n height = rect.height\n } else if (\n element instanceof HTMLImageElement\n && element.naturalWidth > 0\n && element.naturalHeight > 0\n ) {\n width = element.naturalWidth\n height = element.naturalHeight\n }\n return { src, width, height }\n },\n },\n ],\n toDOM(node) {\n const attrs = node.attrs as ImageAttrs\n return ['img', attrs]\n },\n })\n}\n","import { union, type PlainExtension } from '@prosekit/core'\n\nimport {\n defineFileDropHandler,\n defineFilePasteHandler,\n type FileDropHandler,\n type FileDropHandlerOptions,\n type FilePasteHandler,\n type FilePasteHandlerOptions,\n type Uploader,\n} from '../file/index.ts'\n\nimport { uploadImage, type ImageUploadErrorHandler } from './image-commands/upload-image.ts'\n\n/**\n * A predicate to determine if the pasted file should be uploaded and inserted as an image.\n */\nexport type ImageCanPastePredicate = (options: FilePasteHandlerOptions) => boolean\n\n/**\n * A predicate to determine if the dropped file should be uploaded and inserted as an image.\n */\nexport type ImageCanDropPredicate = (options: FileDropHandlerOptions) => boolean\n\n/**\n * A handler to be called when an error occurs during the upload.\n */\nexport interface ImageUploadHandlerOptions {\n /**\n * The uploader used to upload the file. It should return a promise that\n * resolves to the URL of the uploaded image.\n */\n uploader: Uploader<string>\n /**\n * A predicate to determine if the pasted file should be uploaded and inserted as an image.\n * If not provided, it defaults to only allowing paste of files with a content type starting with `image/`.\n */\n canPaste?: ImageCanPastePredicate\n /**\n * A predicate to determine if the dropped file should be uploaded and inserted as an image.\n * If not provided, it defaults to only allowing drop of files with a content type starting with `image/`.\n */\n canDrop?: ImageCanDropPredicate\n /**\n * A handler to be called when an error occurs during the upload.\n * If not provided, it defaults to logging the error to the console.\n */\n onError?: ImageUploadErrorHandler\n}\n\nfunction defaultCanUpload({ file }: { file: File }): boolean {\n // Only handle image files by default\n return file.type.startsWith('image/')\n}\n\nconst defaultOnError: ImageUploadErrorHandler = ({ error }) => {\n console.error('[prosekit] Failed to upload image:', error)\n}\n\n/**\n * Returns an extension that handles image file uploads when pasting or dropping\n * images into the editor.\n *\n * @param options\n */\nexport function defineImageUploadHandler({\n uploader,\n canPaste = defaultCanUpload,\n canDrop = defaultCanUpload,\n onError = defaultOnError,\n}: ImageUploadHandlerOptions): PlainExtension {\n const handlePaste: FilePasteHandler = (options) => {\n if (!canPaste(options)) return false\n const { view, file } = options\n const command = uploadImage({ uploader, file, onError })\n return command(view.state, view.dispatch, view)\n }\n\n const handleDrop: FileDropHandler = (options) => {\n if (!canDrop(options)) return false\n const { view, file, pos } = options\n const command = uploadImage({ uploader, file, onError, pos })\n return command(view.state, view.dispatch, view)\n }\n\n return union(\n defineFilePasteHandler(handlePaste),\n defineFileDropHandler(handleDrop),\n )\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineImageCommands, type ImageCommandsExtension } from './image-commands.ts'\nimport { defineImageSpec, type ImageSpecExtension } from './image-spec.ts'\n\n/**\n * @internal\n */\nexport type ImageExtension = Union<[ImageSpecExtension, ImageCommandsExtension]>\n\n/**\n * @public\n */\nexport function defineImage(): ImageExtension {\n return union(defineImageSpec(), defineImageCommands())\n}\n"],"mappings":";;;;;;;;AAWA,SAAgB,YAAY,OAA6B;AACvD,QAAO,WAAW;EAAE,MAAM;EAAS;EAAO,CAAC;;;;;;;;;;AC+D7C,SAAgB,YAAY,EAC1B,UACA,MACA,KACA,UAAU,OACV,WAC8B;AAC9B,SAAQ,OAAO,UAAU,SAAS;EAChC,MAAM,aAAa,IAAI,WAAW;GAAE;GAAM;GAAU,CAAC;EACrD,MAAM,YAAY,WAAW;AAE7B,aAAW,SACR,MAAM,cAAc;AACnB,OAAI,QAAQ,KAAK,YACf;YACS,OAAO,cAAc,UAAU;IACxC,MAAM,QAAQ,IAAI,cAChB,uDAAuD,OAAO,YAC/D;AACD,cAAU;KAAE;KAAM;KAAO;KAAY,CAAC;cAC7B,CAAC,MAAM;IAChB,MAAM,QAAQ,IAAI,cAChB,kDACD;AACD,cAAU;KAAE;KAAM;KAAO;KAAY,CAAC;UACjC;AACL,oBAAgB,MAAM,WAAW,UAAU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AiB,EAAE,qBAAqB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"input-rule.js","names":[],"sources":["../src/input-rule/index.ts"],"sourcesContent":["import {\n defineFacet,\n defineFacetPayload,\n getMarkType,\n getNodeType,\n isMarkAbsent,\n maybeRun,\n pluginFacet,\n type PlainExtension,\n type PluginPayload,\n} from '@prosekit/core'\nimport { InputRule, inputRules, textblockTypeInputRule, wrappingInputRule } from '@prosekit/pm/inputrules'\nimport type { Attrs, MarkType, NodeType, ProseMirrorNode, Schema } from '@prosekit/pm/model'\nimport type { Plugin } from '@prosekit/pm/state'\n\n/**\n * Defines an input rule extension.\n *\n * @param rule - The ProseMirror input rule to add.\n *\n * @public\n */\nexport function defineInputRule(rule: InputRule): PlainExtension {\n return defineInputRuleFacetPayload(() => rule)\n}\n\n/**\n * Options for {@link defineMarkInputRule}.\n *\n * @public\n */\nexport interface MarkInputRuleOptions {\n /**\n * The regular expression to match against, which should end with `$` and has\n * exactly one capture group. All other matched text outside the capture group\n * will be deleted.\n */\n regex: RegExp\n\n /**\n * The type of mark to set.\n */\n type: string | MarkType\n\n /**\n * Attributes to set on the mark.\n */\n attrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null)\n\n /**\n * Whether this rule should fire inside marks marked as [code](https://prosemirror.net/docs/ref/#model.MarkSpec.code).\n *\n * @default `false`\n */\n inCodeMark?: boolean\n}\n\n/**\n * @internal\n */\nexport function createMarkInputRule({\n regex,\n type,\n attrs = null,\n inCodeMark = false,\n}: MarkInputRuleOptions): InputRule {\n const rule = new InputRule(regex, (state, match, start, end) => {\n const { tr, schema } = state\n const [fullText, markText] = match\n\n if (!markText) {\n return null\n }\n\n const markStart = start + fullText.indexOf(markText)\n const markEnd = markStart + markText.length\n\n if (!(start <= markStart && markStart < markEnd && markEnd <= end)) {\n // Incorrect regex.\n return null\n }\n\n const markType = getMarkType(schema, type)\n const mark = markType.create(maybeRun(attrs, match))\n\n if (!isMarkAbsent(tr.doc, markStart, markEnd, markType, attrs)) {\n // The mark is already active.\n return null\n }\n\n const initialStoredMarks = tr.storedMarks ?? []\n\n tr.addMark(markStart, markEnd, mark)\n\n if (markEnd < end) {\n tr.delete(markEnd, end)\n }\n if (start < markStart) {\n tr.delete(start, markStart)\n }\n\n // Make sure not to reactivate any marks which had previously been\n // deactivated. By keeping track of the initial stored marks we are able to\n // discard any unintended consequences of deleting text and adding it again.\n tr.setStoredMarks(initialStoredMarks)\n\n return tr\n }, { inCodeMark })\n\n return rule\n}\n\n/**\n * Defines an input rule for automatically adding inline marks when a given\n * pattern is typed.\n *\n * @public\n */\nexport function defineMarkInputRule(\n options: MarkInputRuleOptions,\n): PlainExtension {\n return defineInputRule(createMarkInputRule(options))\n}\n\n/**\n * Defines an input rule that changes the type of a textblock when the matched\n * text is typed into it.\n *\n * See also [textblockTypeInputRule](https://prosemirror.net/docs/ref/#inputrules.textblockTypeInputRule)\n *\n * @param options\n *\n * @public\n */\nexport function defineTextBlockInputRule({\n regex,\n type,\n attrs,\n}: {\n /**\n * The regular expression to match against, which should end with `$`. It\n * usually also starts with `^` to that it is only matched at the start of a\n * textblock.\n */\n regex: RegExp\n\n /**\n * The node type to replace the matched text with.\n */\n type: string | NodeType\n\n /**\n * Attributes to set on the node.\n */\n attrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null)\n}): PlainExtension {\n return defineInputRuleFacetPayload(({ schema }): InputRule => {\n const nodeType = getNodeType(schema, type)\n return textblockTypeInputRule(regex, nodeType, attrs)\n })\n}\n\n/**\n * Defines an input rule for automatically wrapping a textblock when a given\n * string is typed.\n *\n * See also [wrappingInputRule](https://prosemirror.net/docs/ref/#inputrules.wrappingInputRule)\n *\n * @param options\n *\n * @public\n */\nexport function defineWrappingInputRule({\n regex,\n type,\n attrs,\n join,\n}: {\n /**\n * The regular expression to match against, which should end with `$`. It\n * usually also starts with `^` to that it is only matched at the start of a\n * textblock.\n */\n regex: RegExp\n\n /**\n * The type of node to wrap in.\n */\n type: string | NodeType\n\n /**\n * Attributes to set on the node.\n */\n attrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null)\n\n /**\n * By default, if there's a node with the same type above the newly wrapped\n * node, the rule will try to\n * [join](https://prosemirror.net/docs/ref/#transform.Transform.join) those\n * two nodes. You can pass a join predicate, which takes a regular expression\n * match and the node before the wrapped node, and can return a boolean to\n * indicate whether a join should happen.\n */\n join?: (match: RegExpMatchArray, node: ProseMirrorNode) => boolean\n}): PlainExtension {\n return defineInputRuleFacetPayload(({ schema }): InputRule => {\n const nodeType = getNodeType(schema, type)\n return wrappingInputRule(regex, nodeType, attrs, join)\n })\n}\n\nfunction defineInputRuleFacetPayload(input: InputRulePayload): PlainExtension {\n return defineFacetPayload(inputRuleFacet, [input]) as PlainExtension\n}\n\ntype InputRulePayload = (context: { schema: Schema }) => InputRule\n\nconst inputRuleFacet = defineFacet<InputRulePayload, PluginPayload>({\n reducer: (inputs: InputRulePayload[]): PluginPayload => {\n return (context): Plugin[] => {\n const rules: InputRule[] = inputs.flatMap((callback) => callback(context))\n return [inputRules({ rules })]\n }\n },\n parent: pluginFacet,\n})\n"],"mappings":";;;;;;;;;AAsBA,SAAgB,gBAAgB,MAAiC;AAC/D,QAAO,kCAAkC,KAAK;;;;;AAqChD,SAAgB,oBAAoB,EAClC,OACA,MACA,QAAQ,MACR,aAAa,SACqB;AA4ClC,QA3Ca,IAAI,UAAU,QAAQ,OAAO,OAAO,OAAO,QAAQ;EAC9D,MAAM,EAAE,IAAI,WAAW;EACvB,MAAM,CAAC,UAAU,YAAY;AAE7B,MAAI,CAAC,SACH,QAAO;EAGT,MAAM,YAAY,QAAQ,SAAS,QAAQ,SAAS;EACpD,MAAM,UAAU,YAAY,SAAS;AAErC,MAAI,EAAE,SAAS,aAAa,YAAY,WAAW,WAAW,KAE5D,QAAO;EAGT,MAAM,WAAW,YAAY,QAAQ,KAAK;EAC1C,MAAM,OAAO,SAAS,OAAO,SAAS,OAAO,MAAM,CAAC;AAEpD,MAAI,CAAC,aAAa,GAAG,KAAK,WAAW,SAAS,UAAU,MAAM,CAE5D,QAAO;EAGT,MAAM,qBAAqB,GAAG,eAAe,EAAE;AAE/C,KAAG,QAAQ,WAAW,SAAS,KAAK;AAEpC,MAAI,UAAU,IACZ,IAAG,OAAO,SAAS,IAAI;AAEzB,MAAI,QAAQ,UACV,IAAG,OAAO,OAAO,UAAU;AAM7B,KAAG,eAAe,mBAAmB;AAErC,SAAO;IACN,EAAE,YAAY,CAAC;;;;;;;;AAWpB,SAAgB,oBACd,SACgB;AAChB,QAAO,gBAAgB,oBAAoB,QAAQ,CAAC;;;;;;;;;;;;AAatD,SAAgB,yBAAyB,EACvC,OACA,MACA,SAkBiB;AACjB,QAAO,6BAA6B,EAAE,aAAwB;AAE5D,SAAO,uBAAuB,OADb,YAAY,QAAQ,KAAK,EACK,MAAM;GACrD;;;;;;;;;;;;AAaJ,SAAgB,wBAAwB,EACtC,OACA,MACA,OACA,QA4BiB;AACjB,QAAO,6BAA6B,EAAE,aAAwB;AAE5D,SAAO,kBAAkB,OADR,YAAY,QAAQ,KAAK,EACA,OAAO,KAAK;GACtD;;AAGJ,SAAS,4BAA4B,OAAyC;AAC5E,QAAO,mBAAmB,gBAAgB,CAAC,MAAM,CAAC;;AAKpD,MAAM,iBAAiB,YAA6C;CAClE,UAAU,WAA8C;AACtD,UAAQ,YAAsB;AAE5B,UAAO,CAAC,WAAW,EAAE,OADM,OAAO,SAAS,aAAa,SAAS,QAAQ,CAAC,EAC9C,CAAC,CAAC;;;CAGlC,QAAQ;CACT,CAAC"}
1
+ {"version":3,"file":"input-rule.js","names":[],"sources":["../src/input-rule/index.ts"],"sourcesContent":["import {\n defineFacet,\n defineFacetPayload,\n getMarkType,\n getNodeType,\n isMarkAbsent,\n maybeRun,\n pluginFacet,\n type PlainExtension,\n type PluginPayload,\n} from '@prosekit/core'\nimport { InputRule, inputRules, textblockTypeInputRule, wrappingInputRule } from '@prosekit/pm/inputrules'\nimport type { Attrs, MarkType, NodeType, ProseMirrorNode, Schema } from '@prosekit/pm/model'\nimport type { Plugin } from '@prosekit/pm/state'\n\n/**\n * Defines an input rule extension.\n *\n * @param rule - The ProseMirror input rule to add.\n *\n * @public\n */\nexport function defineInputRule(rule: InputRule): PlainExtension {\n return defineInputRuleFacetPayload(() => rule)\n}\n\n/**\n * Options for {@link defineMarkInputRule}.\n *\n * @public\n */\nexport interface MarkInputRuleOptions {\n /**\n * The regular expression to match against, which should end with `$` and has\n * exactly one capture group. All other matched text outside the capture group\n * will be deleted.\n */\n regex: RegExp\n\n /**\n * The type of mark to set.\n */\n type: string | MarkType\n\n /**\n * Attributes to set on the mark.\n */\n attrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null)\n\n /**\n * Whether this rule should fire inside marks marked as [code](https://prosemirror.net/docs/ref/#model.MarkSpec.code).\n *\n * @default `false`\n */\n inCodeMark?: boolean\n}\n\n/**\n * @internal\n */\nexport function createMarkInputRule({\n regex,\n type,\n attrs = null,\n inCodeMark = false,\n}: MarkInputRuleOptions): InputRule {\n const rule = new InputRule(regex, (state, match, start, end) => {\n const { tr, schema } = state\n const [fullText, markText] = match\n\n if (!markText) {\n return null\n }\n\n const markStart = start + fullText.indexOf(markText)\n const markEnd = markStart + markText.length\n\n if (!(start <= markStart && markStart < markEnd && markEnd <= end)) {\n // Incorrect regex.\n return null\n }\n\n const markType = getMarkType(schema, type)\n const mark = markType.create(maybeRun(attrs, match))\n\n if (!isMarkAbsent(tr.doc, markStart, markEnd, markType, attrs)) {\n // The mark is already active.\n return null\n }\n\n const initialStoredMarks = tr.storedMarks ?? []\n\n tr.addMark(markStart, markEnd, mark)\n\n if (markEnd < end) {\n tr.delete(markEnd, end)\n }\n if (start < markStart) {\n tr.delete(start, markStart)\n }\n\n // Make sure not to reactivate any marks which had previously been\n // deactivated. By keeping track of the initial stored marks we are able to\n // discard any unintended consequences of deleting text and adding it again.\n tr.setStoredMarks(initialStoredMarks)\n\n return tr\n }, { inCodeMark })\n\n return rule\n}\n\n/**\n * Defines an input rule for automatically adding inline marks when a given\n * pattern is typed.\n *\n * @public\n */\nexport function defineMarkInputRule(\n options: MarkInputRuleOptions,\n): PlainExtension {\n return defineInputRule(createMarkInputRule(options))\n}\n\n/**\n * Defines an input rule that changes the type of a textblock when the matched\n * text is typed into it.\n *\n * See also [textblockTypeInputRule](https://prosemirror.net/docs/ref/#inputrules.textblockTypeInputRule)\n *\n * @param options\n *\n * @public\n */\nexport function defineTextBlockInputRule({\n regex,\n type,\n attrs,\n}: {\n /**\n * The regular expression to match against, which should end with `$`. It\n * usually also starts with `^` to that it is only matched at the start of a\n * textblock.\n */\n regex: RegExp\n\n /**\n * The node type to replace the matched text with.\n */\n type: string | NodeType\n\n /**\n * Attributes to set on the node.\n */\n attrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null)\n}): PlainExtension {\n return defineInputRuleFacetPayload(({ schema }): InputRule => {\n const nodeType = getNodeType(schema, type)\n return textblockTypeInputRule(regex, nodeType, attrs)\n })\n}\n\n/**\n * Defines an input rule for automatically wrapping a textblock when a given\n * string is typed.\n *\n * See also [wrappingInputRule](https://prosemirror.net/docs/ref/#inputrules.wrappingInputRule)\n *\n * @param options\n *\n * @public\n */\nexport function defineWrappingInputRule({\n regex,\n type,\n attrs,\n join,\n}: {\n /**\n * The regular expression to match against, which should end with `$`. It\n * usually also starts with `^` to that it is only matched at the start of a\n * textblock.\n */\n regex: RegExp\n\n /**\n * The type of node to wrap in.\n */\n type: string | NodeType\n\n /**\n * Attributes to set on the node.\n */\n attrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null)\n\n /**\n * By default, if there's a node with the same type above the newly wrapped\n * node, the rule will try to\n * [join](https://prosemirror.net/docs/ref/#transform.Transform.join) those\n * two nodes. You can pass a join predicate, which takes a regular expression\n * match and the node before the wrapped node, and can return a boolean to\n * indicate whether a join should happen.\n */\n join?: (match: RegExpMatchArray, node: ProseMirrorNode) => boolean\n}): PlainExtension {\n return defineInputRuleFacetPayload(({ schema }): InputRule => {\n const nodeType = getNodeType(schema, type)\n return wrappingInputRule(regex, nodeType, attrs, join)\n })\n}\n\nfunction defineInputRuleFacetPayload(input: InputRulePayload): PlainExtension {\n return defineFacetPayload(inputRuleFacet, [input]) as PlainExtension\n}\n\ntype InputRulePayload = (context: { schema: Schema }) => InputRule\n\nconst inputRuleFacet = defineFacet<InputRulePayload, PluginPayload>({\n reducer: (inputs: InputRulePayload[]): PluginPayload => {\n return (context): Plugin[] => {\n const rules: InputRule[] = inputs.flatMap((callback) => callback(context))\n return [inputRules({ rules })]\n }\n },\n parent: pluginFacet,\n})\n"],"mappings":";;;;;;;;;AAsBA,SAAgB,gBAAgB,MAAiC;AAC/D,QAAO,kCAAkC,KAAK;;;;;AAqChD,SAAgB,oBAAoB,EAClC,OACA,MACA,QAAQ,MACR,aAAa,SACqB;AA4ClC,QAAO,IA3CU,UAAU,QAAQ,OAAO,OAAO,OAAO,QAAQ;EAC9D,MAAM,EAAE,IAAI,WAAW;EACvB,MAAM,CAAC,UAAU,YAAY;AAE7B,MAAI,CAAC,SACH,QAAO;EAGT,MAAM,YAAY,QAAQ,SAAS,QAAQ,SAAS;EACpD,MAAM,UAAU,YAAY,SAAS;AAErC,MAAI,EAAE,SAAS,aAAa,YAAY,WAAW,WAAW,KAE5D,QAAO;EAGT,MAAM,WAAW,YAAY,QAAQ,KAAK;EAC1C,MAAM,OAAO,SAAS,OAAO,SAAS,OAAO,MAAM,CAAC;AAEpD,MAAI,CAAC,aAAa,GAAG,KAAK,WAAW,SAAS,UAAU,MAAM,CAE5D,QAAO;EAGT,MAAM,qBAAqB,GAAG,eAAe,EAAE;AAE/C,KAAG,QAAQ,WAAW,SAAS,KAAK;AAEpC,MAAI,UAAU,IACZ,IAAG,OAAO,SAAS,IAAI;AAEzB,MAAI,QAAQ,UACV,IAAG,OAAO,OAAO,UAAU;AAM7B,KAAG,eAAe,mBAAmB;AAErC,SAAO;IACN,EAAE,YAAY,CAEN;;;;;;;;AASb,SAAgB,oBACd,SACgB;AAChB,QAAO,gBAAgB,oBAAoB,QAAQ,CAAC;;;;;;;;;;;;AAatD,SAAgB,yBAAyB,EACvC,OACA,MACA,SAkBiB;AACjB,QAAO,6BAA6B,EAAE,aAAwB;AAE5D,SAAO,uBAAuB,OADb,YAAY,QAAQ,KACQ,EAAE,MAAM;GACrD;;;;;;;;;;;;AAaJ,SAAgB,wBAAwB,EACtC,OACA,MACA,OACA,QA4BiB;AACjB,QAAO,6BAA6B,EAAE,aAAwB;AAE5D,SAAO,kBAAkB,OADR,YAAY,QAAQ,KACG,EAAE,OAAO,KAAK;GACtD;;AAGJ,SAAS,4BAA4B,OAAyC;AAC5E,QAAO,mBAAmB,gBAAgB,CAAC,MAAM,CAAC;;AAKpD,MAAM,iBAAiB,YAA6C;CAClE,UAAU,WAA8C;AACtD,UAAQ,YAAsB;AAE5B,UAAO,CAAC,WAAW,EAAE,OADM,OAAO,SAAS,aAAa,SAAS,QAAQ,CAC/C,EAAE,CAAC,CAAC;;;CAGlC,QAAQ;CACT,CAAC"}
package/dist/list.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"list.js","names":[],"sources":["../src/list/list-commands.ts","../src/list/list-input-rules.ts","../src/list/list-keymap.ts","../src/list/list-plugins.ts","../src/list/list-serializer.ts","../src/list/list-spec.ts","../src/list/list-drop-indicator.ts","../src/list/list.ts"],"sourcesContent":["import { defineCommands, insertNode, type Extension } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\nimport {\n createDedentListCommand as dedentList,\n createIndentListCommand as indentList,\n createMoveListCommand as moveList,\n createSplitListCommand as splitList,\n createToggleCollapsedCommand as toggleCollapsed,\n createToggleListCommand as toggleList,\n createUnwrapListCommand as unwrapList,\n createWrapInListCommand as wrapInList,\n type DedentListOptions,\n type IndentListOptions,\n type ListAttributes,\n type ToggleCollapsedOptions,\n type UnwrapListOptions,\n} from 'prosemirror-flat-list'\n\nfunction insertList(attrs?: ListAttributes): Command {\n return insertNode({ type: 'list', attrs })\n}\n\n/**\n * @internal\n */\nexport type ListCommandsExtension = Extension<{\n Commands: {\n dedentList: [options?: DedentListOptions]\n indentList: [options?: IndentListOptions]\n moveList: [direction: 'up' | 'down']\n splitList: []\n toggleCollapsed: [options?: ToggleCollapsedOptions]\n unwrapList: [options?: UnwrapListOptions]\n toggleList: [attrs?: ListAttributes]\n wrapInList: [attrs?: ListAttributes]\n insertList: [attrs?: ListAttributes]\n }\n}>\n\n/**\n * Defines list commands.\n *\n * @internal\n */\nexport function defineListCommands(): ListCommandsExtension {\n return defineCommands({\n dedentList,\n indentList,\n moveList,\n splitList,\n toggleCollapsed,\n unwrapList,\n toggleList,\n wrapInList,\n insertList,\n })\n}\n","import { union, type Extension } from '@prosekit/core'\nimport { listInputRules } from 'prosemirror-flat-list'\n\nimport { defineInputRule } from '../input-rule/index.ts'\n\n/**\n * @internal\n */\nexport function defineListInputRules(): Extension {\n return union(listInputRules.map(defineInputRule))\n}\n","import { defineKeymap, type PlainExtension } from '@prosekit/core'\nimport { chainCommands, deleteSelection } from '@prosekit/pm/commands'\nimport {\n createDedentListCommand,\n createIndentListCommand,\n deleteCommand,\n enterCommand,\n joinCollapsedListBackward,\n joinListUp,\n protectCollapsed,\n} from 'prosemirror-flat-list'\n\n// This is different from the one exported by prosemirror-flat-list, because\n// some commands are moved to `defineBaseKeymap` in `prosekit/core`.\nconst backspaceCommand = chainCommands(\n protectCollapsed,\n deleteSelection,\n joinListUp,\n joinCollapsedListBackward,\n)\n\nconst dedentListCommand = createDedentListCommand()\nconst indentListCommand = createIndentListCommand()\n\nconst listKeymap = {\n 'Enter': enterCommand,\n 'Backspace': backspaceCommand,\n 'Delete': deleteCommand,\n 'Mod-]': indentListCommand,\n 'Mod-[': dedentListCommand,\n 'Tab': indentListCommand,\n 'Shift-Tab': dedentListCommand,\n}\n\n/**\n * Returns a extension that adds key bindings for list.\n *\n * @internal\n */\nexport function defineListKeymap(): PlainExtension {\n return defineKeymap(listKeymap)\n}\n","import { definePlugin, type PlainExtension } from '@prosekit/core'\nimport { Plugin } from '@prosekit/pm/state'\nimport {\n createListEventPlugin,\n createListRenderingPlugin,\n createSafariInputMethodWorkaroundPlugin,\n unwrapListSlice,\n} from 'prosemirror-flat-list'\n\nfunction createListClipboardPlugin(): Plugin {\n return new Plugin({\n props: {\n transformCopied: unwrapListSlice,\n },\n })\n}\n\nfunction createListPlugins(): Plugin[] {\n return [\n createListEventPlugin(),\n createListRenderingPlugin(),\n createListClipboardPlugin(),\n createSafariInputMethodWorkaroundPlugin(),\n ]\n}\n\n/**\n * @internal\n */\nexport function defineListPlugins(): PlainExtension {\n return definePlugin(createListPlugins)\n}\n","import { isElementLike } from '@ocavue/utils'\nimport { defineClipboardSerializer, type PlainExtension } from '@prosekit/core'\nimport { findCheckboxInListItem, joinListElements, listToDOM } from 'prosemirror-flat-list'\n\n/**\n * @internal\n */\nexport function defineListSerializer(): PlainExtension {\n return defineClipboardSerializer({\n serializeFragmentWrapper: (fn) => {\n return (...args) => {\n const dom = fn(...args)\n return normalizeElementTree(joinListElements(dom))\n }\n },\n serializeNodeWrapper: (fn) => {\n return (...args) => {\n const dom = fn(...args)\n return isElementLike(dom)\n ? normalizeElementTree(joinListElements(dom))\n : dom\n }\n },\n nodesFromSchemaWrapper: (fn) => {\n return (...args) => {\n const nodes = fn(...args)\n return {\n ...nodes,\n list: (node) => listToDOM({ node, nativeList: true }),\n }\n }\n },\n })\n}\n\nfunction normalizeElementTree<T extends Element | DocumentFragment>(\n node: T,\n): T {\n if (isElementLike(node)) {\n normalizeTaskList(node)\n }\n\n for (const child of node.children) {\n normalizeElementTree(child)\n }\n\n return node\n}\n\n/**\n * Modifies the DOM tree for task lists to ensure that the output HTML can be\n * parsed by rehype-remark.\n */\nfunction normalizeTaskList(node: Element): void {\n if (\n !node.classList.contains('prosemirror-flat-list')\n || node.getAttribute('data-list-kind') !== 'task'\n || node.children.length !== 2\n ) {\n return\n }\n\n const marker = node.children.item(0)\n if (!marker || !marker.classList.contains('list-marker')) {\n return\n }\n\n const checkbox = findCheckboxInListItem(marker)\n if (!checkbox) {\n return\n }\n\n const content = node.children.item(1)\n if (!content || !content.classList.contains('list-content')) {\n return\n }\n\n const textBlock = content.children.item(0)\n if (!textBlock || !['P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(textBlock.tagName)) {\n return\n }\n\n node.replaceChildren(...content.children)\n textBlock.prepend(checkbox)\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { DOMOutputSpec, ProseMirrorNode } from '@prosekit/pm/model'\nimport { createListSpec, listToDOM, type ListAttributes } from 'prosemirror-flat-list'\n\nimport type { ListAttrs } from './list-types.ts'\n\n/**\n * @internal\n */\nexport type ListSpecExtension = Extension<{\n Nodes: {\n list: ListAttrs\n }\n}>\n\nfunction getMarkers(node: ProseMirrorNode): DOMOutputSpec[] {\n const attrs = node.attrs as ListAttributes\n switch (attrs.kind) {\n case 'task':\n // Use a `label` element here so that the area around the checkbox is also checkable.\n return [\n [\n 'label',\n [\n 'input',\n { type: 'checkbox', checked: attrs.checked ? '' : undefined },\n ],\n ],\n ]\n default:\n // Always return an empty array so that the marker element is rendered. This\n // is required to make the drop indicator locate the correct position.\n return []\n }\n}\n\n/**\n * @internal\n */\nexport function defineListSpec(): ListSpecExtension {\n const spec = createListSpec()\n\n return defineNodeSpec<'list', ListAttrs>({\n ...spec,\n toDOM: (node) => {\n return listToDOM({ node, getMarkers })\n },\n name: 'list',\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport type { DragEventHandler } from '../drop-indicator/index.ts'\nimport { defineDropIndicator } from '../drop-indicator/index.ts'\n\n/**\n * Configures drop indicator to avoid unexpected drop point.\n *\n * We don't want to drag a list node and drop it as the first\n * child of another list node.\n *\n * @internal\n */\nexport function defineListDropIndicator(): PlainExtension {\n return defineDropIndicator({\n onDrag,\n })\n}\n\nconst onDrag: DragEventHandler = ({ view, pos }): boolean => {\n const slice = view.dragging?.slice\n if (\n slice\n && slice.openStart === 0\n && slice.openEnd === 0\n && slice.content.childCount === 1\n ) {\n const node = slice.content.child(0)\n if (node.type.name === 'list') {\n const $pos = view.state.doc.resolve(pos)\n if ($pos.parent.type.name === 'list' && $pos.index() === 0) {\n return false\n }\n }\n }\n return true\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineListCommands, type ListCommandsExtension } from './list-commands.ts'\nimport { defineListDropIndicator } from './list-drop-indicator.ts'\nimport { defineListInputRules } from './list-input-rules.ts'\nimport { defineListKeymap } from './list-keymap.ts'\nimport { defineListPlugins } from './list-plugins.ts'\nimport { defineListSerializer } from './list-serializer.ts'\nimport { defineListSpec, type ListSpecExtension } from './list-spec.ts'\n\n/**\n * @internal\n */\nexport type ListExtension = Union<[ListSpecExtension, ListCommandsExtension]>\n\n/**\n * @public\n */\nexport function defineList(): ListExtension {\n return union(\n defineListSpec(),\n defineListPlugins(),\n defineListKeymap(),\n defineListInputRules(),\n defineListCommands(),\n defineListSerializer(),\n defineListDropIndicator(),\n )\n}\n"],"mappings":";;;;;;;AAkBA,SAAS,WAAW,OAAiC;AACnD,QAAO,WAAW;EAAE,MAAM;EAAQ;EAAO,CAAC;;;;;;;AAyB5C,SAAgB,qBAA4C;AAC1D,QAAO,eAAe;EACpB,YAAA;EACA,YAAA;EACA,UAAA;EACA,WAAA;EACA,iBAAA;EACA,YAAA;EACA,YAAA;EACA,YAAA;EACA;EACD,CAAC;;;;;AC/CJ,SAAgB,uBAAkC;AAChD,QAAO,MAAM,eAAe,IAAI,gBAAgB,CAAC;;ACKnD,MAAM,mBAAmB,cACvB,kBACA,iBACA,YACA,0BACD;AAED,MAAM,oBAAoB,yBAAyB;AACnD,MAAM,oBAAoB,yBAAyB;AAEnD,MAAM,aAAa;CACjB,SAAS;CACT,aAAa;CACb,UAAU;CACV,SAAS;CACT,SAAS;CACT,OAAO;CACP,aAAa;CACd;;;;;;AAOD,SAAgB,mBAAmC;AACjD,QAAO,aAAa,WAAW;;AC/BjC,SAAS,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ts","../src/list/list-keymap.ts","../src/list/list-plugins.ts","../src/list/list-serializer.ts","../src/list/list-spec.ts","../src/list/list-drop-indicator.ts","../src/list/list.ts"],"sourcesContent":["import { defineCommands, insertNode, type Extension } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\nimport {\n createDedentListCommand as dedentList,\n createIndentListCommand as indentList,\n createMoveListCommand as moveList,\n createSplitListCommand as splitList,\n createToggleCollapsedCommand as toggleCollapsed,\n createToggleListCommand as toggleList,\n createUnwrapListCommand as unwrapList,\n createWrapInListCommand as wrapInList,\n type DedentListOptions,\n type IndentListOptions,\n type ListAttributes,\n type ToggleCollapsedOptions,\n type UnwrapListOptions,\n} from 'prosemirror-flat-list'\n\nfunction insertList(attrs?: ListAttributes): Command {\n return insertNode({ type: 'list', attrs })\n}\n\n/**\n * @internal\n */\nexport type ListCommandsExtension = Extension<{\n Commands: {\n dedentList: [options?: DedentListOptions]\n indentList: [options?: IndentListOptions]\n moveList: [direction: 'up' | 'down']\n splitList: []\n toggleCollapsed: [options?: ToggleCollapsedOptions]\n unwrapList: [options?: UnwrapListOptions]\n toggleList: [attrs?: ListAttributes]\n wrapInList: [attrs?: ListAttributes]\n insertList: [attrs?: ListAttributes]\n }\n}>\n\n/**\n * Defines list commands.\n *\n * @internal\n */\nexport function defineListCommands(): ListCommandsExtension {\n return defineCommands({\n dedentList,\n indentList,\n moveList,\n splitList,\n toggleCollapsed,\n unwrapList,\n toggleList,\n wrapInList,\n insertList,\n })\n}\n","import { union, type Extension } from '@prosekit/core'\nimport { listInputRules } from 'prosemirror-flat-list'\n\nimport { defineInputRule } from '../input-rule/index.ts'\n\n/**\n * @internal\n */\nexport function defineListInputRules(): Extension {\n return union(listInputRules.map(defineInputRule))\n}\n","import { defineKeymap, type PlainExtension } from '@prosekit/core'\nimport { chainCommands, deleteSelection } from '@prosekit/pm/commands'\nimport {\n createDedentListCommand,\n createIndentListCommand,\n deleteCommand,\n enterCommand,\n joinCollapsedListBackward,\n joinListUp,\n protectCollapsed,\n} from 'prosemirror-flat-list'\n\n// This is different from the one exported by prosemirror-flat-list, because\n// some commands are moved to `defineBaseKeymap` in `prosekit/core`.\nconst backspaceCommand = chainCommands(\n protectCollapsed,\n deleteSelection,\n joinListUp,\n joinCollapsedListBackward,\n)\n\nconst dedentListCommand = createDedentListCommand()\nconst indentListCommand = createIndentListCommand()\n\nconst listKeymap = {\n 'Enter': enterCommand,\n 'Backspace': backspaceCommand,\n 'Delete': deleteCommand,\n 'Mod-]': indentListCommand,\n 'Mod-[': dedentListCommand,\n 'Tab': indentListCommand,\n 'Shift-Tab': dedentListCommand,\n}\n\n/**\n * Returns a extension that adds key bindings for list.\n *\n * @internal\n */\nexport function defineListKeymap(): PlainExtension {\n return defineKeymap(listKeymap)\n}\n","import { definePlugin, type PlainExtension } from '@prosekit/core'\nimport { Plugin } from '@prosekit/pm/state'\nimport {\n createListEventPlugin,\n createListRenderingPlugin,\n createSafariInputMethodWorkaroundPlugin,\n unwrapListSlice,\n} from 'prosemirror-flat-list'\n\nfunction createListClipboardPlugin(): Plugin {\n return new Plugin({\n props: {\n transformCopied: unwrapListSlice,\n },\n })\n}\n\nfunction createListPlugins(): Plugin[] {\n return [\n createListEventPlugin(),\n createListRenderingPlugin(),\n createListClipboardPlugin(),\n createSafariInputMethodWorkaroundPlugin(),\n ]\n}\n\n/**\n * @internal\n */\nexport function defineListPlugins(): PlainExtension {\n return definePlugin(createListPlugins)\n}\n","import { isElementLike } from '@ocavue/utils'\nimport { defineClipboardSerializer, type PlainExtension } from '@prosekit/core'\nimport { findCheckboxInListItem, joinListElements, listToDOM } from 'prosemirror-flat-list'\n\n/**\n * @internal\n */\nexport function defineListSerializer(): PlainExtension {\n return defineClipboardSerializer({\n serializeFragmentWrapper: (fn) => {\n return (...args) => {\n const dom = fn(...args)\n return normalizeElementTree(joinListElements(dom))\n }\n },\n serializeNodeWrapper: (fn) => {\n return (...args) => {\n const dom = fn(...args)\n return isElementLike(dom)\n ? normalizeElementTree(joinListElements(dom))\n : dom\n }\n },\n nodesFromSchemaWrapper: (fn) => {\n return (...args) => {\n const nodes = fn(...args)\n return {\n ...nodes,\n list: (node) => listToDOM({ node, nativeList: true }),\n }\n }\n },\n })\n}\n\nfunction normalizeElementTree<T extends Element | DocumentFragment>(\n node: T,\n): T {\n if (isElementLike(node)) {\n normalizeTaskList(node)\n }\n\n for (const child of node.children) {\n normalizeElementTree(child)\n }\n\n return node\n}\n\n/**\n * Modifies the DOM tree for task lists to ensure that the output HTML can be\n * parsed by rehype-remark.\n */\nfunction normalizeTaskList(node: Element): void {\n if (\n !node.classList.contains('prosemirror-flat-list')\n || node.getAttribute('data-list-kind') !== 'task'\n || node.children.length !== 2\n ) {\n return\n }\n\n const marker = node.children.item(0)\n if (!marker || !marker.classList.contains('list-marker')) {\n return\n }\n\n const checkbox = findCheckboxInListItem(marker)\n if (!checkbox) {\n return\n }\n\n const content = node.children.item(1)\n if (!content || !content.classList.contains('list-content')) {\n return\n }\n\n const textBlock = content.children.item(0)\n if (!textBlock || !['P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(textBlock.tagName)) {\n return\n }\n\n node.replaceChildren(...content.children)\n textBlock.prepend(checkbox)\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { DOMOutputSpec, ProseMirrorNode } from '@prosekit/pm/model'\nimport { createListSpec, listToDOM, type ListAttributes } from 'prosemirror-flat-list'\n\nimport type { ListAttrs } from './list-types.ts'\n\n/**\n * @internal\n */\nexport type ListSpecExtension = Extension<{\n Nodes: {\n list: ListAttrs\n }\n}>\n\nfunction getMarkers(node: ProseMirrorNode): DOMOutputSpec[] {\n const attrs = node.attrs as ListAttributes\n switch (attrs.kind) {\n case 'task':\n // Use a `label` element here so that the area around the checkbox is also checkable.\n return [\n [\n 'label',\n [\n 'input',\n { type: 'checkbox', checked: attrs.checked ? '' : undefined },\n ],\n ],\n ]\n default:\n // Always return an empty array so that the marker element is rendered. This\n // is required to make the drop indicator locate the correct position.\n return []\n }\n}\n\n/**\n * @internal\n */\nexport function defineListSpec(): ListSpecExtension {\n const spec = createListSpec()\n\n return defineNodeSpec<'list', ListAttrs>({\n ...spec,\n toDOM: (node) => {\n return listToDOM({ node, getMarkers })\n },\n name: 'list',\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport type { DragEventHandler } from '../drop-indicator/index.ts'\nimport { defineDropIndicator } from '../drop-indicator/index.ts'\n\n/**\n * Configures drop indicator to avoid unexpected drop point.\n *\n * We don't want to drag a list node and drop it as the first\n * child of another list node.\n *\n * @internal\n */\nexport function defineListDropIndicator(): PlainExtension {\n return defineDropIndicator({\n onDrag,\n })\n}\n\nconst onDrag: DragEventHandler = ({ view, pos }): boolean => {\n const slice = view.dragging?.slice\n if (\n slice\n && slice.openStart === 0\n && slice.openEnd === 0\n && slice.content.childCount === 1\n ) {\n const node = slice.content.child(0)\n if (node.type.name === 'list') {\n const $pos = view.state.doc.resolve(pos)\n if ($pos.parent.type.name === 'list' && $pos.index() === 0) {\n return false\n }\n }\n }\n return true\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineListCommands, type ListCommandsExtension } from './list-commands.ts'\nimport { defineListDropIndicator } from './list-drop-indicator.ts'\nimport { defineListInputRules } from './list-input-rules.ts'\nimport { defineListKeymap } from './list-keymap.ts'\nimport { defineListPlugins } from './list-plugins.ts'\nimport { defineListSerializer } from './list-serializer.ts'\nimport { defineListSpec, type ListSpecExtension } from './list-spec.ts'\n\n/**\n * @internal\n */\nexport type ListExtension = Union<[ListSpecExtension, ListCommandsExtension]>\n\n/**\n * @public\n */\nexport function defineList(): ListExtension {\n return union(\n defineListSpec(),\n defineListPlugins(),\n defineListKeymap(),\n defineListInputRules(),\n defineListCommands(),\n defineListSerializer(),\n defineListDropIndicator(),\n )\n}\n"],"mappings":";;;;;;;AAkBA,SAAS,WAAW,OAAiC;AACnD,QAAO,WAAW;EAAE,MAAM;EAAQ;EAAO,CAAC;;;;;;;AAyB5C,SA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U;AAC7B,KACE,SACG,MAAM,cAAc,KACpB,MAAM,YAAY,KAClB,MAAM,QAAQ,eAAe;MAEnB,MAAM,QAAQ,MAAM,EACzB,CAAC,KAAK,SAAS,QAAQ;GAC7B,MAAM,OAAO,KAAK,MAAM,IAAI,QAAQ,IAAI;AACxC,OAAI,KAAK,OAAO,KAAK,SAAS,UAAU,KAAK,OAAO,KAAK,EACvD,QAAO;;;AAIb,QAAO;;;;;ACjBT,SAAgB,aAA4B;AAC1C,QAAO,MACL,gBAAgB,EAChB,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,CAC1B"}
@@ -1 +1 @@
1
- {"version":3,"file":"mark-rule2.js","names":[],"sources":["../src/mark-rule/range.ts","../src/mark-rule/apply.ts","../src/mark-rule/mark-rule.ts"],"sourcesContent":["import type { ProseMirrorNode, ResolvedPos } from '@prosekit/pm/model'\nimport type { EditorState, Transaction } from '@prosekit/pm/state'\nimport type { ProsemirrorNode } from 'prosemirror-flat-list'\n\nfunction getSpanTextRanges($from: ResolvedPos, $to: ResolvedPos) {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n const stack: Array<[start: number, node: ProseMirrorNode]> = []\n let start = nodeRange.start\n\n for (let i = nodeRange.startIndex; i < nodeRange.endIndex; i++) {\n const child = nodeRange.parent.child(i)\n stack.push([start, child])\n start += child.nodeSize\n }\n\n const ranges: Array<[number, number]> = []\n\n while (stack.length > 0) {\n const [start, node] = stack.pop()!\n if (node.type.spec.code) {\n continue\n }\n\n if (node.type.isTextblock) {\n ranges.push([start + 1, start + 1 + node.content.size])\n continue\n }\n\n node.forEach((child, offset) => {\n stack.push([start + offset + 1, child])\n })\n }\n\n return ranges\n}\n\nfunction getInlineTextRange(\n $from: ResolvedPos,\n $to: ResolvedPos,\n): [number, number] {\n return [$from.start(), $to.end()]\n}\n\nfunction getTextRanges(\n doc: ProsemirrorNode,\n from: number,\n to: number,\n): Array<[number, number]> {\n const $from = doc.resolve(from)\n const $to = doc.resolve(to)\n\n if ($from.sameParent($to) && $from.parent.isTextblock) {\n return [getInlineTextRange($from, $to)]\n } else {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n return getSpanTextRanges($from, $to)\n }\n}\n\nfunction getMapRange(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n) {\n let lo = oldState.selection.from\n let hi = oldState.selection.to\n\n for (const tr of transactions) {\n for (const map of tr.mapping.maps) {\n lo = map.map(lo)\n hi = map.map(hi)\n\n map.forEach((_oldStart, _oldEnd, newStart, newEnd) => {\n lo = Math.min(lo, hi, newStart)\n hi = Math.max(lo, hi, newEnd)\n })\n }\n }\n\n lo = Math.min(lo, hi, newState.selection.from)\n hi = Math.min(lo, hi, newState.selection.to)\n\n return [lo, hi] as const\n}\n\nexport function getCheckRanges(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Array<[number, number]> {\n const [from, to] = getMapRange(transactions, oldState, newState)\n return getTextRanges(newState.doc, from, to)\n}\n","import { getMarkType, maybeRun, OBJECT_REPLACEMENT_CHARACTER } from '@prosekit/core'\nimport type { Mark, ProseMirrorNode } from '@prosekit/pm/model'\nimport type { EditorState, Transaction } from '@prosekit/pm/state'\n\nimport { getCheckRanges } from './range.ts'\nimport type { MarkRuleOptions } from './types.ts'\n\ntype MarkRange = [from: number, to: number, mark: Mark]\n\nfunction getExpectedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const text = doc.textBetween(from, to, null, OBJECT_REPLACEMENT_CHARACTER)\n const ranges: MarkRange[] = []\n\n for (const rule of rules) {\n rule.regex.lastIndex = 0\n const matches = text.matchAll(rule.regex)\n const markType = getMarkType(doc.type.schema, rule.type)\n\n for (const match of matches) {\n const index = match.index\n if (index == null) continue\n const attrs = maybeRun(rule.attrs, match)\n const mark = markType.create(attrs)\n ranges.push([from + index, from + index + match[0].length, mark])\n }\n }\n\n // Sort by start position. If start positions are equal, the longer match\n // should be prioritized.\n ranges.sort((a, b) => a[0] - b[0] || b[1] - a[1])\n\n // Remove overlapped marks.\n const result: MarkRange[] = []\n let freeIndex = 0\n\n for (const range of ranges) {\n if (range[0] >= freeIndex) {\n result.push(range)\n freeIndex = range[1]\n }\n }\n\n return result\n}\n\nfunction getReceivedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const result: MarkRange[] = []\n const schema = doc.type.schema\n const markTypes = rules.map((rule) => getMarkType(schema, rule.type))\n\n doc.nodesBetween(from, to, (node, pos) => {\n if (!node.isInline) {\n return\n }\n\n for (const markType of markTypes) {\n const mark = node.marks.find((mark) => mark.type === markType)\n if (mark) {\n result.push([pos, pos + node.nodeSize, mark])\n }\n }\n })\n return result\n}\n\nfunction markRangeEquals(a: MarkRange, b: MarkRange): boolean {\n return a[0] === b[0] && a[1] === b[1] && a[2].eq(b[2])\n}\n\nfunction markRangeDiffs(a: MarkRange[], b: MarkRange[]): MarkRange[] {\n return a.filter((x) => !b.some((y) => markRangeEquals(x, y)))\n}\n\nexport function applyMarkRules(\n rules: MarkRuleOptions[],\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Transaction | null {\n if (transactions.length === 0 || transactions.every((tr) => !tr.docChanged)) {\n return null\n }\n\n const ranges = getCheckRanges(transactions, oldState, newState)\n\n const toRemove: MarkRange[] = []\n const toCreate: MarkRange[] = []\n\n for (const [from, to] of ranges) {\n const expected = getExpectedMarkings(rules, newState.doc, from, to)\n const received = getReceivedMarkings(rules, newState.doc, from, to)\n\n toRemove.push(...markRangeDiffs(received, expected))\n toCreate.push(...markRangeDiffs(expected, received))\n }\n\n if (toCreate.length === 0 && toRemove.length === 0) {\n return null\n }\n\n const tr = newState.tr\n for (const [from, to, mark] of toRemove) {\n tr.removeMark(from, to, mark)\n }\n for (const [from, to, mark] of toCreate) {\n tr.addMark(from, to, mark)\n }\n return tr\n}\n","import { defineFacet, defineFacetPayload, pluginFacet, type PlainExtension, type PluginPayload } from '@prosekit/core'\nimport { PluginKey, ProseMirrorPlugin, type EditorState, type Transaction } from '@prosekit/pm/state'\n\nimport { applyMarkRules } from './apply.ts'\nimport type { MarkRuleOptions } from './types.ts'\n\n/**\n * A mark rule is something that can automatically apply marks to text if it\n * matches a certain pattern, and remove them if it doesn't match anymore.\n */\nexport function defineMarkRule(options: MarkRuleOptions): PlainExtension {\n return defineFacetPayload(markRuleFacet, [options]) as PlainExtension\n}\n\nconst markRuleFacet = defineFacet<MarkRuleOptions, PluginPayload>({\n reduce: () => {\n let rules: MarkRuleOptions[] = []\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-mark-rule'),\n appendTransaction: (\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n ) => {\n return applyMarkRules(rules, transactions, oldState, newState)\n },\n })\n\n return function reducer(input) {\n rules = input\n return plugin\n }\n },\n\n parent: pluginFacet,\n})\n"],"mappings":";;AAIA,SAAS,kBAAkB,OAAoB,KAAkB;CAC/D,MAAM,YAAY,MAAM,WAAW,IAAI;AACvC,KAAI,CAAC,UACH,QAAO,EAAE;CAGX,MAAM,QAAuD,EAAE;CAC/D,IAAI,QAAQ,UAAU;AAEtB,MAAK,IAAI,IAAI,UAAU,YAAY,IAAI,UAAU,UAAU,KAAK;EAC9D,MAAM,QAAQ,UAAU,OAAO,MAAM,EAAE;AACvC,QAAM,KAAK,CAAC,OAAO,MAAM,CAAC;AAC1B,WAAS,MAAM;;CAGjB,MAAM,SAAkC,EAAE;AAE1C,QAAO,MAAM,SAAS,GAAG;EACvB,MAAM,CAAC,OAAO,QAAQ,MAAM,KAAK;AACjC,MAAI,KAAK,KAAK,KAAK,KACjB;AAGF,MAAI,KAAK,KAAK,aAAa;AACzB,UAAO,KAAK,CAAC,QAAQ,GAAG,QAAQ,IAAI,KAAK,QAAQ,KAAK,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":["../src/mark-rule/range.ts","../src/mark-rule/apply.ts","../src/mark-rule/mark-rule.ts"],"sourcesContent":["import type { ProseMirrorNode, ResolvedPos } from '@prosekit/pm/model'\nimport type { EditorState, Transaction } from '@prosekit/pm/state'\nimport type { ProsemirrorNode } from 'prosemirror-flat-list'\n\nfunction getSpanTextRanges($from: ResolvedPos, $to: ResolvedPos) {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n const stack: Array<[start: number, node: ProseMirrorNode]> = []\n let start = nodeRange.start\n\n for (let i = nodeRange.startIndex; i < nodeRange.endIndex; i++) {\n const child = nodeRange.parent.child(i)\n stack.push([start, child])\n start += child.nodeSize\n }\n\n const ranges: Array<[number, number]> = []\n\n while (stack.length > 0) {\n const [start, node] = stack.pop()!\n if (node.type.spec.code) {\n continue\n }\n\n if (node.type.isTextblock) {\n ranges.push([start + 1, start + 1 + node.content.size])\n continue\n }\n\n node.forEach((child, offset) => {\n stack.push([start + offset + 1, child])\n })\n }\n\n return ranges\n}\n\nfunction getInlineTextRange(\n $from: ResolvedPos,\n $to: ResolvedPos,\n): [number, number] {\n return [$from.start(), $to.end()]\n}\n\nfunction getTextRanges(\n doc: ProsemirrorNode,\n from: number,\n to: number,\n): Array<[number, number]> {\n const $from = doc.resolve(from)\n const $to = doc.resolve(to)\n\n if ($from.sameParent($to) && $from.parent.isTextblock) {\n return [getInlineTextRange($from, $to)]\n } else {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n return getSpanTextRanges($from, $to)\n }\n}\n\nfunction getMapRange(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n) {\n let lo = oldState.selection.from\n let hi = oldState.selection.to\n\n for (const tr of transactions) {\n for (const map of tr.mapping.maps) {\n lo = map.map(lo)\n hi = map.map(hi)\n\n map.forEach((_oldStart, _oldEnd, newStart, newEnd) => {\n lo = Math.min(lo, hi, newStart)\n hi = Math.max(lo, hi, newEnd)\n })\n }\n }\n\n lo = Math.min(lo, hi, newState.selection.from)\n hi = Math.min(lo, hi, newState.selection.to)\n\n return [lo, hi] as const\n}\n\nexport function getCheckRanges(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Array<[number, number]> {\n const [from, to] = getMapRange(transactions, oldState, newState)\n return getTextRanges(newState.doc, from, to)\n}\n","import { getMarkType, maybeRun, OBJECT_REPLACEMENT_CHARACTER } from '@prosekit/core'\nimport type { Mark, ProseMirrorNode } from '@prosekit/pm/model'\nimport type { EditorState, Transaction } from '@prosekit/pm/state'\n\nimport { getCheckRanges } from './range.ts'\nimport type { MarkRuleOptions } from './types.ts'\n\ntype MarkRange = [from: number, to: number, mark: Mark]\n\nfunction getExpectedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const text = doc.textBetween(from, to, null, OBJECT_REPLACEMENT_CHARACTER)\n const ranges: MarkRange[] = []\n\n for (const rule of rules) {\n rule.regex.lastIndex = 0\n const matches = text.matchAll(rule.regex)\n const markType = getMarkType(doc.type.schema, rule.type)\n\n for (const match of matches) {\n const index = match.index\n if (index == null) continue\n const attrs = maybeRun(rule.attrs, match)\n const mark = markType.create(attrs)\n ranges.push([from + index, from + index + match[0].length, mark])\n }\n }\n\n // Sort by start position. If start positions are equal, the longer match\n // should be prioritized.\n ranges.sort((a, b) => a[0] - b[0] || b[1] - a[1])\n\n // Remove overlapped marks.\n const result: MarkRange[] = []\n let freeIndex = 0\n\n for (const range of ranges) {\n if (range[0] >= freeIndex) {\n result.push(range)\n freeIndex = range[1]\n }\n }\n\n return result\n}\n\nfunction getReceivedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const result: MarkRange[] = []\n const schema = doc.type.schema\n const markTypes = rules.map((rule) => getMarkType(schema, rule.type))\n\n doc.nodesBetween(from, to, (node, pos) => {\n if (!node.isInline) {\n return\n }\n\n for (const markType of markTypes) {\n const mark = node.marks.find((mark) => mark.type === markType)\n if (mark) {\n result.push([pos, pos + node.nodeSize, mark])\n }\n }\n })\n return result\n}\n\nfunction markRangeEquals(a: MarkRange, b: MarkRange): boolean {\n return a[0] === b[0] && a[1] === b[1] && a[2].eq(b[2])\n}\n\nfunction markRangeDiffs(a: MarkRange[], b: MarkRange[]): MarkRange[] {\n return a.filter((x) => !b.some((y) => markRangeEquals(x, y)))\n}\n\nexport function applyMarkRules(\n rules: MarkRuleOptions[],\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Transaction | null {\n if (transactions.length === 0 || transactions.every((tr) => !tr.docChanged)) {\n return null\n }\n\n const ranges = getCheckRanges(transactions, oldState, newState)\n\n const toRemove: MarkRange[] = []\n const toCreate: MarkRange[] = []\n\n for (const [from, to] of ranges) {\n const expected = getExpectedMarkings(rules, newState.doc, from, to)\n const received = getReceivedMarkings(rules, newState.doc, from, to)\n\n toRemove.push(...markRangeDiffs(received, expected))\n toCreate.push(...markRangeDiffs(expected, received))\n }\n\n if (toCreate.length === 0 && toRemove.length === 0) {\n return null\n }\n\n const tr = newState.tr\n for (const [from, to, mark] of toRemove) {\n tr.removeMark(from, to, mark)\n }\n for (const [from, to, mark] of toCreate) {\n tr.addMark(from, to, mark)\n }\n return tr\n}\n","import { defineFacet, defineFacetPayload, pluginFacet, type PlainExtension, type PluginPayload } from '@prosekit/core'\nimport { PluginKey, ProseMirrorPlugin, type EditorState, type Transaction } from '@prosekit/pm/state'\n\nimport { applyMarkRules } from './apply.ts'\nimport type { MarkRuleOptions } from './types.ts'\n\n/**\n * A mark rule is something that can automatically apply marks to text if it\n * matches a certain pattern, and remove them if it doesn't match anymore.\n */\nexport function defineMarkRule(options: MarkRuleOptions): PlainExtension {\n return defineFacetPayload(markRuleFacet, [options]) as PlainExtension\n}\n\nconst markRuleFacet = defineFacet<MarkRuleOptions, PluginPayload>({\n reduce: () => {\n let rules: MarkRuleOptions[] = []\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-mark-rule'),\n appendTransaction: (\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n ) => {\n return applyMarkRules(rules, transactions, oldState, newState)\n },\n })\n\n return function reducer(input) {\n rules = input\n return plugin\n }\n },\n\n parent: pluginFacet,\n})\n"],"mappings":";;AAIA,SAAS,kBAAkB,OAAoB,KAAkB;CAC/D,MAAM,YAAY,MAAM,WAAW,IAAI;AACvC,KAAI,CAAC,UACH,QAAO,EAAE;CAGX,MAAM,QAAuD,EAAE;CAC/D,IAAI,QAAQ,UAAU;AAEtB,MAAK,IAAI,IAAI,UAAU,YAAY,IAAI,UAAU,UAAU,KAAK;EAC9D,MAAM,QAAQ,UAAU,OAAO,MAAM,EAAE;AACvC,QAAM,KAAK,CAAC,OAAO,MAAM,CAAC;AAC1B,WAAS,MAAM;;CAGjB,MAAM,SAAkC,EAAE;AAE1C,QAAO,MAAM,SAAS,GAAG;EACvB,MAAM,CAAC,OAAO,QAAQ,MAAM,KAAK;AACjC,MAAI,KAAK,KAAK,KAAK,KACjB;AAGF,MAAI,KAAK,KAAK,aAAa;AACzB,UAAO,KAAK,CAAC,QAAQ,GAAG,QAAQ,IAAI,KAAK,QAAQ,KAAK,CAAC;AACvD;;AAGF,OAAK,SAAS,OAAO,WAAW;AAC9B,SAAM,KAAK,CAAC,QAAQ,SAAS,GAAG,MAAM,CAAC;IACvC;;AAGJ,QAAO;;AAGT,SAAS,mBACP,OACA,KACkB;AAClB,QAAO,CAAC,MAAM,OAAO,EAAE,IAAI,KAAK,CAAC;;AAGnC,SAAS,cACP,KACA,MACA,IACyB;CACzB,MAAM,QAAQ,IAAI,QAAQ,KAAK;CAC/B,MAAM,MAAM,IAAI,QAAQ,GAAG;AAE3B,KAAI,MAAM,WAAW,IAAI,IAAI,MAAM,OAAO,YACxC,QAAO,CAAC,mBAAmB,OAAO,IAAI,CAAC;MAClC;AAEL,MAAI,CADc,MAAM,WAAW,IACrB,CACZ,QAAO,EAAE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,WAAS,KAAK,GAAG,eAAe,UAAU,SAAS,CAAC;AACpD,WAAS,KAAK,GAAG,eAAe,UAAU,SAAS,CAAC;;AAGtD,KAAI,SAAS,WAAW,KAAK,SAAS,WAAW,EAC/C,QAAO;CAGT,MAAM,KAAK,SAAS;AACpB,MAAK,MAAM,CAAC,MAAM,IAAI,SAAS,SAC7B,IAAG,WAAW,MAAM,IAAI,KAAK;AAE/B,MAAK,MAAM,CAAC,MAAM,IAAI,SAAS,SAC7B,IAAG,QAAQ,MAAM,IAAI,KAAK;AAE5B,QAAO;;;;;;AC3GT,SAAgB,eAAe,SAA0C;AACvE,QAAO,mBAAmB,eAAe,CAAC,QAAQ,CAAC;;AAGrD,MAAM,gBAAgB,YAA4C;CAChE,cAAc;EACZ,IAAI,QAA2B,EAAE;EAEjC,MAAM,SAAS,IAAI,kBAAkB;GACnC,KAAK,IAAI,UAAU,qBAAqB;GACxC,oBACE,cACA,UACA,aACG;AACH,WAAO,eAAe,OAAO,cAAc,UAAU,SAAS;;GAEjE,CAAC;AAEF,SAAO,SAAS,QAAQ,OAAO;AAC7B,WAAQ;AACR,UAAO;;;CAIX,QAAQ;CACT,CAAC"}
package/dist/page.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"page.js","names":["#parseDataAttributes","#isLeader","#isHeadPending","#render","#contentBoxHeight","#paddingTop","#paddingBottom","#requestUpdate","#group","#forceNextBreak","#pageHeight","#parseFloatAttribute","#pageMarginTop","#pageMarginBottom","#size","#paddingTopPending","#paddingBottomPending","#isHead","#isTail","#isTailPending","#updateRequested","#updateAll"],"sources":["../src/page/page-break-commands.ts","../src/page/page-break-keymap.ts","../src/page/page-break-spec.ts","../src/page/page-break.ts","../src/page/page-element.ts","../src/page/page-rendering.ts"],"sourcesContent":["import { defineCommands, getNodeType, type Extension } from '@prosekit/core'\nimport { Fragment, Slice } from '@prosekit/pm/model'\nimport type { Command } from '@prosekit/pm/state'\n\n/**\n * @internal\n */\nexport type PageBreakCommandsExtension = Extension<{\n Commands: {\n insertPageBreak: []\n }\n}>\n\nconst insertPageBreakCommand: Command = (state, dispatch): boolean => {\n if (!dispatch) return true\n\n const { schema, tr } = state\n const type = getNodeType(schema, 'pageBreak')\n const node = type.createChecked()\n const pos = tr.selection.anchor\n const slice = new Slice(Fragment.from(node), 0, 0)\n tr.replaceRange(pos, pos, slice).scrollIntoView()\n dispatch(tr)\n return true\n}\n\n/**\n * @internal\n */\nexport function insertPageBreak(): Command {\n return insertPageBreakCommand\n}\n\n/**\n * @internal\n */\nexport function definePageBreakCommands(): PageBreakCommandsExtension {\n return defineCommands({\n insertPageBreak: insertPageBreak,\n })\n}\n","import { defineKeymap, type PlainExtension } from '@prosekit/core'\n\nimport { insertPageBreak } from './page-break-commands.ts'\n\n/**\n * @internal\n */\nexport type PageBreakKeymapExtension = PlainExtension\n\n/**\n * @internal\n */\nexport function definePageBreakKeymap(): PageBreakKeymapExtension {\n return defineKeymap({\n 'Mod-Enter': insertPageBreak(),\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { Attrs } from '@prosekit/pm/model'\n\n/**\n * @internal\n */\nexport type PageBreakSpecExtension = Extension<{\n Nodes: {\n pageBreak: Attrs\n }\n}>\n\n/**\n * @internal\n */\nexport function definePageBreakSpec(): PageBreakSpecExtension {\n return defineNodeSpec({\n name: 'pageBreak',\n group: 'block',\n selectable: true,\n parseDOM: [{ tag: 'div.prosekit-page-break' }],\n toDOM() {\n return ['div', { class: 'prosekit-horizontal-rule prosekit-page-break' }, ['hr']]\n },\n pageBreak: true,\n })\n}\n\ndeclare module '@prosekit/pm/model' {\n interface NodeSpec {\n pageBreak?: boolean | undefined\n }\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { definePageBreakCommands, type PageBreakCommandsExtension } from './page-break-commands.ts'\nimport { definePageBreakKeymap, type PageBreakKeymapExtension } from './page-break-keymap.ts'\nimport { definePageBreakSpec, type PageBreakSpecExtension } from './page-break-spec.ts'\n\n/**\n * @internal\n */\nexport type PageBreakExtension = Union<\n [PageBreakSpecExtension, PageBreakCommandsExtension, PageBreakKeymapExtension]\n>\n\n/**\n * @public\n */\nexport function definePageBreak(): PageBreakExtension {\n return union(\n definePageBreakSpec(),\n definePageBreakCommands(),\n definePageBreakKeymap(),\n )\n}\n","import { once } from '@ocavue/utils'\nimport { customElements, HTMLElement } from 'server-dom-shim'\n\n/**\n * @internal\n */\nexport const PAGE_CHUNK_TAG_NAME = 'pm-page-chunk'\n\n/**\n * @internal\n */\nexport function registerPageChunkElement(): void {\n if (typeof window === 'undefined' || customElements.get(PAGE_CHUNK_TAG_NAME)) return\n customElements.define(PAGE_CHUNK_TAG_NAME, PageChunkElement)\n}\n\nclass PageChunkElement extends HTMLElement {\n static observedAttributes = [\n 'data-group',\n 'data-break',\n 'data-h',\n 'data-mt',\n 'data-mb',\n\n // Only the first chunk of the whole document has this attribute.\n 'data-size',\n ]\n\n // Data attributes set by external code\n #group: string = ''\n #forceNextBreak: boolean = false\n #pageHeight: number = 0\n #pageMarginTop: number = 0\n #pageMarginBottom: number = 0\n #size: number | undefined = undefined\n\n // Internal states\n #updateRequested: boolean = false\n #contentBoxHeight: number = 0\n\n // Rendering states\n #isHead: boolean = false\n #isTail: boolean = false\n #paddingTop: number = 0\n #paddingBottom: number = 0\n\n // Pending rendering states\n #isHeadPending: boolean = false\n #isTailPending: boolean = false\n #paddingTopPending: number = 0\n #paddingBottomPending: number = 0\n\n connectedCallback() {\n this.#parseDataAttributes()\n\n if (this.#isLeader()) {\n this.#isHeadPending = true\n }\n\n this.#render()\n\n // Get the initial content box height when the resize observer is not started yet. Notice that\n // `this.clientHeight` is an integer while the content box height can be a float, so this is not\n // accurate but should be good enough for the first render.\n this.#contentBoxHeight = this.clientHeight - this.#paddingTop - this.#paddingBottom\n\n observeElement(this)\n\n this.#requestUpdate()\n }\n\n disconnectedCallback() {\n unobserveElement(this)\n }\n\n attributeChangedCallback(_: string, oldValue: string | null, newValue: string | null) {\n if (oldValue === newValue) return\n this.#parseDataAttributes()\n this.#requestUpdate()\n }\n\n #parseDataAttributes() {\n this.#group = this.getAttribute('data-group') || ''\n this.#forceNextBreak = this.hasAttribute('data-break')\n this.#pageHeight = this.#parseFloatAttribute('data-h')\n this.#pageMarginTop = this.#parseFloatAttribute('data-mt')\n this.#pageMarginBottom = this.#parseFloatAttribute('data-mb')\n\n const sizeAttr = this.getAttribute('data-size')\n this.#size = sizeAttr ? Number.parseInt(sizeAttr, 10) : undefined\n }\n\n #parseFloatAttribute(name: string): number {\n const value = this.getAttribute(name)\n return value != null ? Number.parseFloat(value) : 0\n }\n\n #isLeader() {\n return this.#size != null\n }\n\n #render() {\n if (this.#paddingTop !== this.#paddingTopPending || this.#paddingBottom !== this.#paddingBottomPending) {\n Object.assign(this.style, {\n paddingTop: `${this.#paddingTop = this.#paddingTopPending}px`,\n paddingBottom: `${this.#paddingBottom = this.#paddingBottomPending}px`,\n })\n }\n if (this.#isHead !== this.#isHeadPending) {\n this.toggleAttribute('data-page-head', this.#isHead = this.#isHeadPending)\n }\n if (this.#isTail !== this.#isTailPending) {\n this.toggleAttribute('data-page-tail', this.#isTail = this.#isTailPending)\n }\n }\n\n setHeight(height: number) {\n // Avoid potential float number precision issues\n if (Math.abs(this.#contentBoxHeight - height) < 0.1) {\n return\n }\n this.#contentBoxHeight = height\n this.#requestUpdate()\n }\n\n /**\n * Schedules a batched page layout recalculation.\n *\n * Any chunk can call this method, but the actual layout work (#updateAll)\n * always runs on the leader chunk, because it needs to iterate over every\n * chunk in order to compute page breaks.\n *\n * Two nested microtasks are used to batch updates:\n *\n * Microtask 1 – Delegation: non-leader chunks forward the request to the\n * leader chunk, so multiple chunks changing in the same tick only trigger\n * one layout pass.\n *\n * Microtask 2 – Execution: the leader chunk defers #updateAll to a second\n * microtask so that any other attribute / resize changes that were queued\n * in the same tick (and forwarded during microtask 1) are already reflected\n * before the layout is recalculated.\n *\n * The #updateRequested flag acts as a deduplication guard so that rapid\n * successive calls (e.g. multiple attributes changing at once) result in at\n * most one scheduled pass per chunk.\n */\n #requestUpdate() {\n if (this.#updateRequested) {\n return\n }\n\n this.#updateRequested = true\n queueMicrotask(() => {\n if (!this.#isLeader()) {\n this.#updateRequested = false\n const leader = findLeaderChunk(this, this.#group)\n if (!leader) return\n leader.#requestUpdate()\n return\n }\n queueMicrotask(() => {\n this.#updateRequested = false\n this.#updateAll()\n })\n })\n }\n\n #updateAll() {\n if (!this.isConnected) {\n return\n }\n\n const elements = findAllChunks(this, this.#group)\n const count = elements.length\n if (count === 0) return\n\n const pageHeight = this.#pageHeight\n const pageMarginTop = this.#pageMarginTop\n const maxContentHeight = pageHeight - pageMarginTop - this.#pageMarginBottom\n\n let currentContentHeight = 0\n let forceNextBreak = false\n\n for (let i = 0; i < count; i++) {\n const element = elements[i]\n const h = element.#contentBoxHeight\n const isHead = forceNextBreak || i === 0 || (currentContentHeight + h > maxContentHeight)\n\n forceNextBreak = element.#forceNextBreak\n\n if (isHead && i > 0) {\n const prev = elements[i - 1]\n prev.#paddingBottomPending = Math.max(0, pageHeight - pageMarginTop - currentContentHeight)\n prev.#isTailPending = true\n currentContentHeight = h\n } else {\n currentContentHeight += h\n }\n\n element.#paddingTopPending = isHead ? pageMarginTop : 0\n element.#paddingBottomPending = 0\n element.#isTailPending = false\n element.#isHeadPending = isHead\n }\n\n const last = elements[count - 1]\n last.#paddingBottomPending = Math.max(0, pageHeight - pageMarginTop - currentContentHeight)\n last.#isTailPending = true\n\n for (const element of elements) {\n element.#render()\n }\n }\n}\n\nfunction handleResize(entries: ResizeObserverEntry[]) {\n for (const entry of entries) {\n const contentBoxHeight = entry.contentBoxSize?.[0]?.blockSize ?? entry.contentRect.height\n const element = entry.target as PageChunkElement\n element.setHeight(contentBoxHeight)\n }\n}\n\nconst getResizeObserver = /* @__PURE__ */ once(() => {\n return new ResizeObserver(handleResize)\n})\n\nfunction observeElement(element: PageChunkElement) {\n getResizeObserver().observe(element)\n}\n\nfunction unobserveElement(element: PageChunkElement) {\n getResizeObserver().unobserve(element)\n}\n\nfunction findLeaderChunk(element: HTMLElement, group: string): PageChunkElement | null | undefined {\n const root = element.closest('.ProseMirror')\n return root?.querySelector<PageChunkElement>(`${PAGE_CHUNK_TAG_NAME}[data-group=\"${group}\"][data-size]`)\n}\n\nfunction findAllChunks(element: HTMLElement, group: string): PageChunkElement[] {\n const root = element.closest('.ProseMirror')\n const elements = root?.querySelectorAll<PageChunkElement>(`${PAGE_CHUNK_TAG_NAME}[data-group=\"${group}\"]`)\n return Array.from(elements || [])\n}\n","import { getId } from '@ocavue/utils'\nimport { definePlugin, type Extension } from '@prosekit/core'\nimport type { Node } from '@prosekit/pm/model'\nimport { Plugin, PluginKey } from '@prosekit/pm/state'\nimport { Decoration, DecorationSet } from '@prosekit/pm/view'\n\nimport { PAGE_CHUNK_TAG_NAME, registerPageChunkElement } from './page-element.ts'\n\n/**\n * @public\n */\nexport interface PageRenderingOptions {\n /**\n * The width of the page in px.\n *\n * @default 794 (Portrait A4 paper size in 96 DPI)\n */\n pageWidth?: number\n\n /**\n * The height of the page in px.\n *\n * @default 1123 (Portrait A4 paper size in 96 DPI)\n */\n pageHeight?: number\n\n /**\n * The top margin of the page in px.\n *\n * @default 70\n */\n marginTop?: number\n\n /**\n * The right margin of the page in px.\n *\n * @default 70\n */\n marginRight?: number\n\n /**\n * The bottom margin of the page in px.\n *\n * @default 70\n */\n marginBottom?: number\n\n /**\n * The left margin of the page in px.\n *\n * @default 70\n */\n marginLeft?: number\n}\n\n/**\n * @public\n */\nexport function definePageRendering(options: PageRenderingOptions = {}): PageRenderingExtension {\n return definePlugin(\n createPageRenderingPlugin(options),\n )\n}\n\n/**\n * @internal\n */\nexport type PageRenderingExtension = Extension\n\nfunction createPageRenderingPlugin(options: PageRenderingOptions): Plugin {\n const {\n pageWidth = 794,\n pageHeight = 1123,\n marginTop = 70,\n marginRight = 70,\n marginBottom = 70,\n marginLeft = 70,\n } = options\n\n type PluginState = [group: string, decoration: DecorationSet]\n\n const key = new PluginKey<PluginState>('prosekit-page-render')\n\n function createDecorationSet(doc: Node, group: string): DecorationSet {\n const decorations: Decoration[] = []\n const totalCount = doc.childCount\n\n doc.forEach((node, pos, index) => {\n const isPageBreak: boolean | undefined = node.type.spec.pageBreak\n\n decorations.push(Decoration.node(pos, pos + node.nodeSize, {\n 'nodeName': PAGE_CHUNK_TAG_NAME,\n 'data-group': group,\n 'data-break': isPageBreak ? 'true' : undefined,\n 'data-h': String(pageHeight),\n 'data-mt': String(marginTop),\n 'data-mb': String(marginBottom),\n 'data-size': index === 0 ? String(totalCount) : undefined,\n }))\n })\n\n return DecorationSet.create(doc, decorations)\n }\n\n return new Plugin<PluginState>({\n key,\n view: () => {\n registerPageChunkElement()\n return {}\n },\n state: {\n init: (_config, state): PluginState => {\n const group = `page-group-${getId()}`\n const decoration = createDecorationSet(state.doc, group)\n return [group, decoration]\n },\n apply: (tr, value, oldState, newState): PluginState => {\n if (!tr.docChanged) return value\n\n const [group, decoration] = value\n\n let needRecreate = oldState.doc.childCount !== newState.doc.childCount\n\n if (!needRecreate) {\n const count = oldState.doc.childCount\n for (let i = 0; i < count; i++) {\n const oldNode = oldState.doc.child(i)\n const newNode = newState.doc.child(i)\n if (oldNode.type !== newNode.type) {\n needRecreate = true\n break\n }\n }\n }\n\n if (!needRecreate) {\n const mapped = decoration.map(tr.mapping, tr.doc, {\n onRemove: () => {\n needRecreate = true\n },\n })\n if (!needRecreate) {\n return [group, mapped]\n }\n }\n\n return [group, createDecorationSet(newState.doc, group)]\n },\n },\n props: {\n decorations: (state) => {\n return key.getState(state)?.[1]\n },\n attributes: {\n style: [\n `--page-margin-right:${marginRight}px;`,\n `--page-margin-left:${marginLeft}px;`,\n `--page-width:${pageWidth}px;`,\n `--page-height:${pageHeight}px;`,\n ].join(''),\n },\n },\n })\n}\n"],"mappings":";;;;;;AAaA,MAAM,0BAAmC,OAAO,aAAsB;AACpE,KAAI,CAAC,SAAU,QAAO;CAEtB,MAAM,EAAE,QAA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ingTop","#paddingBottom","#requestUpdate","#group","#forceNextBreak","#pageHeight","#parseFloatAttribute","#pageMarginTop","#pageMarginBottom","#size","#paddingTopPending","#paddingBottomPending","#isHead","#isTail","#isTailPending","#updateRequested","#updateAll"],"sources":["../src/page/page-break-commands.ts","../src/page/page-break-keymap.ts","../src/page/page-break-spec.ts","../src/page/page-break.ts","../src/page/page-element.ts","../src/page/page-rendering.ts"],"sourcesContent":["import { defineCommands, getNodeType, type Extension } from '@prosekit/core'\nimport { Fragment, Slice } from '@prosekit/pm/model'\nimport type { Command } from '@prosekit/pm/state'\n\n/**\n * @internal\n */\nexport type PageBreakCommandsExtension = Extension<{\n Commands: {\n insertPageBreak: []\n }\n}>\n\nconst insertPageBreakCommand: Command = (state, dispatch): boolean => {\n if (!dispatch) return true\n\n const { schema, tr } = state\n const type = getNodeType(schema, 'pageBreak')\n const node = type.createChecked()\n const pos = tr.selection.anchor\n const slice = new Slice(Fragment.from(node), 0, 0)\n tr.replaceRange(pos, pos, slice).scrollIntoView()\n dispatch(tr)\n return true\n}\n\n/**\n * @internal\n */\nexport function insertPageBreak(): Command {\n return insertPageBreakCommand\n}\n\n/**\n * @internal\n */\nexport function definePageBreakCommands(): PageBreakCommandsExtension {\n return defineCommands({\n insertPageBreak: insertPageBreak,\n })\n}\n","import { defineKeymap, type PlainExtension } from '@prosekit/core'\n\nimport { insertPageBreak } from './page-break-commands.ts'\n\n/**\n * @internal\n */\nexport type PageBreakKeymapExtension = PlainExtension\n\n/**\n * @internal\n */\nexport function definePageBreakKeymap(): PageBreakKeymapExtension {\n return defineKeymap({\n 'Mod-Enter': insertPageBreak(),\n })\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { Attrs } from '@prosekit/pm/model'\n\n/**\n * @internal\n */\nexport type PageBreakSpecExtension = Extension<{\n Nodes: {\n pageBreak: Attrs\n }\n}>\n\n/**\n * @internal\n */\nexport function definePageBreakSpec(): PageBreakSpecExtension {\n return defineNodeSpec({\n name: 'pageBreak',\n group: 'block',\n selectable: true,\n parseDOM: [{ tag: 'div.prosekit-page-break' }],\n toDOM() {\n return ['div', { class: 'prosekit-horizontal-rule prosekit-page-break' }, ['hr']]\n },\n pageBreak: true,\n })\n}\n\ndeclare module '@prosekit/pm/model' {\n interface NodeSpec {\n pageBreak?: boolean | undefined\n }\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { definePageBreakCommands, type PageBreakCommandsExtension } from './page-break-commands.ts'\nimport { definePageBreakKeymap, type PageBreakKeymapExtension } from './page-break-keymap.ts'\nimport { definePageBreakSpec, type PageBreakSpecExtension } from './page-break-spec.ts'\n\n/**\n * @internal\n */\nexport type PageBreakExtension = Union<\n [PageBreakSpecExtension, PageBreakCommandsExtension, PageBreakKeymapExtension]\n>\n\n/**\n * @public\n */\nexport function definePageBreak(): PageBreakExtension {\n return union(\n definePageBreakSpec(),\n definePageBreakCommands(),\n definePageBreakKeymap(),\n )\n}\n","import { once } from '@ocavue/utils'\nimport { customElements, HTMLElement } from 'server-dom-shim'\n\n/**\n * @internal\n */\nexport const PAGE_CHUNK_TAG_NAME = 'pm-page-chunk'\n\n/**\n * @internal\n */\nexport function registerPageChunkElement(): void {\n if (typeof window === 'undefined' || customElements.get(PAGE_CHUNK_TAG_NAME)) return\n customElements.define(PAGE_CHUNK_TAG_NAME, PageChunkElement)\n}\n\nclass PageChunkElement extends HTMLElement {\n static observedAttributes = [\n 'data-group',\n 'data-break',\n 'data-h',\n 'data-mt',\n 'data-mb',\n\n // Only the first chunk of the whole document has this attribute.\n 'data-size',\n ]\n\n // Data attributes set by external code\n #group: string = ''\n #forceNextBreak: boolean = false\n #pageHeight: number = 0\n #pageMarginTop: number = 0\n #pageMarginBottom: number = 0\n #size: number | undefined = undefined\n\n // Internal states\n #updateRequested: boolean = false\n #contentBoxHeight: number = 0\n\n // Rendering states\n #isHead: boolean = false\n #isTail: boolean = false\n #paddingTop: number = 0\n #paddingBottom: number = 0\n\n // Pending rendering states\n #isHeadPending: boolean = false\n #isTailPending: boolean = false\n #paddingTopPending: number = 0\n #paddingBottomPending: number = 0\n\n connectedCallback() {\n this.#parseDataAttributes()\n\n if (this.#isLeader()) {\n this.#isHeadPending = true\n }\n\n this.#render()\n\n // Get the initial content box height when the resize observer is not started yet. Notice that\n // `this.clientHeight` is an integer while the content box height can be a float, so this is not\n // accurate but should be good enough for the first render.\n this.#contentBoxHeight = this.clientHeight - this.#paddingTop - this.#paddingBottom\n\n observeElement(this)\n\n this.#requestUpdate()\n }\n\n disconnectedCallback() {\n unobserveElement(this)\n }\n\n attributeChangedCallback(_: string, oldValue: string | null, newValue: string | null) {\n if (oldValue === newValue) return\n this.#parseDataAttributes()\n this.#requestUpdate()\n }\n\n #parseDataAttributes() {\n this.#group = this.getAttribute('data-group') || ''\n this.#forceNextBreak = this.hasAttribute('data-break')\n this.#pageHeight = this.#parseFloatAttribute('data-h')\n this.#pageMarginTop = this.#parseFloatAttribute('data-mt')\n this.#pageMarginBottom = this.#parseFloatAttribute('data-mb')\n\n const sizeAttr = this.getAttribute('data-size')\n this.#size = sizeAttr ? Number.parseInt(sizeAttr, 10) : undefined\n }\n\n #parseFloatAttribute(name: string): number {\n const value = this.getAttribute(name)\n return value != null ? Number.parseFloat(value) : 0\n }\n\n #isLeader() {\n return this.#size != null\n }\n\n #render() {\n if (this.#paddingTop !== this.#paddingTopPending || this.#paddingBottom !== this.#paddingBottomPending) {\n Object.assign(this.style, {\n paddingTop: `${this.#paddingTop = this.#paddingTopPending}px`,\n paddingBottom: `${this.#paddingBottom = this.#paddingBottomPending}px`,\n })\n }\n if (this.#isHead !== this.#isHeadPending) {\n this.toggleAttribute('data-page-head', this.#isHead = this.#isHeadPending)\n }\n if (this.#isTail !== this.#isTailPending) {\n this.toggleAttribute('data-page-tail', this.#isTail = this.#isTailPending)\n }\n }\n\n setHeight(height: number) {\n // Avoid potential float number precision issues\n if (Math.abs(this.#contentBoxHeight - height) < 0.1) {\n return\n }\n this.#contentBoxHeight = height\n this.#requestUpdate()\n }\n\n /**\n * Schedules a batched page layout recalculation.\n *\n * Any chunk can call this method, but the actual layout work (#updateAll)\n * always runs on the leader chunk, because it needs to iterate over every\n * chunk in order to compute page breaks.\n *\n * Two nested microtasks are used to batch updates:\n *\n * Microtask 1 – Delegation: non-leader chunks forward the request to the\n * leader chunk, so multiple chunks changing in the same tick only trigger\n * one layout pass.\n *\n * Microtask 2 – Execution: the leader chunk defers #updateAll to a second\n * microtask so that any other attribute / resize changes that were queued\n * in the same tick (and forwarded during microtask 1) are already reflected\n * before the layout is recalculated.\n *\n * The #updateRequested flag acts as a deduplication guard so that rapid\n * successive calls (e.g. multiple attributes changing at once) result in at\n * most one scheduled pass per chunk.\n */\n #requestUpdate() {\n if (this.#updateRequested) {\n return\n }\n\n this.#updateRequested = true\n queueMicrotask(() => {\n if (!this.#isLeader()) {\n this.#updateRequested = false\n const leader = findLeaderChunk(this, this.#group)\n if (!leader) return\n leader.#requestUpdate()\n return\n }\n queueMicrotask(() => {\n this.#updateRequested = false\n this.#updateAll()\n })\n })\n }\n\n #updateAll() {\n if (!this.isConnected) {\n return\n }\n\n const elements = findAllChunks(this, this.#group)\n const count = elements.length\n if (count === 0) return\n\n const pageHeight = this.#pageHeight\n const pageMarginTop = this.#pageMarginTop\n const maxContentHeight = pageHeight - pageMarginTop - this.#pageMarginBottom\n\n let currentContentHeight = 0\n let forceNextBreak = false\n\n for (let i = 0; i < count; i++) {\n const element = elements[i]\n const h = element.#contentBoxHeight\n const isHead = forceNextBreak || i === 0 || (currentContentHeight + h > maxContentHeight)\n\n forceNextBreak = element.#forceNextBreak\n\n if (isHead && i > 0) {\n const prev = elements[i - 1]\n prev.#paddingBottomPending = Math.max(0, pageHeight - pageMarginTop - currentContentHeight)\n prev.#isTailPending = true\n currentContentHeight = h\n } else {\n currentContentHeight += h\n }\n\n element.#paddingTopPending = isHead ? pageMarginTop : 0\n element.#paddingBottomPending = 0\n element.#isTailPending = false\n element.#isHeadPending = isHead\n }\n\n const last = elements[count - 1]\n last.#paddingBottomPending = Math.max(0, pageHeight - pageMarginTop - currentContentHeight)\n last.#isTailPending = true\n\n for (const element of elements) {\n element.#render()\n }\n }\n}\n\nfunction handleResize(entries: ResizeObserverEntry[]) {\n for (const entry of entries) {\n const contentBoxHeight = entry.contentBoxSize?.[0]?.blockSize ?? entry.contentRect.height\n const element = entry.target as PageChunkElement\n element.setHeight(contentBoxHeight)\n }\n}\n\nconst getResizeObserver = /* @__PURE__ */ once(() => {\n return new ResizeObserver(handleResize)\n})\n\nfunction observeElement(element: PageChunkElement) {\n getResizeObserver().observe(element)\n}\n\nfunction unobserveElement(element: PageChunkElement) {\n getResizeObserver().unobserve(element)\n}\n\nfunction findLeaderChunk(element: HTMLElement, group: string): PageChunkElement | null | undefined {\n const root = element.closest('.ProseMirror')\n return root?.querySelector<PageChunkElement>(`${PAGE_CHUNK_TAG_NAME}[data-group=\"${group}\"][data-size]`)\n}\n\nfunction findAllChunks(element: HTMLElement, group: string): PageChunkElement[] {\n const root = element.closest('.ProseMirror')\n const elements = root?.querySelectorAll<PageChunkElement>(`${PAGE_CHUNK_TAG_NAME}[data-group=\"${group}\"]`)\n return Array.from(elements || [])\n}\n","import { getId } from '@ocavue/utils'\nimport { definePlugin, type Extension } from '@prosekit/core'\nimport type { Node } from '@prosekit/pm/model'\nimport { Plugin, PluginKey } from '@prosekit/pm/state'\nimport { Decoration, DecorationSet } from '@prosekit/pm/view'\n\nimport { PAGE_CHUNK_TAG_NAME, registerPageChunkElement } from './page-element.ts'\n\n/**\n * @public\n */\nexport interface PageRenderingOptions {\n /**\n * The width of the page in px.\n *\n * @default 794 (Portrait A4 paper size in 96 DPI)\n */\n pageWidth?: number\n\n /**\n * The height of the page in px.\n *\n * @default 1123 (Portrait A4 paper size in 96 DPI)\n */\n pageHeight?: number\n\n /**\n * The top margin of the page in px.\n *\n * @default 70\n */\n marginTop?: number\n\n /**\n * The right margin of the page in px.\n *\n * @default 70\n */\n marginRight?: number\n\n /**\n * The bottom margin of the page in px.\n *\n * @default 70\n */\n marginBottom?: number\n\n /**\n * The left margin of the page in px.\n *\n * @default 70\n */\n marginLeft?: number\n}\n\n/**\n * @public\n */\nexport function definePageRendering(options: PageRenderingOptions = {}): PageRenderingExtension {\n return definePlugin(\n createPageRenderingPlugin(options),\n )\n}\n\n/**\n * @internal\n */\nexport type PageRenderingExtension = Extension\n\nfunction createPageRenderingPlugin(options: PageRenderingOptions): Plugin {\n const {\n pageWidth = 794,\n pageHeight = 1123,\n marginTop = 70,\n marginRight = 70,\n marginBottom = 70,\n marginLeft = 70,\n } = options\n\n type PluginState = [group: string, decoration: DecorationSet]\n\n const key = new PluginKey<PluginState>('prosekit-page-render')\n\n function createDecorationSet(doc: Node, group: string): DecorationSet {\n const decorations: Decoration[] = []\n const totalCount = doc.childCount\n\n doc.forEach((node, pos, index) => {\n const isPageBreak: boolean | undefined = node.type.spec.pageBreak\n\n decorations.push(Decoration.node(pos, pos + node.nodeSize, {\n 'nodeName': PAGE_CHUNK_TAG_NAME,\n 'data-group': group,\n 'data-break': isPageBreak ? 'true' : undefined,\n 'data-h': String(pageHeight),\n 'data-mt': String(marginTop),\n 'data-mb': String(marginBottom),\n 'data-size': index === 0 ? String(totalCount) : undefined,\n }))\n })\n\n return DecorationSet.create(doc, decorations)\n }\n\n return new Plugin<PluginState>({\n key,\n view: () => {\n registerPageChunkElement()\n return {}\n },\n state: {\n init: (_config, state): PluginState => {\n const group = `page-group-${getId()}`\n const decoration = createDecorationSet(state.doc, group)\n return [group, decoration]\n },\n apply: (tr, value, oldState, newState): PluginState => {\n if (!tr.docChanged) return value\n\n const [group, decoration] = value\n\n let needRecreate = oldState.doc.childCount !== newState.doc.childCount\n\n if (!needRecreate) {\n const count = oldState.doc.childCount\n for (let i = 0; i < count; i++) {\n const oldNode = oldState.doc.child(i)\n const newNode = newState.doc.child(i)\n if (oldNode.type !== newNode.type) {\n needRecreate = true\n break\n }\n }\n }\n\n if (!needRecreate) {\n const mapped = decoration.map(tr.mapping, tr.doc, {\n onRemove: () => {\n needRecreate = true\n },\n })\n if (!needRecreate) {\n return [group, mapped]\n }\n }\n\n return [group, createDecorationSet(newState.doc, group)]\n },\n },\n props: {\n decorations: (state) => {\n return key.getState(state)?.[1]\n },\n attributes: {\n style: [\n `--page-margin-right:${marginRight}px;`,\n `--page-margin-left:${marginLeft}px;`,\n `--page-width:${pageWidth}px;`,\n `--page-height:${pageHeight}px;`,\n ].join(''),\n },\n },\n })\n}\n"],"mappings":";;;;;;AAaA,MAAM,0BAAmC,OAAO,aAAsB;AACpE,KAAI,CAAC,SAAU,QAAO;CAEtB,MAAM,EAAE,QAAQ,OAAO;CAEvB,MAAM,OADO,YAAY,QAAQ,YAChB,CAAC,eAAe;CACjC,MAAM,MAAM,GAAG,UAAU;CACzB,MAAM,QAAQ,IAAI,MAAM,SAAS,KAAK,KAAK,EAAE,GAAG,EAAE;AAClD,IAAG,aAAa,KAAK,KAAK,MAAM,CAAC,gBAAgB;AACjD,UAAS,GAAG;AACZ,QAAO;;;;;AAMT,SAAgB,kBAA2B;AACzC,QAAO;;;;;AAMT,SAAgB,0BAAsD;AACpE,QAAO,eAAe,EACH,iBAClB,CAAC;;;;;AC3BJ,SAAgB,wBAAkD;AAChE,QAAO,aAAa,EAClB,aAAa,iBAAiB,EAC/B,CAAC;;;;;ACAJ,SAAgB,sBAA8C;AAC5D,QAAO,eAAe;EACpB,MAAM;EACN,OAAO;EACP,YAAY;EACZ,UAAU,CAAC,EAAE,KAAK,2BAA2B,CAAC;EAC9C,QAAQ;AACN,UAAO;IAAC;IAAO,EAAE,OAAO,gDAAgD;IAAE,CAAC,KAAK;IAAC;;EAEnF,WAAW;EACZ,CAAC;;;;;ACTJ,SAAgB,kBAAsC;AACpD,QAAO,MACL,qBAAqB,EACrB,yBAAyB,EACzB,uBAAuB,CACxB;;;;;ACfH,MAAa,sBAAsB;;;;AAKnC,SAAgB,2BAAiC;AAC/C,KAAI,OAAO,WAAW,eAAe,eAAe,IAAA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AAC,aACH,QAAO,CAAC,OAAO,OAAO;;AAI1B,WAAO,CAAC,OAAO,oBAAoB,SAAS,KAAK,MAAM,CAAC;;GAE3D;EACD,OAAO;GACL,cAAc,UAAU;AACtB,WAAO,IAAI,SAAS,MAAM,GAAG;;GAE/B,YAAY,EACV,OAAO;IACL,uBAAuB,YAAY;IACnC,sBAAsB,WAAW;IACjC,gBAAgB,UAAU;IAC1B,iBAAiB,WAAW;IAC7B,CAAC,KAAK,GAAG,EACX;GACF;EACF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"paste-rule2.js","names":[],"sources":["../src/paste-rule/paste-rule-plugin.ts","../src/paste-rule/paste-rule.ts","../src/paste-rule/split-text-by-regex.ts","../src/paste-rule/mark-paste-rule.ts"],"sourcesContent":["import { defineFacet, defineFacetPayload, pluginFacet, type PlainExtension, type PluginPayload } from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport { PluginKey, ProseMirrorPlugin } from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\n\ntype PasteRulePayload = (options: { slice: Slice; view: EditorView; plain: boolean }) => Slice\n\n/**\n * @internal\n */\nconst pasteRuleFacet = defineFacet<PasteRulePayload, PluginPayload>({\n reduce: () => {\n let handlers: PasteRulePayload[] = []\n\n const transformPasted = (slice: Slice, view: EditorView, plain: boolean): Slice => {\n for (const handler of handlers) {\n slice = handler({ slice, view, plain })\n }\n return slice\n }\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-paste-rule'),\n props: {\n transformPasted,\n },\n })\n\n return (inputs: PasteRulePayload[]) => {\n // Last added rule (highest priority) is applied first\n handlers = [...inputs].reverse()\n return plugin\n }\n },\n singleton: true,\n parent: pluginFacet,\n})\n\n/**\n * @internal\n */\nexport function definePasteRulePlugin(payload: PasteRulePayload): PlainExtension {\n return defineFacetPayload(pasteRuleFacet, [payload]) as PlainExtension\n}\n","import type { PlainExtension } from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { definePasteRulePlugin } from './paste-rule-plugin.ts'\n\n/**\n * @public\n *\n * Options for {@link PasteRuleHandler}.\n */\nexport interface PasteRuleHandlerOptions {\n /**\n * The slice to be pasted.\n */\n slice: Slice\n\n /**\n * The editor view.\n */\n view: EditorView\n\n /**\n * Whether the pasted content is treated as plain text. This is true when the\n * `Shift` key is held when pasting.\n */\n plain: boolean\n}\n\n/**\n * @public\n *\n * Can be used to transform pasted or dragged-and-dropped content before it is\n * applied to the document.\n */\nexport type PasteRuleHandler = (options: PasteRuleHandlerOptions) => Slice\n\n/**\n * Options for {@link definePasteRule}.\n *\n * @public\n */\nexport interface PasteRuleOptions {\n /**\n * A function to be called when a paste rule is triggered.\n */\n handler: PasteRuleHandler\n}\n\n/**\n * Defines a paste rule. This rule allows you to modify pasted or dragged\n * content before it is inserted into the document.\n *\n * @param options\n *\n * @public\n */\nexport function definePasteRule({ handler }: PasteRuleOptions): PlainExtension {\n return definePasteRulePlugin(handler)\n}\n","/**\n * Splits text into chunks based on regex matches, preserving both matched and unmatched segments.\n * Returns an array of tuples where each tuple contains a text segment and either the match data\n * (for matched segments) or undefined (for unmatched segments).\n */\nexport function splitTextByRegex(\n text: string,\n regex: RegExp,\n): Array<[string, RegExpExecArray | undefined]> | undefined {\n regex.lastIndex = 0\n\n const chunks: Array<[string, RegExpExecArray | undefined]> = []\n let lastIndex = 0\n let match: RegExpExecArray | null\n let matched = false\n\n while ((match = regex.exec(text))) {\n const start = match.index\n const end = regex.lastIndex\n\n // Push the unmatched prefix, if any.\n if (start > lastIndex) {\n chunks.push([text.slice(lastIndex, start), undefined])\n }\n\n // Push the matched segment.\n chunks.push([text.slice(start, end), match])\n matched = true\n\n if (lastIndex === end) {\n // Safeguard against zero-width matches that would otherwise cause an infinite loop.\n return\n }\n lastIndex = end\n }\n\n if (matched && lastIndex < text.length) {\n chunks.push([text.slice(lastIndex), undefined])\n }\n\n regex.lastIndex = 0\n\n return matched ? chunks : undefined\n}\n","import { getMarkType, type PlainExtension } from '@prosekit/core'\nimport type { Attrs, MarkType, ProseMirrorNode } from '@prosekit/pm/model'\nimport { Fragment, Slice } from '@prosekit/pm/model'\n\nimport { definePasteRule } from './paste-rule.ts'\nimport { splitTextByRegex } from './split-text-by-regex.ts'\n\n/**\n * The options for {@link defineMarkPasteRule}.\n *\n * @public\n */\nexport interface MarkPasteRuleOptions {\n /**\n * The regular expression to match against. It must have a `g` flag to match\n * all instances of the mark.\n */\n regex: RegExp\n\n /**\n * The mark type to apply to the matched text.\n */\n type: string | MarkType\n\n /**\n * A function used to compute attributes to set on the mark created by this\n * rule. When it returns `false`, the rule won't match. When it returns `null`\n * or `undefined`, that is interpreted as an empty/default set of attributes.\n * @default null\n */\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n\n /**\n * Optional function to determine if a text node should be skipped.\n * Default behavior: skip code nodes and nodes that already have the target mark.\n */\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\n/**\n * Defines a paste rule that applies marks based on regex patterns.\n *\n * @public\n */\nexport function defineMarkPasteRule(options: MarkPasteRuleOptions): PlainExtension {\n return definePasteRule({\n handler: ({ slice, view, plain }) => {\n if (plain) {\n return slice\n }\n\n const markType = getMarkType(view.state.schema, options.type)\n\n return replaceMarkInSlice({\n markType,\n regex: options.regex,\n getAttrs: options.getAttrs,\n shouldSkip: options.shouldSkip,\n }, slice)\n },\n })\n}\n\ninterface MarkPasteRuleHandlerOptions {\n markType: MarkType\n regex: RegExp\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\nfunction replaceMarkInSlice(options: MarkPasteRuleHandlerOptions, slice: Slice): Slice {\n const newFragment = replaceMarkInFragment(options, slice.content)\n if (!newFragment) {\n return slice\n }\n return new Slice(newFragment, slice.openStart, slice.openEnd)\n}\n\nfunction replaceMarkInFragment(options: MarkPasteRuleHandlerOptions, fragment: Fragment): Fragment | undefined {\n let changed = false\n const children: ProseMirrorNode[] = []\n\n for (const child of fragment.content) {\n const newChild = replaceMarkInNode(options, child)\n if (newChild) {\n changed = true\n }\n children.push(newChild || child)\n }\n\n if (changed) {\n return Fragment.from(children)\n }\n\n return\n}\n\nfunction replaceMarkInNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n if (node.type.spec.code) {\n return\n }\n if (node.type.isInline) {\n return\n }\n if (node.type.isTextblock) {\n return replaceMarkInTextblockNode(options, node)\n }\n\n const newChildren = replaceMarkInFragment(options, node.content)\n if (!newChildren) {\n return\n }\n return node.copy(newChildren)\n}\n\nfunction replaceMarkInTextblockNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n const newChildren: ProseMirrorNode[] = []\n let changed = false\n\n for (const inlineNode of node.content.content) {\n const newInlineNodes = replaceMarkInInlineNode(options, inlineNode)\n if (newInlineNodes) {\n changed = true\n newChildren.push(...newInlineNodes)\n } else {\n newChildren.push(inlineNode)\n }\n }\n if (changed) {\n return node.copy(Fragment.from(newChildren))\n }\n return\n}\n\nfunction replaceMarkInInlineNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode[] | undefined {\n const text = node.text\n if (!text) {\n return\n }\n\n const { markType, shouldSkip } = options\n\n // Use custom skip logic if provided, otherwise use default\n if (shouldSkip) {\n if (shouldSkip(node)) {\n return\n }\n } else {\n // Default skip logic: skip if already has the target mark or has code mark\n if (node.marks.some((mark) => mark.type === markType)) {\n return\n }\n if (node.marks.some((mark) => mark.type.spec.code)) {\n return\n }\n }\n\n const chunks = splitTextByRegex(text, options.regex)\n if (!chunks) {\n return\n }\n\n const schema = node.type.schema\n const nodes: ProseMirrorNode[] = []\n\n for (const [text, match] of chunks) {\n if (!text) {\n continue\n }\n if (match) {\n const attrs = options.getAttrs?.(match) ?? null\n if (attrs !== false) {\n const mark = markType.create(attrs)\n nodes.push(schema.text(text, [...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,WAAW;;AAGhC,KAAI,QACF,QAAO,KAAK,KAAK,SAAS,KAAK,YAAY,CAAC;;AAKhD,SAAS,wBAAwB,SAAsC,MAAsD;CAC3H,MAAM,OAAO,KAAK;AAClB,KAAI,CAAC,KACH;CAGF,MAAM,EAAE,UAAU,eAAe;AAGjC,KAAI;MACE,WAAW,KAAK,CAClB;QAEG;AAEL,MAAI,KAAK,MAAM,MAAM,SAAS,KAAK,SAAS,SAAS,CACnD;AAEF,MAAI,KAAK,MAAM,MAAM,SAAS,KAAK,KAAK,KAAK,KAAK,CAChD;;CAIJ,MAAM,SAAS,iBAAiB,MAAM,QAAQ,MAAM;AACpD,KAAI,CAAC,OACH;CAGF,MAAM,SAAS,KAAK,KAAK;CACzB,MAAM,QAA2B,EAAE;AAEnC,MAAK,MAAM,CAAC,MAAM,UAAU,QAAQ;AAClC,MAAI,CAAC,KACH;AAEF,MAAI,OAAO;GACT,MAAM,QAAQ,QAAQ,WAAW,MAAM,IAAI;AAC3C,OAAI,UAAU,OAAO;IACnB,MAAM,OAAO,SAAS,OAAO,MAAM;AACnC,UAAM,KAAK,OAAO,KAAK,MAAM,CAAC,GAAG,KAAK,OAAO,KAAK,CAAC,CAAC;SAEpD,OAAM,KAAK,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC;QAG3C,OAAM,KAAK,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC;;AAI7C,QAAO"}
1
+ {"version":3,"file":"paste-rule2.js","names":[],"sources":["../src/paste-rule/paste-rule-plugin.ts","../src/paste-rule/paste-rule.ts","../src/paste-rule/split-text-by-regex.ts","../src/paste-rule/mark-paste-rule.ts"],"sourcesContent":["import { defineFacet, defineFacetPayload, pluginFacet, type PlainExtension, type PluginPayload } from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport { PluginKey, ProseMirrorPlugin } from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\n\ntype PasteRulePayload = (options: { slice: Slice; view: EditorView; plain: boolean }) => Slice\n\n/**\n * @internal\n */\nconst pasteRuleFacet = defineFacet<PasteRulePayload, PluginPayload>({\n reduce: () => {\n let handlers: PasteRulePayload[] = []\n\n const transformPasted = (slice: Slice, view: EditorView, plain: boolean): Slice => {\n for (const handler of handlers) {\n slice = handler({ slice, view, plain })\n }\n return slice\n }\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-paste-rule'),\n props: {\n transformPasted,\n },\n })\n\n return (inputs: PasteRulePayload[]) => {\n // Last added rule (highest priority) is applied first\n handlers = [...inputs].reverse()\n return plugin\n }\n },\n singleton: true,\n parent: pluginFacet,\n})\n\n/**\n * @internal\n */\nexport function definePasteRulePlugin(payload: PasteRulePayload): PlainExtension {\n return defineFacetPayload(pasteRuleFacet, [payload]) as PlainExtension\n}\n","import type { PlainExtension } from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { definePasteRulePlugin } from './paste-rule-plugin.ts'\n\n/**\n * @public\n *\n * Options for {@link PasteRuleHandler}.\n */\nexport interface PasteRuleHandlerOptions {\n /**\n * The slice to be pasted.\n */\n slice: Slice\n\n /**\n * The editor view.\n */\n view: EditorView\n\n /**\n * Whether the pasted content is treated as plain text. This is true when the\n * `Shift` key is held when pasting.\n */\n plain: boolean\n}\n\n/**\n * @public\n *\n * Can be used to transform pasted or dragged-and-dropped content before it is\n * applied to the document.\n */\nexport type PasteRuleHandler = (options: PasteRuleHandlerOptions) => Slice\n\n/**\n * Options for {@link definePasteRule}.\n *\n * @public\n */\nexport interface PasteRuleOptions {\n /**\n * A function to be called when a paste rule is triggered.\n */\n handler: PasteRuleHandler\n}\n\n/**\n * Defines a paste rule. This rule allows you to modify pasted or dragged\n * content before it is inserted into the document.\n *\n * @param options\n *\n * @public\n */\nexport function definePasteRule({ handler }: PasteRuleOptions): PlainExtension {\n return definePasteRulePlugin(handler)\n}\n","/**\n * Splits text into chunks based on regex matches, preserving both matched and unmatched segments.\n * Returns an array of tuples where each tuple contains a text segment and either the match data\n * (for matched segments) or undefined (for unmatched segments).\n */\nexport function splitTextByRegex(\n text: string,\n regex: RegExp,\n): Array<[string, RegExpExecArray | undefined]> | undefined {\n regex.lastIndex = 0\n\n const chunks: Array<[string, RegExpExecArray | undefined]> = []\n let lastIndex = 0\n let match: RegExpExecArray | null\n let matched = false\n\n while ((match = regex.exec(text))) {\n const start = match.index\n const end = regex.lastIndex\n\n // Push the unmatched prefix, if any.\n if (start > lastIndex) {\n chunks.push([text.slice(lastIndex, start), undefined])\n }\n\n // Push the matched segment.\n chunks.push([text.slice(start, end), match])\n matched = true\n\n if (lastIndex === end) {\n // Safeguard against zero-width matches that would otherwise cause an infinite loop.\n return\n }\n lastIndex = end\n }\n\n if (matched && lastIndex < text.length) {\n chunks.push([text.slice(lastIndex), undefined])\n }\n\n regex.lastIndex = 0\n\n return matched ? chunks : undefined\n}\n","import { getMarkType, type PlainExtension } from '@prosekit/core'\nimport type { Attrs, MarkType, ProseMirrorNode } from '@prosekit/pm/model'\nimport { Fragment, Slice } from '@prosekit/pm/model'\n\nimport { definePasteRule } from './paste-rule.ts'\nimport { splitTextByRegex } from './split-text-by-regex.ts'\n\n/**\n * The options for {@link defineMarkPasteRule}.\n *\n * @public\n */\nexport interface MarkPasteRuleOptions {\n /**\n * The regular expression to match against. It must have a `g` flag to match\n * all instances of the mark.\n */\n regex: RegExp\n\n /**\n * The mark type to apply to the matched text.\n */\n type: string | MarkType\n\n /**\n * A function used to compute attributes to set on the mark created by this\n * rule. When it returns `false`, the rule won't match. When it returns `null`\n * or `undefined`, that is interpreted as an empty/default set of attributes.\n * @default null\n */\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n\n /**\n * Optional function to determine if a text node should be skipped.\n * Default behavior: skip code nodes and nodes that already have the target mark.\n */\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\n/**\n * Defines a paste rule that applies marks based on regex patterns.\n *\n * @public\n */\nexport function defineMarkPasteRule(options: MarkPasteRuleOptions): PlainExtension {\n return definePasteRule({\n handler: ({ slice, view, plain }) => {\n if (plain) {\n return slice\n }\n\n const markType = getMarkType(view.state.schema, options.type)\n\n return replaceMarkInSlice({\n markType,\n regex: options.regex,\n getAttrs: options.getAttrs,\n shouldSkip: options.shouldSkip,\n }, slice)\n },\n })\n}\n\ninterface MarkPasteRuleHandlerOptions {\n markType: MarkType\n regex: RegExp\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\nfunction replaceMarkInSlice(options: MarkPasteRuleHandlerOptions, slice: Slice): Slice {\n const newFragment = replaceMarkInFragment(options, slice.content)\n if (!newFragment) {\n return slice\n }\n return new Slice(newFragment, slice.openStart, slice.openEnd)\n}\n\nfunction replaceMarkInFragment(options: MarkPasteRuleHandlerOptions, fragment: Fragment): Fragment | undefined {\n let changed = false\n const children: ProseMirrorNode[] = []\n\n for (const child of fragment.content) {\n const newChild = replaceMarkInNode(options, child)\n if (newChild) {\n changed = true\n }\n children.push(newChild || child)\n }\n\n if (changed) {\n return Fragment.from(children)\n }\n\n return\n}\n\nfunction replaceMarkInNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n if (node.type.spec.code) {\n return\n }\n if (node.type.isInline) {\n return\n }\n if (node.type.isTextblock) {\n return replaceMarkInTextblockNode(options, node)\n }\n\n const newChildren = replaceMarkInFragment(options, node.content)\n if (!newChildren) {\n return\n }\n return node.copy(newChildren)\n}\n\nfunction replaceMarkInTextblockNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n const newChildren: ProseMirrorNode[] = []\n let changed = false\n\n for (const inlineNode of node.content.content) {\n const newInlineNodes = replaceMarkInInlineNode(options, inlineNode)\n if (newInlineNodes) {\n changed = true\n newChildren.push(...newInlineNodes)\n } else {\n newChildren.push(inlineNode)\n }\n }\n if (changed) {\n return node.copy(Fragment.from(newChildren))\n }\n return\n}\n\nfunction replaceMarkInInlineNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode[] | undefined {\n const text = node.text\n if (!text) {\n return\n }\n\n const { markType, shouldSkip } = options\n\n // Use custom skip logic if provided, otherwise use default\n if (shouldSkip) {\n if (shouldSkip(node)) {\n return\n }\n } else {\n // Default skip logic: skip if already has the target mark or has code mark\n if (node.marks.some((mark) => mark.type === markType)) {\n return\n }\n if (node.marks.some((mark) => mark.type.spec.code)) {\n return\n }\n }\n\n const chunks = splitTextByRegex(text, options.regex)\n if (!chunks) {\n return\n }\n\n const schema = node.type.schema\n const nodes: ProseMirrorNode[] = []\n\n for (const [text, match] of chunks) {\n if (!text) {\n continue\n }\n if (match) {\n const attrs = options.getAttrs?.(match) ?? null\n if (attrs !== false) {\n const mark = markType.create(attrs)\n nodes.push(schema.text(text, [...node.marks, mark]))\n } else {\n nodes.push(schema.text(text, node.marks))\n }\n } else {\n nodes.push(schema.text(text, node.marks))\n }\n }\n\n return nodes\n}\n"],"mappings":";;;;;;AAUA,MAAM,iBAAiB,YAA6C;CAClE,cAAc;EACZ,IAAI,WAA+B,EAAE;EAErC,MAAM,mBAAmB,OAAc,MAAkB,UAA0B;AACjF,QAAK,MAAM,WAAW,SACpB,SAAQ,QAAQ;IAAE;IAAO;IAAM;IAAO,CAAC;AAEzC,UAAO;;EAGT,MAAM,SAAS,IAAI,kBAAkB;GACnC,KAAK,IAAI,UAAU,sBAAsB;GACzC,OAAO,EACL,iBACD;GACF,CAAC;AAEF,UAAQ,WAA+B;AAErC,cAAW,CAAC,GAAG,OAAO,CAAC,SAAS;AAChC,UAAO;;;CAGX,WAAW;CACX,QAAQ;CACT,CAAC;;;;AAKF,SAAgB,sBAAsB,SAA2C;AAC/E,QAAO,mBAAmB,gBAAgB,CAAC,QAAQ,CAAC;;;;;;;;;;ACetD,SAAgB,gBAAgB,EAAE,WAA6C;AAC7E,QAAO,sBAAsB,QAAQ;;;;;;;ACrDvC,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,KAAK,MAAM,CAAC,GAAG,KAAK,OAAO,KAAK,CAAC,CAAC;SAEpD,OAAM,KAAK,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC;QAG3C,OAAM,KAAK,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC;;AAI7C,QAAO"}
@@ -1 +1 @@
1
- {"version":3,"file":"placeholder.js","names":[],"sources":["../src/placeholder/index.ts"],"sourcesContent":["import { definePlugin, isInCodeBlock, isTextSelection, maybeRun, type PlainExtension } from '@prosekit/core'\nimport type { ProseMirrorNode } from '@prosekit/pm/model'\nimport { Plugin, PluginKey, type EditorState } from '@prosekit/pm/state'\nimport { Decoration, DecorationSet } from '@prosekit/pm/view'\n\nimport { findTable } from '../table/index.ts'\n\nexport interface PlaceholderOptions {\n /**\n * The placeholder to use. It can be a static string or a function that\n * receives the current editor state and returns a string.\n */\n placeholder: string | ((state: EditorState) => string)\n\n /**\n * By default, the placeholder text will be shown whenever the current text\n * cursor is in an empty text node and it's not inside a code block or a\n * table node.\n *\n * If you only want to show the placeholder when the whole doc is empty, you\n * can set this option to 'doc'.\n *\n * You can also pass a function that receives the current editor state and\n * returns a boolean value to determine whether the placeholder should be\n * shown.\n *\n * @default 'block'\n */\n strategy?: 'doc' | 'block' | ((state: EditorState) => boolean)\n}\n\n/**\n * Add a placeholder text to the editor when the current block or document is\n * empty.\n */\nexport function definePlaceholder(options: PlaceholderOptions): PlainExtension {\n return definePlugin(createPlaceholderPlugin(options))\n}\n\nfunction createPlaceholderPlugin({\n placeholder,\n strategy = 'block',\n}: PlaceholderOptions): Plugin {\n return new Plugin({\n key: new PluginKey('prosekit-placeholder'),\n props: {\n decorations: (state) => {\n const strategyFn = typeof strategy === 'function'\n ? strategy\n : strategy === 'doc'\n ? docStrategy\n : defaultStrategy\n\n if (!strategyFn(state)) {\n return null\n }\n\n const placeholderText: string = maybeRun(placeholder, state)\n const deco = createPlaceholderDecoration(state, placeholderText)\n if (!deco) {\n return null\n }\n\n return DecorationSet.create(state.doc, [deco])\n },\n },\n })\n}\n\nfunction defaultStrategy(state: EditorState): boolean {\n return !isInCodeBlock(state.selection) && !findTable(state.selection.$from)\n}\n\nfunction docStrategy(state: EditorState): boolean {\n return isDocEmpty(state.doc) && defaultStrategy(state)\n}\n\nfunction isDocEmpty(doc: ProseMirrorNode) {\n return doc.childCount <= 1 && !doc.firstChild?.content.size\n}\n\nfunction createPlaceholderDecoration(\n state: EditorState,\n placeholderText: string,\n): Decoration | null {\n if (!placeholderText) return null\n\n const { selection } = state\n if (!selection.empty || !isTextSelection(selection)) return null\n\n const $pos = selection.$anchor\n const node = $pos.parent\n if (node.content.size > 0) return null\n\n const before = $pos.before()\n\n return Decoration.node(before, before + node.nodeSize, {\n 'class': 'prosekit-placeholder',\n 'data-placeholder': placeholderText,\n })\n}\n"],"mappings":";;;;;;;;AAmCA,SAAgB,kBAAkB,SAA6C;AAC7E,QAAO,aAAa,wBAAwB,QAAQ,CAAC;;AAGvD,SAAS,wBAAwB,EAC/B,aACA,WAAW,WACkB;AAC7B,QAAO,IAAI,OAAO;EAChB,KAAK,IAAI,UAAU,uBAAuB;EAC1C,OAAO,EACL,cAAc,UAAU;AAOtB,OAAI,EANe,OAAO,aAAa,aACnC,WACA,aAAa,QACb,cACA,iBAEY,MAAM,CACpB,QAAO;GAIT,MAAM,OAAO,4BAA4B,OADT,SAAS,aAAa,MAAM,CACI;AAChE,OAAI,CAAC,KACH,QAAO;AAGT,UAAO,cAAc,OAAO,MAAM,KAAK,CAAC,KAAK,CAAC;KAEjD;EACF,CAAC;;AAGJ,SAAS,gBAAgB,OAA6B;AACpD,QAAO,CAAC,cAAc,MAAM,UAAU,IAAI,CAAC,UAAU,MAAM,UAAU,MAAM;;AAG7E,SAAS,YAAY,OAA6B;AAChD,QAAO,WAAW,MAAM,IAAI,IAAI,gBAAgB,MAAM;;AAGxD,SAAS,WAAW,KAAsB;AACxC,QAAO,IAAI,cAAc,KAAK,CAAC,IAAI,YAAY,QAAQ;;AAGzD,SAAS,4BACP,OACA,iBACmB;AACnB,KAAI,CAAC,gBAAiB,QAAO;CAE7B,MAAM,EAAE,cAAc;AACtB,KAAI,CAAC,UAAU,SAAS,CAAC,gBAAgB,UAAU,CAAE,QAAO;CAE5D,MAAM,OAAO,UAAU;CACvB,MAAM,OAAO,KAAK;AAClB,KAAI,KAAK,QAAQ,OAAO,EAAG,QAAO;CAElC,MAAM,SAAS,KAAK,QAAQ;AAE5B,QAAO,WAAW,KAAK,QAAQ,SAAS,KAAK,UAAU;EACrD,SAAS;EACT,oBAAoB;EACrB,CAAC"}
1
+ {"version":3,"file":"placeholder.js","names":[],"sources":["../src/placeholder/index.ts"],"sourcesContent":["import { definePlugin, isInCodeBlock, isTextSelection, maybeRun, type PlainExtension } from '@prosekit/core'\nimport type { ProseMirrorNode } from '@prosekit/pm/model'\nimport { Plugin, PluginKey, type EditorState } from '@prosekit/pm/state'\nimport { Decoration, DecorationSet } from '@prosekit/pm/view'\n\nimport { findTable } from '../table/index.ts'\n\nexport interface PlaceholderOptions {\n /**\n * The placeholder to use. It can be a static string or a function that\n * receives the current editor state and returns a string.\n */\n placeholder: string | ((state: EditorState) => string)\n\n /**\n * By default, the placeholder text will be shown whenever the current text\n * cursor is in an empty text node and it's not inside a code block or a\n * table node.\n *\n * If you only want to show the placeholder when the whole doc is empty, you\n * can set this option to 'doc'.\n *\n * You can also pass a function that receives the current editor state and\n * returns a boolean value to determine whether the placeholder should be\n * shown.\n *\n * @default 'block'\n */\n strategy?: 'doc' | 'block' | ((state: EditorState) => boolean)\n}\n\n/**\n * Add a placeholder text to the editor when the current block or document is\n * empty.\n */\nexport function definePlaceholder(options: PlaceholderOptions): PlainExtension {\n return definePlugin(createPlaceholderPlugin(options))\n}\n\nfunction createPlaceholderPlugin({\n placeholder,\n strategy = 'block',\n}: PlaceholderOptions): Plugin {\n return new Plugin({\n key: new PluginKey('prosekit-placeholder'),\n props: {\n decorations: (state) => {\n const strategyFn = typeof strategy === 'function'\n ? strategy\n : strategy === 'doc'\n ? docStrategy\n : defaultStrategy\n\n if (!strategyFn(state)) {\n return null\n }\n\n const placeholderText: string = maybeRun(placeholder, state)\n const deco = createPlaceholderDecoration(state, placeholderText)\n if (!deco) {\n return null\n }\n\n return DecorationSet.create(state.doc, [deco])\n },\n },\n })\n}\n\nfunction defaultStrategy(state: EditorState): boolean {\n return !isInCodeBlock(state.selection) && !findTable(state.selection.$from)\n}\n\nfunction docStrategy(state: EditorState): boolean {\n return isDocEmpty(state.doc) && defaultStrategy(state)\n}\n\nfunction isDocEmpty(doc: ProseMirrorNode) {\n return doc.childCount <= 1 && !doc.firstChild?.content.size\n}\n\nfunction createPlaceholderDecoration(\n state: EditorState,\n placeholderText: string,\n): Decoration | null {\n if (!placeholderText) return null\n\n const { selection } = state\n if (!selection.empty || !isTextSelection(selection)) return null\n\n const $pos = selection.$anchor\n const node = $pos.parent\n if (node.content.size > 0) return null\n\n const before = $pos.before()\n\n return Decoration.node(before, before + node.nodeSize, {\n 'class': 'prosekit-placeholder',\n 'data-placeholder': placeholderText,\n })\n}\n"],"mappings":";;;;;;;;AAmCA,SAAgB,kBAAkB,SAA6C;AAC7E,QAAO,aAAa,wBAAwB,QAAQ,CAAC;;AAGvD,SAAS,wBAAwB,EAC/B,aACA,WAAW,WACkB;AAC7B,QAAO,IAAI,OAAO;EAChB,KAAK,IAAI,UAAU,uBAAuB;EAC1C,OAAO,EACL,cAAc,UAAU;AAOtB,OAAI,EANe,OAAO,aAAa,aACnC,WACA,aAAa,QACb,cACA,iBAEY,MAAM,CACpB,QAAO;GAIT,MAAM,OAAO,4BAA4B,OADT,SAAS,aAAa,MACS,CAAC;AAChE,OAAI,CAAC,KACH,QAAO;AAGT,UAAO,cAAc,OAAO,MAAM,KAAK,CAAC,KAAK,CAAC;KAEjD;EACF,CAAC;;AAGJ,SAAS,gBAAgB,OAA6B;AACpD,QAAO,CAAC,cAAc,MAAM,UAAU,IAAI,CAAC,UAAU,MAAM,UAAU,MAAM;;AAG7E,SAAS,YAAY,OAA6B;AAChD,QAAO,WAAW,MAAM,IAAI,IAAI,gBAAgB,MAAM;;AAGxD,SAAS,WAAW,KAAsB;AACxC,QAAO,IAAI,cAAc,KAAK,CAAC,IAAI,YAAY,QAAQ;;AAGzD,SAAS,4BACP,OACA,iBACmB;AACnB,KAAI,CAAC,gBAAiB,QAAO;CAE7B,MAAM,EAAE,cAAc;AACtB,KAAI,CAAC,UAAU,SAAS,CAAC,gBAAgB,UAAU,CAAE,QAAO;CAE5D,MAAM,OAAO,UAAU;CACvB,MAAM,OAAO,KAAK;AAClB,KAAI,KAAK,QAAQ,OAAO,EAAG,QAAO;CAElC,MAAM,SAAS,KAAK,QAAQ;AAE5B,QAAO,WAAW,KAAK,QAAQ,SAAS,KAAK,UAAU;EACrD,SAAS;EACT,oBAAoB;EACrB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"search.js","names":[],"sources":["../src/search/index.ts"],"sourcesContent":["import { defineCommands, definePlugin, type Extension, type PlainExtension } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\nimport {\n findNext,\n findNextNoWrap,\n findPrev,\n findPrevNoWrap,\n replaceAll,\n replaceCurrent,\n replaceNext,\n replaceNextNoWrap,\n search,\n SearchQuery,\n} from 'prosemirror-search'\n\n/**\n * Options for {@link defineSearchQuery}\n *\n * @public\n */\nexport interface SearchQueryOptions {\n /**\n * The search string (or regular expression).\n */\n search: string\n\n /**\n * The replace text.\n */\n replace?: string\n\n /**\n * Indicates whether the search is case-sensitive\n *\n * @default false\n */\n caseSensitive?: boolean\n\n /**\n * By default, string search will replace `\\n`, `\\r`, and `\\t` in the query\n * with newline, return, and tab characters. When this is set to true, that\n * behavior is disabled.\n *\n * @default false\n */\n literal?: boolean\n\n /**\n * When true, the search string is interpreted as a regular expression.\n *\n * @default false\n */\n regexp?: boolean\n\n /**\n * Enable whole-word matching.\n *\n * @default false\n */\n wholeWord?: boolean\n}\n\n/**\n * Defines an extension that stores a current search query and replace string.\n *\n * @public\n */\nexport function defineSearchQuery(options: SearchQueryOptions): PlainExtension {\n const query = new SearchQuery(options)\n return definePlugin(search({ initialQuery: query }))\n}\n\n/**\n * Scrolls the active search match into view.\n */\nfunction scrollActiveIntoView(view: EditorView) {\n if (view.isDestroyed) return\n const active = view.dom.querySelector('.ProseMirror-active-search-match')\n active?.scrollIntoView({\n block: 'nearest',\n inline: 'nearest',\n behavior: 'smooth',\n })\n}\n\n/**\n * Wraps a command and scrolls the active search match into view when the command\n * is applied.\n */\nfunction withScrollActiveIntoView(command: Command): Command {\n return (state, dispatch, view) => {\n const result = command(state, dispatch, view)\n if (result && dispatch && view) {\n // Add a small delay because the command itself will handle scrolling if\n // the view is focused.\n setTimeout(() => scrollActiveIntoView(view), 50)\n }\n return result\n }\n}\n\n/**\n * @internal\n */\nexport type SearchCommandsExtension = Extension<{\n Commands: {\n findNext: []\n findPrev: []\n findNextNoWrap: []\n findPrevNoWrap: []\n replaceNext: []\n replaceNextNoWrap: []\n replaceCurrent: []\n replaceAll: []\n }\n}>\n\n/**\n * Defines commands for search and replace.\n *\n * @public\n */\nexport function defineSearchCommands(): SearchCommandsExtension {\n return defineCommands({\n findNext: () => withScrollActiveIntoView(findNext),\n findPrev: () => withScrollActiveIntoView(findPrev),\n findNextNoWrap: () => withScrollActiveIntoView(findNextNoWrap),\n findPrevNoWrap: () => withScrollActiveIntoView(findPrevNoWrap),\n replaceNext: () => withScrollActiveIntoView(replaceNext),\n replaceNextNoWrap: () => withScrollActiveIntoView(replaceNextNoWrap),\n replaceCurrent: () => withScrollActiveIntoView(replaceCurrent),\n replaceAll: () => withScrollActiveIntoView(replaceAll),\n })\n}\n"],"mappings":";;;;;;;AAoEA,SAAgB,kBAAkB,SAA6C;AAE7E,QAAO,aAAa,OAAO,EAAE,cADf,IAAI,YAAY,QAAQ,EACY,CAAC,CAAC;;;;;AAMtD,SAAS,qBAAqB,MAAkB;AAC9C,KAAI,KAAK,YAAa;AACP,MAAK,IAAI,cAAc,mCAAmC,EACjE,eAAe;EACrB,OAAO;EACP,QAAQ;EACR,UAAU;EACX,CAAC;;;;;;AAOJ,SAAS,yBAAyB,SAA2B;AAC3D,SAAQ,OAAO,UAAU,SAAS;EAChC,MAAM,SAAS,QAAQ,OAAO,UAAU,KAAK;AAC7C,MAAI,UAAU,YAAY,KAGxB,kBAAiB,qBAAqB,KAAK,EAAE,GAAG;AAElD,SAAO;;;;;;;;AAyBX,SAAgB,uBAAgD;AAC9D,QAAO,eAAe;EACpB,gBAAgB,yBAAyB,SAAS;EAClD,gBAAgB,yBAAyB,SAAS;EAClD,sBAAsB,yBAAyB,eAAe;EAC9D,sBAAsB,yBAAyB,eAAe;EAC9D,mBAAmB,yBAAyB,YAAY;EACxD,yBAAyB,yBAAyB,kBAAkB;EACpE,sBAAsB,yBAAyB,eAAe;EAC9D,kBAAkB,yBAAyB,WAAW;EACvD,CAAC"}
1
+ {"version":3,"file":"search.js","names":[],"sources":["../src/search/index.ts"],"sourcesContent":["import { defineCommands, definePlugin, type Extension, type PlainExtension } from '@prosekit/core'\nimport type { Command } from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\nimport {\n findNext,\n findNextNoWrap,\n findPrev,\n findPrevNoWrap,\n replaceAll,\n replaceCurrent,\n replaceNext,\n replaceNextNoWrap,\n search,\n SearchQuery,\n} from 'prosemirror-search'\n\n/**\n * Options for {@link defineSearchQuery}\n *\n * @public\n */\nexport interface SearchQueryOptions {\n /**\n * The search string (or regular expression).\n */\n search: string\n\n /**\n * The replace text.\n */\n replace?: string\n\n /**\n * Indicates whether the search is case-sensitive\n *\n * @default false\n */\n caseSensitive?: boolean\n\n /**\n * By default, string search will replace `\\n`, `\\r`, and `\\t` in the query\n * with newline, return, and tab characters. When this is set to true, that\n * behavior is disabled.\n *\n * @default false\n */\n literal?: boolean\n\n /**\n * When true, the search string is interpreted as a regular expression.\n *\n * @default false\n */\n regexp?: boolean\n\n /**\n * Enable whole-word matching.\n *\n * @default false\n */\n wholeWord?: boolean\n}\n\n/**\n * Defines an extension that stores a current search query and replace string.\n *\n * @public\n */\nexport function defineSearchQuery(options: SearchQueryOptions): PlainExtension {\n const query = new SearchQuery(options)\n return definePlugin(search({ initialQuery: query }))\n}\n\n/**\n * Scrolls the active search match into view.\n */\nfunction scrollActiveIntoView(view: EditorView) {\n if (view.isDestroyed) return\n const active = view.dom.querySelector('.ProseMirror-active-search-match')\n active?.scrollIntoView({\n block: 'nearest',\n inline: 'nearest',\n behavior: 'smooth',\n })\n}\n\n/**\n * Wraps a command and scrolls the active search match into view when the command\n * is applied.\n */\nfunction withScrollActiveIntoView(command: Command): Command {\n return (state, dispatch, view) => {\n const result = command(state, dispatch, view)\n if (result && dispatch && view) {\n // Add a small delay because the command itself will handle scrolling if\n // the view is focused.\n setTimeout(() => scrollActiveIntoView(view), 50)\n }\n return result\n }\n}\n\n/**\n * @internal\n */\nexport type SearchCommandsExtension = Extension<{\n Commands: {\n findNext: []\n findPrev: []\n findNextNoWrap: []\n findPrevNoWrap: []\n replaceNext: []\n replaceNextNoWrap: []\n replaceCurrent: []\n replaceAll: []\n }\n}>\n\n/**\n * Defines commands for search and replace.\n *\n * @public\n */\nexport function defineSearchCommands(): SearchCommandsExtension {\n return defineCommands({\n findNext: () => withScrollActiveIntoView(findNext),\n findPrev: () => withScrollActiveIntoView(findPrev),\n findNextNoWrap: () => withScrollActiveIntoView(findNextNoWrap),\n findPrevNoWrap: () => withScrollActiveIntoView(findPrevNoWrap),\n replaceNext: () => withScrollActiveIntoView(replaceNext),\n replaceNextNoWrap: () => withScrollActiveIntoView(replaceNextNoWrap),\n replaceCurrent: () => withScrollActiveIntoView(replaceCurrent),\n replaceAll: () => withScrollActiveIntoView(replaceAll),\n })\n}\n"],"mappings":";;;;;;;AAoEA,SAAgB,kBAAkB,SAA6C;AAE7E,QAAO,aAAa,OAAO,EAAE,cAAc,IADzB,YAAY,QACkB,EAAE,CAAC,CAAC;;;;;AAMtD,SAAS,qBAAqB,MAAkB;AAC9C,KAAI,KAAK,YAAa;AACP,MAAK,IAAI,cAAc,mCAChC,EAAE,eAAe;EACrB,OAAO;EACP,QAAQ;EACR,UAAU;EACX,CAAC;;;;;;AAOJ,SAAS,yBAAyB,SAA2B;AAC3D,SAAQ,OAAO,UAAU,SAAS;EAChC,MAAM,SAAS,QAAQ,OAAO,UAAU,KAAK;AAC7C,MAAI,UAAU,YAAY,KAGxB,kBAAiB,qBAAqB,KAAK,EAAE,GAAG;AAElD,SAAO;;;;;;;;AAyBX,SAAgB,uBAAgD;AAC9D,QAAO,eAAe;EACpB,gBAAgB,yBAAyB,SAAS;EAClD,gBAAgB,yBAAyB,SAAS;EAClD,sBAAsB,yBAAyB,eAAe;EAC9D,sBAAsB,yBAAyB,eAAe;EAC9D,mBAAmB,yBAAyB,YAAY;EACxD,yBAAyB,yBAAyB,kBAAkB;EACpE,sBAAsB,yBAAyB,eAAe;EAC9D,kBAAkB,yBAAyB,WAAW;EACvD,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"shiki-highlighter-chunk.js","names":[],"sources":["../src/code-block/shiki-highlighter-chunk.ts"],"sourcesContent":["import type { BundledHighlighterOptions, BundledLanguage, BundledTheme, Highlighter, SpecialLanguage } from 'shiki'\nimport { createHighlighter } from 'shiki/bundle/full'\nimport { createJavaScriptRegexEngine } from 'shiki/engine/javascript'\n\nlet highlighterPromise: Promise<void> | undefined\nlet highlighter: Highlighter | undefined\nconst loadedLangs = new Set<BundledLanguage | SpecialLanguage>()\nconst loadedThemes = new Set<BundledTheme>()\n\nexport interface ShikiHighlighterOptions extends BundledHighlighterOptions<BundledLanguage, BundledTheme> {}\n\nexport interface HighlighterOptions extends Omit<ShikiHighlighterOptions, 'langs' | 'themes'> {\n themes: BundledTheme[]\n langs: (BundledLanguage | SpecialLanguage)[]\n}\n\nfunction ensureHighlighter({\n ...options\n}: HighlighterOptions): Promise<void> {\n if (!highlighterPromise) {\n // If no engine is provided, use the JavaScript engine, which is\n // smaller than the WASM engine.\n if (!options.engine) {\n const engine = createJavaScriptRegexEngine({ forgiving: true })\n options.engine = engine\n }\n highlighterPromise = createHighlighter(options).then((createdHighlighter) => {\n highlighter = createdHighlighter\n })\n }\n return highlighterPromise\n}\n\nasync function loadLanguages(langs: (BundledLanguage | SpecialLanguage)[]) {\n for (const lang of langs) {\n if (!highlighter) break\n await highlighter.loadLanguage(lang)\n loadedLangs.add(lang)\n }\n}\n\nasync function loadThemes(themes: BundledTheme[]) {\n for (const theme of themes) {\n if (!highlighter) break\n await highlighter.loadTheme(theme)\n loadedThemes.add(theme)\n }\n}\n\nexport type HighlighterResult =\n | {\n highlighter: Highlighter\n promise?: undefined\n }\n | {\n highlighter?: undefined\n promise: Promise<void>\n }\n\nexport function createOrGetHighlighter(\n options: HighlighterOptions,\n): HighlighterResult {\n if (!highlighter) {\n return { promise: ensureHighlighter(options) }\n }\n\n const langs = options.langs.filter((lang) => !loadedLangs.has(lang))\n if (langs.length > 0) {\n return { promise: loadLanguages(langs) }\n }\n\n const themes = options.themes.filter((theme) => !loadedThemes.has(theme))\n if (themes.length > 0) {\n return { promise: loadThemes(themes) }\n }\n\n return { highlighter }\n}\n"],"mappings":";;AAIA,IAAI;AACJ,IAAI;AACJ,MAAM,8BAAc,IAAI,KAAwC;AAChE,MAAM,+BAAe,IAAI,KAAmB;AAS5C,SAAS,kBAAkB,EACzB,GAAG,WACiC;AACpC,KAAI,CAAC,oBAAoB;AAGvB,MAAI,CAAC,QAAQ,OAEX,SAAQ,SADO,4BAA4B,EAAE,WAAW,MAAM,CAAC;AAGjE,uBAAqB,kBAAkB,QAAQ,CAAC,MAAM,uBAAuB;AAC3E,iBAAc;IACd;;AAEJ,QAAO;;AAGT,eAAe,cAAc,OAA8C;AACzE,MAAK,MAAM,QAAQ,OAAO;AACxB,MAAI,CAAC,YAAa;AAClB,QAAM,YAAY,aAAa,KAAK;AACpC,cAAY,IAAI,KAAK;;;AAIzB,eAAe,WAAW,QAAwB;AAChD,MAAK,MAAM,SAAS,QAAQ;AAC1B,MAAI,CAAC,YAAa;AAClB,QAAM,YAAY,UAAU,MAAM;AAClC,eAAa,IAAI,MAAM;;;AAc3B,SAAgB,uBACd,SACmB;AACnB,KAAI,CAAC,YACH,QAAO,EAAE,SAAS,kBAAkB,QAAQ,EAAE;CAGhD,MAAM,QAAQ,QAAQ,MAAM,QAAQ,SAAS,CAAC,YAAY,IAAI,KAAK,CAAC;AACpE,KAAI,MAAM,SAAS,EACjB,QAAO,EAAE,SAAS,cAAc,MAAM,EAAE;CAG1C,MAAM,SAAS,QAAQ,OAAO,QAAQ,UAAU,CAAC,aAAa,IAAI,MAAM,CAAC;AACzE,KAAI,OAAO,SAAS,EAClB,QAAO,EAAE,SAAS,WAAW,OAAO,EAAE;AAGxC,QAAO,EAAE,aAAa"}
1
+ {"version":3,"file":"shiki-highlighter-chunk.js","names":[],"sources":["../src/code-block/shiki-highlighter-chunk.ts"],"sourcesContent":["import type { BundledHighlighterOptions, BundledLanguage, BundledTheme, Highlighter, SpecialLanguage } from 'shiki'\nimport { createHighlighter } from 'shiki/bundle/full'\nimport { createJavaScriptRegexEngine } from 'shiki/engine/javascript'\n\nlet highlighterPromise: Promise<void> | undefined\nlet highlighter: Highlighter | undefined\nconst loadedLangs = new Set<BundledLanguage | SpecialLanguage>()\nconst loadedThemes = new Set<BundledTheme>()\n\nexport interface ShikiHighlighterOptions extends BundledHighlighterOptions<BundledLanguage, BundledTheme> {}\n\nexport interface HighlighterOptions extends Omit<ShikiHighlighterOptions, 'langs' | 'themes'> {\n themes: BundledTheme[]\n langs: (BundledLanguage | SpecialLanguage)[]\n}\n\nfunction ensureHighlighter({\n ...options\n}: HighlighterOptions): Promise<void> {\n if (!highlighterPromise) {\n // If no engine is provided, use the JavaScript engine, which is\n // smaller than the WASM engine.\n if (!options.engine) {\n const engine = createJavaScriptRegexEngine({ forgiving: true })\n options.engine = engine\n }\n highlighterPromise = createHighlighter(options).then((createdHighlighter) => {\n highlighter = createdHighlighter\n })\n }\n return highlighterPromise\n}\n\nasync function loadLanguages(langs: (BundledLanguage | SpecialLanguage)[]) {\n for (const lang of langs) {\n if (!highlighter) break\n await highlighter.loadLanguage(lang)\n loadedLangs.add(lang)\n }\n}\n\nasync function loadThemes(themes: BundledTheme[]) {\n for (const theme of themes) {\n if (!highlighter) break\n await highlighter.loadTheme(theme)\n loadedThemes.add(theme)\n }\n}\n\nexport type HighlighterResult =\n | {\n highlighter: Highlighter\n promise?: undefined\n }\n | {\n highlighter?: undefined\n promise: Promise<void>\n }\n\nexport function createOrGetHighlighter(\n options: HighlighterOptions,\n): HighlighterResult {\n if (!highlighter) {\n return { promise: ensureHighlighter(options) }\n }\n\n const langs = options.langs.filter((lang) => !loadedLangs.has(lang))\n if (langs.length > 0) {\n return { promise: loadLanguages(langs) }\n }\n\n const themes = options.themes.filter((theme) => !loadedThemes.has(theme))\n if (themes.length > 0) {\n return { promise: loadThemes(themes) }\n }\n\n return { highlighter }\n}\n"],"mappings":";;AAIA,IAAI;AACJ,IAAI;AACJ,MAAM,8BAAc,IAAI,KAAwC;AAChE,MAAM,+BAAe,IAAI,KAAmB;AAS5C,SAAS,kBAAkB,EACzB,GAAG,WACiC;AACpC,KAAI,CAAC,oBAAoB;AAGvB,MAAI,CAAC,QAAQ,OAEX,SAAQ,SADO,4BAA4B,EAAE,WAAW,MAAM,CACvC;AAEzB,uBAAqB,kBAAkB,QAAQ,CAAC,MAAM,uBAAuB;AAC3E,iBAAc;IACd;;AAEJ,QAAO;;AAGT,eAAe,cAAc,OAA8C;AACzE,MAAK,MAAM,QAAQ,OAAO;AACxB,MAAI,CAAC,YAAa;AAClB,QAAM,YAAY,aAAa,KAAK;AACpC,cAAY,IAAI,KAAK;;;AAIzB,eAAe,WAAW,QAAwB;AAChD,MAAK,MAAM,SAAS,QAAQ;AAC1B,MAAI,CAAC,YAAa;AAClB,QAAM,YAAY,UAAU,MAAM;AAClC,eAAa,IAAI,MAAM;;;AAc3B,SAAgB,uBACd,SACmB;AACnB,KAAI,CAAC,YACH,QAAO,EAAE,SAAS,kBAAkB,QAAQ,EAAE;CAGhD,MAAM,QAAQ,QAAQ,MAAM,QAAQ,SAAS,CAAC,YAAY,IAAI,KAAK,CAAC;AACpE,KAAI,MAAM,SAAS,EACjB,QAAO,EAAE,SAAS,cAAc,MAAM,EAAE;CAG1C,MAAM,SAAS,QAAQ,OAAO,QAAQ,UAAU,CAAC,aAAa,IAAI,MAAM,CAAC;AACzE,KAAI,OAAO,SAAS,EAClB,QAAO,EAAE,SAAS,WAAW,OAAO,EAAE;AAGxC,QAAO,EAAE,aAAa"}
@@ -1 +1 @@
1
- {"version":3,"file":"table2.js","names":[],"sources":["../src/table/table-commands/exit-table.ts","../src/table/table-commands/insert-table.ts","../src/table/table-utils.ts","../src/table/table-commands/select-table-cell.ts","../src/table/table-commands/select-table-column.ts","../src/table/table-commands/select-table-row.ts","../src/table/table-commands/select-table.ts","../src/table/table-commands.ts","../src/table/table-drop-indicator.ts","../src/table/table-plugins.ts","../src/table/table-spec.ts","../src/table/table.ts"],"sourcesContent":["import { defaultBlockAt } from '@prosekit/core'\nimport { TextSelection } from '@prosekit/pm/state'\nimport type { Command } from '@prosekit/pm/state'\nimport type { TableRole } from 'prosemirror-tables'\n\n/**\n * When the selection is in a table node, create a default block after the table\n * table, and move the cursor there.\n *\n * @public\n */\nexport const exitTable: Command = (state, dispatch) => {\n const { $head, $anchor } = state.selection\n\n if (!$head.sameParent($anchor)) {\n return false\n }\n\n let tableStart = -1\n let tableDepth = -1\n for (let depth = $head.depth; depth >= 0; depth--) {\n const node = $head.node(depth)\n if ((node.type.spec.tableRole as TableRole) === 'table') {\n tableStart = $head.before(depth)\n tableDepth = depth\n }\n }\n\n if (tableStart < 0 || tableDepth <= 0) {\n return false\n }\n\n const above = $head.node(tableDepth - 1)\n const after = $head.indexAfter(tableDepth - 1)\n const type = defaultBlockAt(above.contentMatchAt(after))\n const node = type?.createAndFill()\n\n if (!type || !node || !above.canReplaceWith(after, after, type)) {\n return false\n }\n\n if (dispatch) {\n const pos = $head.after(tableDepth)\n const tr = state.tr.replaceWith(pos, pos, node)\n tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1))\n dispatch(tr.scrollIntoView())\n }\n return true\n}\n","import { getNodeType, insertNode } from '@prosekit/core'\nimport type { Schema } from '@prosekit/pm/model'\nimport type { Command } from '@prosekit/pm/state'\n\nfunction createEmptyTable(\n schema: Schema,\n row: number,\n col: number,\n header: boolean,\n) {\n const tableType = getNodeType(schema, 'table')\n const tableRowType = getNodeType(schema, 'tableRow')\n const tableCellType = getNodeType(schema, 'tableCell')\n const tableHeaderCellType = getNodeType(schema, 'tableHeaderCell')\n\n if (header) {\n const headerCell = tableHeaderCellType.createAndFill()!\n const headerCells = repeat(headerCell, col)\n const headerRow = tableRowType.createAndFill(null, headerCells)!\n\n const bodyCell = tableCellType.createAndFill()!\n const bodyCells = repeat(bodyCell, col)\n const bodyRow = tableRowType.createAndFill(null, bodyCells)!\n const bodyRows = repeat(bodyRow, row - 1)\n\n return tableType.createAndFill(null, [headerRow, ...bodyRows])!\n } else {\n const bodyCell = tableCellType.createAndFill()!\n const bodyCells = repeat(bodyCell, col)\n const bodyRow = tableRowType.createAndFill(null, bodyCells)!\n const bodyRows = repeat(bodyRow, row)\n\n return tableType.createAndFill(null, bodyRows)!\n }\n}\n\nfunction repeat<T>(node: T, length: number): T[] {\n return Array<T>(length).fill(node)\n}\n\n/**\n * @public\n */\nexport interface InsertTableOptions {\n /**\n * The number of rows in the table.\n */\n row: number\n\n /**\n * The number of columns in the table.\n */\n col: number\n\n /**\n * Whether the table has a header row.\n *\n * @default false\n */\n header?: boolean\n}\n\n/**\n * Insert a table node with the given number of rows and columns, and optionally\n * a header row.\n *\n * @param options\n *\n * @public\n */\nexport function insertTable(options: InsertTableOptions): Command {\n return (state, dispatch, view) => {\n const { row, col, header = false } = options\n const table = createEmptyTable(state.schema, row, col, header)\n return insertNode({ node: table })(state, dispatch, view)\n }\n}\n","import { CellSelection, findCellPos, findCellRange, findTable } from 'prosemirror-tables'\n\nexport { findCellPos, findCellRange, findTable }\n\n/**\n * Checks if the given object is a `CellSelection` instance.\n *\n * @public\n */\nexport function isCellSelection(value: unknown): value is CellSelection {\n return value instanceof CellSelection\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection } from 'prosemirror-tables'\n\nimport { findCellPos } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableCellOptions {\n /**\n * A hit position of the table cell to select from. By default, the selection\n * anchor will be used.\n */\n pos?: number\n}\n\n/**\n * @public\n */\nexport function selectTableCell(options?: SelectTableCellOptions): Command {\n return (state, dispatch) => {\n const $cellPos = findCellPos(\n state.doc,\n options?.pos ?? state.selection.anchor,\n )\n if (!$cellPos) {\n return false\n }\n if (dispatch) {\n const selection = new CellSelection($cellPos)\n dispatch(state.tr.setSelection(selection))\n }\n return true\n }\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection } from 'prosemirror-tables'\n\nimport { findCellRange } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableColumnOptions {\n /**\n * A hit position of the table cell to select from. By default, the selection\n * anchor will be used.\n */\n anchor?: number\n\n /**\n * A hit position of the table cell to select to. By default, the selection\n * head will be used.\n */\n head?: number\n}\n\n/**\n * @public\n */\nexport function selectTableColumn(options?: SelectTableColumnOptions): Command {\n return (state, dispatch) => {\n const range = findCellRange(state.selection, options?.anchor, options?.head)\n if (!range) {\n return false\n }\n if (dispatch) {\n const [$anchorCell, $headCell] = range\n const selection = CellSelection.colSelection($anchorCell, $headCell)\n dispatch(state.tr.setSelection(selection))\n }\n return true\n }\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection } from 'prosemirror-tables'\n\nimport { findCellRange } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableRowOptions {\n /**\n * A hit position of the table cell to select from. By default, the selection\n * anchor will be used.\n */\n anchor?: number\n\n /**\n * A hit position of the table cell to select to. By default, the selection\n * head will be used.\n */\n head?: number\n}\n\n/**\n * @public\n */\nexport function selectTableRow(options?: SelectTableRowOptions): Command {\n return (state, dispatch) => {\n const range = findCellRange(state.selection, options?.anchor, options?.head)\n if (!range) {\n return false\n }\n if (dispatch) {\n const [$anchorCell, $headCell] = range\n const selection = CellSelection.rowSelection($anchorCell, $headCell)\n dispatch(state.tr.setSelection(selection))\n }\n return true\n }\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection, TableMap } from 'prosemirror-tables'\n\nimport { findTable } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableOptions {\n /**\n * A hit position of the table to select from. By default, the selection\n * anchor will be used.\n */\n pos?: number\n}\n\n/**\n * @public\n */\nexport function selectTable(options?: SelectTableOptions): Command {\n return (state, dispatch) => {\n const $pos = options?.pos\n ? state.doc.resolve(options.pos)\n : state.selection.$anchor\n const table = findTable($pos)\n if (!table) {\n return false\n }\n const map = TableMap.get(table.node)\n if (map.map.length === 0) {\n return false\n }\n if (dispatch) {\n let tr = state.tr\n const firstCellPosInTable = map.map[0]\n const lastCellPosInTable = map.map[map.map.length - 1]\n const firstCellPos = table.pos + firstCellPosInTable + 1\n const lastCellPos = table.pos + lastCellPosInTable + 1\n const $firstCellPos = tr.doc.resolve(firstCellPos)\n const $lastCellPos = tr.doc.resolve(lastCellPos)\n const selection = new CellSelection($firstCellPos, $lastCellPos)\n tr = tr.setSelection(selection)\n dispatch?.(tr)\n }\n return true\n }\n}\n","import { defineCommands, type Extension } from '@prosekit/core'\nimport {\n addColumnAfter,\n addColumnBefore,\n addRowAfter,\n addRowBefore,\n deleteColumn,\n deleteRow,\n deleteTable,\n mergeCells,\n splitCell,\n} from 'prosemirror-tables'\n\nimport { deleteCellSelection } from './table-commands/delete-cell-selection.ts'\nimport { exitTable } from './table-commands/exit-table.ts'\nimport { insertTable, type InsertTableOptions } from './table-commands/insert-table.ts'\nimport { moveTableColumn, type MoveTableColumnOptions } from './table-commands/move-table-column.ts'\nimport { moveTableRow, type MoveTableRowOptions } from './table-commands/move-table-row.ts'\nimport { selectTableCell, type SelectTableCellOptions } from './table-commands/select-table-cell.ts'\nimport { selectTableColumn, type SelectTableColumnOptions } from './table-commands/select-table-column.ts'\nimport { selectTableRow, type SelectTableRowOptions } from './table-commands/select-table-row.ts'\nimport { selectTable, type SelectTableOptions } from './table-commands/select-table.ts'\n\n/**\n * @internal\n */\nexport type TableCommandsExtension = Extension<{\n Commands: {\n insertTable: [options: InsertTableOptions]\n exitTable: []\n\n selectTable: [options?: SelectTableOptions]\n selectTableCell: [options?: SelectTableCellOptions]\n selectTableColumn: [options?: SelectTableColumnOptions]\n selectTableRow: [options?: SelectTableRowOptions]\n\n addTableColumnBefore: []\n addTableColumnAfter: []\n addTableRowAbove: []\n addTableRowBelow: []\n\n deleteTable: []\n deleteTableColumn: []\n deleteTableRow: []\n deleteCellSelection: []\n\n mergeTableCells: []\n splitTableCell: []\n\n moveTableRow: [options: MoveTableRowOptions]\n moveTableColumn: [options: MoveTableColumnOptions]\n }\n}>\n\n/**\n * Adds commands for working with `table` nodes.\n *\n * @public\n */\nexport function defineTableCommands(): TableCommandsExtension {\n return defineCommands({\n insertTable,\n exitTable: () => exitTable,\n\n selectTable,\n selectTableCell,\n selectTableColumn,\n selectTableRow,\n\n addTableColumnBefore: () => addColumnBefore,\n addTableColumnAfter: () => addColumnAfter,\n addTableRowAbove: () => addRowBefore,\n addTableRowBelow: () => addRowAfter,\n\n deleteTable: () => deleteTable,\n deleteTableColumn: () => deleteColumn,\n deleteTableRow: () => deleteRow,\n deleteCellSelection: () => deleteCellSelection,\n\n mergeTableCells: () => mergeCells,\n splitTableCell: () => splitCell,\n\n moveTableRow,\n moveTableColumn,\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport type { DragEventHandler } from '../drop-indicator/index.ts'\nimport { defineDropIndicator } from '../drop-indicator/index.ts'\n\n/**\n * Hides the drop indicator when dragging a table column or row by using the\n * table handle.\n *\n * @internal\n */\nexport function defineTableDropIndicator(): PlainExtension {\n return defineDropIndicator({\n onDrag,\n })\n}\n\nconst matchMap = new WeakMap<DataTransfer, boolean>()\n\nconst onDrag: DragEventHandler = ({ event }): boolean => {\n const dataTransfer = event.dataTransfer\n if (!dataTransfer) return true\n\n let match: boolean\n\n if (matchMap.has(dataTransfer)) {\n match = matchMap.get(dataTransfer)!\n } else {\n // On Safari, accessing `dataTransfer.types` is more than 10x slower than in\n // Chrome. This becomes a bottleneck when `onDrag` is called frequently, so\n // we cache the result in a WeakMap.\n const types = dataTransfer.types\n match = types.includes('application/x-prosekit-table-handle-drag')\n matchMap.set(dataTransfer, match)\n }\n\n // Don't show the drop indicator when the drag event has\n // \"application/x-prosekit-table-handle-drag\" type.\n return !match\n}\n","import { definePlugin, type PlainExtension } from '@prosekit/core'\nimport { columnResizing, tableEditing } from 'prosemirror-tables'\n\n/**\n * @public\n */\nexport function defineTablePlugins(): PlainExtension {\n return definePlugin([tableEditing(), columnResizing()])\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { AttributeSpec, Attrs } from '@prosekit/pm/model'\nimport { tableNodes } from 'prosemirror-tables'\n\nconst cellContent = 'block+'\n\n/**\n * @public\n */\nexport interface CellAttrs {\n colspan?: number\n rowspan?: number\n colwidth?: number[] | null\n}\n\nconst cellAttrs = {\n colspan: { default: 1 },\n rowspan: { default: 1 },\n colwidth: { default: null },\n} satisfies Record<string, AttributeSpec>\n\n/**\n * @internal\n */\nexport type TableSpecExtension = Extension<{\n Nodes: {\n table: Attrs\n }\n}>\n\nconst specs = tableNodes({\n tableGroup: 'block',\n cellContent,\n cellAttributes: {},\n})\n\n/**\n * @internal\n */\nexport function defineTableSpec(): TableSpecExtension {\n return defineNodeSpec({\n ...specs['table'],\n content: 'tableRow+',\n name: 'table',\n })\n}\n\n/**\n * @internal\n */\nexport type TableRowSpecExtension = Extension<{\n Nodes: {\n tableRow: Attrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineTableRowSpec(): TableRowSpecExtension {\n return defineNodeSpec({\n ...specs['table_row'],\n content: '(tableCell | tableHeaderCell)*',\n name: 'tableRow',\n })\n}\n\n/**\n * @internal\n */\nexport type TableCellSpecExtension = Extension<{\n Nodes: {\n tableCell: CellAttrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineTableCellSpec(): TableCellSpecExtension {\n return defineNodeSpec({\n ...specs['table_cell'],\n name: 'tableCell',\n attrs: cellAttrs,\n })\n}\n\n/**\n * @internal\n */\nexport type TableHeaderCellSpecExtension = Extension<{\n Nodes: {\n tableHeaderCell: CellAttrs\n }\n}>\n\nexport function defineTableHeaderCellSpec(): TableHeaderCellSpecExtension {\n return defineNodeSpec({\n ...specs['table_header'],\n name: 'tableHeaderCell',\n attrs: cellAttrs,\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineTableCommands, type TableCommandsExtension } from './table-commands.ts'\nimport { defineTableDropIndicator } from './table-drop-indicator.ts'\nimport { defineTablePlugins } from './table-plugins.ts'\nimport {\n defineTableCellSpec,\n defineTableHeaderCellSpec,\n defineTableRowSpec,\n defineTableSpec,\n type TableCellSpecExtension,\n type TableHeaderCellSpecExtension,\n type TableRowSpecExtension,\n type TableSpecExtension,\n} from './table-spec.ts'\n\n/**\n * @internal\n */\nexport type TableExtension = Union<\n [\n TableSpecExtension,\n TableRowSpecExtension,\n TableCellSpecExtension,\n TableHeaderCellSpecExtension,\n TableCommandsExtension,\n ]\n>\n\n/**\n * @public\n */\nexport function defineTable(): TableExtension {\n return union(\n defineTableSpec(),\n defineTableRowSpec(),\n defineTableCellSpec(),\n defineTableHeaderCellSpec(),\n defineTablePlugins(),\n defineTableCommands(),\n defineTableDropIndicator(),\n )\n}\n"],"mappings":";;;;;;;;;;AAWA,MAAa,aAAsB,OAAO,aAAa;CACrD,MAAM,EAAE,OAAO,YAAY,MAAM;AAEjC,KAAI,CAAC,MAAM,WAAW,QAAQ,CAC5B,QAAO;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5B,SAAQ,SAAS,IAAI,aAAa;MAC7B;AAKL,UADc,aAAa,MACb,SAAS,2CAA2C;AAClE,WAAS,IAAI,cAAc,MAAM;;AAKnC,QAAO,CAAC;;;;;AChCV,SAAgB,qBAAqC;AACnD,QAAO,aAAa,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;;ACHzD,MAAM,cAAc;AAWpB,MAAM,YAAY;CAChB,SAAS,EAAE,SAAS,GAAG;CACvB,SAAS,EAAE,SAAS,GAAG;CACvB,UAAU,EAAE,SAAS,MAAM;CAC5B;AAWD,MAAM,QAAQ,WAAW;CACvB,YAAY;CACZ;CACA,gBAAgB,EAAE;CACnB,CAAC;;;;AAKF,SAAgB,kBAAsC;AACpD,QAAO,eAAe;EACpB,GAAG,MAAM;EACT,SAAS;EACT,MAAM;EACP,CAAC;;;;;AAeJ,SAAgB,qBAA4C;AAC1D,QAAO,eAAe;EACpB,GAAG,MAAM;EACT,SAAS;EACT,MAAM;EACP,CAAC;;;;;AAeJ,SAAgB,sBAA8C;AAC5D,QAAO,eAAe;EACpB,GAAG,MAAM;EACT,MAAM;EACN,OAAO;EACR,CAAC;;AAYJ,SAAgB,4BAA0D;AACxE,QAAO,eAAe;EACpB,GAAG,MAAM;EACT,MAAM;EACN,OAAO;EACR,CAAC;;;;;ACrEJ,SAAgB,cAA8B;AAC5C,QAAO,MACL,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,2BAA2B,EAC3B,oBAAoB,EACpB,qBAAqB,EACrB,0BAA0B,CAC3B"}
1
+ {"version":3,"file":"table2.js","names":[],"sources":["../src/table/table-commands/exit-table.ts","../src/table/table-commands/insert-table.ts","../src/table/table-utils.ts","../src/table/table-commands/select-table-cell.ts","../src/table/table-commands/select-table-column.ts","../src/table/table-commands/select-table-row.ts","../src/table/table-commands/select-table.ts","../src/table/table-commands.ts","../src/table/table-drop-indicator.ts","../src/table/table-plugins.ts","../src/table/table-spec.ts","../src/table/table.ts"],"sourcesContent":["import { defaultBlockAt } from '@prosekit/core'\nimport { TextSelection } from '@prosekit/pm/state'\nimport type { Command } from '@prosekit/pm/state'\nimport type { TableRole } from 'prosemirror-tables'\n\n/**\n * When the selection is in a table node, create a default block after the table\n * table, and move the cursor there.\n *\n * @public\n */\nexport const exitTable: Command = (state, dispatch) => {\n const { $head, $anchor } = state.selection\n\n if (!$head.sameParent($anchor)) {\n return false\n }\n\n let tableStart = -1\n let tableDepth = -1\n for (let depth = $head.depth; depth >= 0; depth--) {\n const node = $head.node(depth)\n if ((node.type.spec.tableRole as TableRole) === 'table') {\n tableStart = $head.before(depth)\n tableDepth = depth\n }\n }\n\n if (tableStart < 0 || tableDepth <= 0) {\n return false\n }\n\n const above = $head.node(tableDepth - 1)\n const after = $head.indexAfter(tableDepth - 1)\n const type = defaultBlockAt(above.contentMatchAt(after))\n const node = type?.createAndFill()\n\n if (!type || !node || !above.canReplaceWith(after, after, type)) {\n return false\n }\n\n if (dispatch) {\n const pos = $head.after(tableDepth)\n const tr = state.tr.replaceWith(pos, pos, node)\n tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1))\n dispatch(tr.scrollIntoView())\n }\n return true\n}\n","import { getNodeType, insertNode } from '@prosekit/core'\nimport type { Schema } from '@prosekit/pm/model'\nimport type { Command } from '@prosekit/pm/state'\n\nfunction createEmptyTable(\n schema: Schema,\n row: number,\n col: number,\n header: boolean,\n) {\n const tableType = getNodeType(schema, 'table')\n const tableRowType = getNodeType(schema, 'tableRow')\n const tableCellType = getNodeType(schema, 'tableCell')\n const tableHeaderCellType = getNodeType(schema, 'tableHeaderCell')\n\n if (header) {\n const headerCell = tableHeaderCellType.createAndFill()!\n const headerCells = repeat(headerCell, col)\n const headerRow = tableRowType.createAndFill(null, headerCells)!\n\n const bodyCell = tableCellType.createAndFill()!\n const bodyCells = repeat(bodyCell, col)\n const bodyRow = tableRowType.createAndFill(null, bodyCells)!\n const bodyRows = repeat(bodyRow, row - 1)\n\n return tableType.createAndFill(null, [headerRow, ...bodyRows])!\n } else {\n const bodyCell = tableCellType.createAndFill()!\n const bodyCells = repeat(bodyCell, col)\n const bodyRow = tableRowType.createAndFill(null, bodyCells)!\n const bodyRows = repeat(bodyRow, row)\n\n return tableType.createAndFill(null, bodyRows)!\n }\n}\n\nfunction repeat<T>(node: T, length: number): T[] {\n return Array<T>(length).fill(node)\n}\n\n/**\n * @public\n */\nexport interface InsertTableOptions {\n /**\n * The number of rows in the table.\n */\n row: number\n\n /**\n * The number of columns in the table.\n */\n col: number\n\n /**\n * Whether the table has a header row.\n *\n * @default false\n */\n header?: boolean\n}\n\n/**\n * Insert a table node with the given number of rows and columns, and optionally\n * a header row.\n *\n * @param options\n *\n * @public\n */\nexport function insertTable(options: InsertTableOptions): Command {\n return (state, dispatch, view) => {\n const { row, col, header = false } = options\n const table = createEmptyTable(state.schema, row, col, header)\n return insertNode({ node: table })(state, dispatch, view)\n }\n}\n","import { CellSelection, findCellPos, findCellRange, findTable } from 'prosemirror-tables'\n\nexport { findCellPos, findCellRange, findTable }\n\n/**\n * Checks if the given object is a `CellSelection` instance.\n *\n * @public\n */\nexport function isCellSelection(value: unknown): value is CellSelection {\n return value instanceof CellSelection\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection } from 'prosemirror-tables'\n\nimport { findCellPos } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableCellOptions {\n /**\n * A hit position of the table cell to select from. By default, the selection\n * anchor will be used.\n */\n pos?: number\n}\n\n/**\n * @public\n */\nexport function selectTableCell(options?: SelectTableCellOptions): Command {\n return (state, dispatch) => {\n const $cellPos = findCellPos(\n state.doc,\n options?.pos ?? state.selection.anchor,\n )\n if (!$cellPos) {\n return false\n }\n if (dispatch) {\n const selection = new CellSelection($cellPos)\n dispatch(state.tr.setSelection(selection))\n }\n return true\n }\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection } from 'prosemirror-tables'\n\nimport { findCellRange } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableColumnOptions {\n /**\n * A hit position of the table cell to select from. By default, the selection\n * anchor will be used.\n */\n anchor?: number\n\n /**\n * A hit position of the table cell to select to. By default, the selection\n * head will be used.\n */\n head?: number\n}\n\n/**\n * @public\n */\nexport function selectTableColumn(options?: SelectTableColumnOptions): Command {\n return (state, dispatch) => {\n const range = findCellRange(state.selection, options?.anchor, options?.head)\n if (!range) {\n return false\n }\n if (dispatch) {\n const [$anchorCell, $headCell] = range\n const selection = CellSelection.colSelection($anchorCell, $headCell)\n dispatch(state.tr.setSelection(selection))\n }\n return true\n }\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection } from 'prosemirror-tables'\n\nimport { findCellRange } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableRowOptions {\n /**\n * A hit position of the table cell to select from. By default, the selection\n * anchor will be used.\n */\n anchor?: number\n\n /**\n * A hit position of the table cell to select to. By default, the selection\n * head will be used.\n */\n head?: number\n}\n\n/**\n * @public\n */\nexport function selectTableRow(options?: SelectTableRowOptions): Command {\n return (state, dispatch) => {\n const range = findCellRange(state.selection, options?.anchor, options?.head)\n if (!range) {\n return false\n }\n if (dispatch) {\n const [$anchorCell, $headCell] = range\n const selection = CellSelection.rowSelection($anchorCell, $headCell)\n dispatch(state.tr.setSelection(selection))\n }\n return true\n }\n}\n","import type { Command } from '@prosekit/pm/state'\nimport { CellSelection, TableMap } from 'prosemirror-tables'\n\nimport { findTable } from '../table-utils.ts'\n\n/**\n * @public\n */\nexport interface SelectTableOptions {\n /**\n * A hit position of the table to select from. By default, the selection\n * anchor will be used.\n */\n pos?: number\n}\n\n/**\n * @public\n */\nexport function selectTable(options?: SelectTableOptions): Command {\n return (state, dispatch) => {\n const $pos = options?.pos\n ? state.doc.resolve(options.pos)\n : state.selection.$anchor\n const table = findTable($pos)\n if (!table) {\n return false\n }\n const map = TableMap.get(table.node)\n if (map.map.length === 0) {\n return false\n }\n if (dispatch) {\n let tr = state.tr\n const firstCellPosInTable = map.map[0]\n const lastCellPosInTable = map.map[map.map.length - 1]\n const firstCellPos = table.pos + firstCellPosInTable + 1\n const lastCellPos = table.pos + lastCellPosInTable + 1\n const $firstCellPos = tr.doc.resolve(firstCellPos)\n const $lastCellPos = tr.doc.resolve(lastCellPos)\n const selection = new CellSelection($firstCellPos, $lastCellPos)\n tr = tr.setSelection(selection)\n dispatch?.(tr)\n }\n return true\n }\n}\n","import { defineCommands, type Extension } from '@prosekit/core'\nimport {\n addColumnAfter,\n addColumnBefore,\n addRowAfter,\n addRowBefore,\n deleteColumn,\n deleteRow,\n deleteTable,\n mergeCells,\n splitCell,\n} from 'prosemirror-tables'\n\nimport { deleteCellSelection } from './table-commands/delete-cell-selection.ts'\nimport { exitTable } from './table-commands/exit-table.ts'\nimport { insertTable, type InsertTableOptions } from './table-commands/insert-table.ts'\nimport { moveTableColumn, type MoveTableColumnOptions } from './table-commands/move-table-column.ts'\nimport { moveTableRow, type MoveTableRowOptions } from './table-commands/move-table-row.ts'\nimport { selectTableCell, type SelectTableCellOptions } from './table-commands/select-table-cell.ts'\nimport { selectTableColumn, type SelectTableColumnOptions } from './table-commands/select-table-column.ts'\nimport { selectTableRow, type SelectTableRowOptions } from './table-commands/select-table-row.ts'\nimport { selectTable, type SelectTableOptions } from './table-commands/select-table.ts'\n\n/**\n * @internal\n */\nexport type TableCommandsExtension = Extension<{\n Commands: {\n insertTable: [options: InsertTableOptions]\n exitTable: []\n\n selectTable: [options?: SelectTableOptions]\n selectTableCell: [options?: SelectTableCellOptions]\n selectTableColumn: [options?: SelectTableColumnOptions]\n selectTableRow: [options?: SelectTableRowOptions]\n\n addTableColumnBefore: []\n addTableColumnAfter: []\n addTableRowAbove: []\n addTableRowBelow: []\n\n deleteTable: []\n deleteTableColumn: []\n deleteTableRow: []\n deleteCellSelection: []\n\n mergeTableCells: []\n splitTableCell: []\n\n moveTableRow: [options: MoveTableRowOptions]\n moveTableColumn: [options: MoveTableColumnOptions]\n }\n}>\n\n/**\n * Adds commands for working with `table` nodes.\n *\n * @public\n */\nexport function defineTableCommands(): TableCommandsExtension {\n return defineCommands({\n insertTable,\n exitTable: () => exitTable,\n\n selectTable,\n selectTableCell,\n selectTableColumn,\n selectTableRow,\n\n addTableColumnBefore: () => addColumnBefore,\n addTableColumnAfter: () => addColumnAfter,\n addTableRowAbove: () => addRowBefore,\n addTableRowBelow: () => addRowAfter,\n\n deleteTable: () => deleteTable,\n deleteTableColumn: () => deleteColumn,\n deleteTableRow: () => deleteRow,\n deleteCellSelection: () => deleteCellSelection,\n\n mergeTableCells: () => mergeCells,\n splitTableCell: () => splitCell,\n\n moveTableRow,\n moveTableColumn,\n })\n}\n","import type { PlainExtension } from '@prosekit/core'\n\nimport type { DragEventHandler } from '../drop-indicator/index.ts'\nimport { defineDropIndicator } from '../drop-indicator/index.ts'\n\n/**\n * Hides the drop indicator when dragging a table column or row by using the\n * table handle.\n *\n * @internal\n */\nexport function defineTableDropIndicator(): PlainExtension {\n return defineDropIndicator({\n onDrag,\n })\n}\n\nconst matchMap = new WeakMap<DataTransfer, boolean>()\n\nconst onDrag: DragEventHandler = ({ event }): boolean => {\n const dataTransfer = event.dataTransfer\n if (!dataTransfer) return true\n\n let match: boolean\n\n if (matchMap.has(dataTransfer)) {\n match = matchMap.get(dataTransfer)!\n } else {\n // On Safari, accessing `dataTransfer.types` is more than 10x slower than in\n // Chrome. This becomes a bottleneck when `onDrag` is called frequently, so\n // we cache the result in a WeakMap.\n const types = dataTransfer.types\n match = types.includes('application/x-prosekit-table-handle-drag')\n matchMap.set(dataTransfer, match)\n }\n\n // Don't show the drop indicator when the drag event has\n // \"application/x-prosekit-table-handle-drag\" type.\n return !match\n}\n","import { definePlugin, type PlainExtension } from '@prosekit/core'\nimport { columnResizing, tableEditing } from 'prosemirror-tables'\n\n/**\n * @public\n */\nexport function defineTablePlugins(): PlainExtension {\n return definePlugin([tableEditing(), columnResizing()])\n}\n","import { defineNodeSpec, type Extension } from '@prosekit/core'\nimport type { AttributeSpec, Attrs } from '@prosekit/pm/model'\nimport { tableNodes } from 'prosemirror-tables'\n\nconst cellContent = 'block+'\n\n/**\n * @public\n */\nexport interface CellAttrs {\n colspan?: number\n rowspan?: number\n colwidth?: number[] | null\n}\n\nconst cellAttrs = {\n colspan: { default: 1 },\n rowspan: { default: 1 },\n colwidth: { default: null },\n} satisfies Record<string, AttributeSpec>\n\n/**\n * @internal\n */\nexport type TableSpecExtension = Extension<{\n Nodes: {\n table: Attrs\n }\n}>\n\nconst specs = tableNodes({\n tableGroup: 'block',\n cellContent,\n cellAttributes: {},\n})\n\n/**\n * @internal\n */\nexport function defineTableSpec(): TableSpecExtension {\n return defineNodeSpec({\n ...specs['table'],\n content: 'tableRow+',\n name: 'table',\n })\n}\n\n/**\n * @internal\n */\nexport type TableRowSpecExtension = Extension<{\n Nodes: {\n tableRow: Attrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineTableRowSpec(): TableRowSpecExtension {\n return defineNodeSpec({\n ...specs['table_row'],\n content: '(tableCell | tableHeaderCell)*',\n name: 'tableRow',\n })\n}\n\n/**\n * @internal\n */\nexport type TableCellSpecExtension = Extension<{\n Nodes: {\n tableCell: CellAttrs\n }\n}>\n\n/**\n * @internal\n */\nexport function defineTableCellSpec(): TableCellSpecExtension {\n return defineNodeSpec({\n ...specs['table_cell'],\n name: 'tableCell',\n attrs: cellAttrs,\n })\n}\n\n/**\n * @internal\n */\nexport type TableHeaderCellSpecExtension = Extension<{\n Nodes: {\n tableHeaderCell: CellAttrs\n }\n}>\n\nexport function defineTableHeaderCellSpec(): TableHeaderCellSpecExtension {\n return defineNodeSpec({\n ...specs['table_header'],\n name: 'tableHeaderCell',\n attrs: cellAttrs,\n })\n}\n","import { union, type Union } from '@prosekit/core'\n\nimport { defineTableCommands, type TableCommandsExtension } from './table-commands.ts'\nimport { defineTableDropIndicator } from './table-drop-indicator.ts'\nimport { defineTablePlugins } from './table-plugins.ts'\nimport {\n defineTableCellSpec,\n defineTableHeaderCellSpec,\n defineTableRowSpec,\n defineTableSpec,\n type TableCellSpecExtension,\n type TableHeaderCellSpecExtension,\n type TableRowSpecExtension,\n type TableSpecExtension,\n} from './table-spec.ts'\n\n/**\n * @internal\n */\nexport type TableExtension = Union<\n [\n TableSpecExtension,\n TableRowSpecExtension,\n TableCellSpecExtension,\n TableHeaderCellSpecExtension,\n TableCommandsExtension,\n ]\n>\n\n/**\n * @public\n */\nexport function defineTable(): TableExtension {\n return union(\n defineTableSpec(),\n defineTableRowSpec(),\n defineTableCellSpec(),\n defineTableHeaderCellSpec(),\n defineTablePlugins(),\n defineTableCommands(),\n defineTableDropIndicator(),\n )\n}\n"],"mappings":";;;;;;;;;;AAWA,MAAa,aAAsB,OAAO,aAAa;CACrD,MAAM,EAAE,OAAO,YAAY,MAAM;AAEjC,KAAI,CAAC,MAAM,WAAW,QAAQ,CAC5B,QAAO;CAGT,IAAI,aAAa;CACjB,IAAI,aAAa;AACjB,MAAK,IAAI,QAAQ,MAAM,OAAO,SAAS,GAAG,QAExC,KADa,MAAM,KAAK,MACf,CAAC,KAAK,KAAK,cAA4B,SAAS;AACvD,eAAa,MAAM,OAAO,MAAM;AAChC,eAAa;;AAIjB,KAAI,aAAa,KAAK,cAAc,EAClC,QAAO;CAGT,MAAM,QAAQ,MAAM,KAAK,aAAa,EAAE;CACxC,MAAM,QAAQ,MAAM,WAAW,aAAa,EAAE;CAC9C,MAAM,OAAO,eAAe,MAAM,eAAe,MAAM,CAAC;CACxD,MAAM,OAAO,MAAM,eAAe;AAElC,KAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,eAAe,OAAO,OAAO,KAAK,CAC7D,QAAO;AAGT,KAAI,UAAU;EACZ,MAAM,MAAM,MAAM,MAAM,WAAW;EACnC,MAAM,KAAK,MAAM,GAAG,YAAY,KAAK,KAAK,KAAK;AAC/C,KAAG,aAAa,cAAc,KAAK,GAAG,IAAI,QAAQ,IAAI,EAAE,EAAE,CAAC;AAC3D,WAAS,GAAG,gBAAgB,CA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AAC;;AAYJ,SAAgB,4BAA0D;AACxE,QAAO,eAAe;EACpB,GAAG,MAAM;EACT,MAAM;EACN,OAAO;EACR,CAAC;;;;;ACrEJ,SAAgB,cAA8B;AAC5C,QAAO,MACL,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,2BAA2B,EAC3B,oBAAoB,EACpB,qBAAqB,EACrB,0BAA0B,CAC3B"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/extensions",
3
3
  "type": "module",
4
- "version": "0.16.0-beta.1",
4
+ "version": "0.16.0",
5
5
  "private": false,
6
6
  "description": "A collection of common extensions for ProseKit",
7
7
  "author": {
@@ -239,8 +239,8 @@
239
239
  "prosemirror-tables": "^1.8.5",
240
240
  "server-dom-shim": "^1.1.0",
241
241
  "shiki": "^3.23.0 || ^4.0.2",
242
- "@prosekit/core": "^0.12.0-beta.1",
243
- "@prosekit/pm": "^0.1.16-beta.0"
242
+ "@prosekit/core": "^0.12.0",
243
+ "@prosekit/pm": "^0.1.16"
244
244
  },
245
245
  "peerDependencies": {
246
246
  "loro-crdt": ">= 1.10.0",
@@ -266,7 +266,7 @@
266
266
  "diffable-html-snapshot": "^0.2.0",
267
267
  "just-pick": "^4.2.0",
268
268
  "katex": "^0.16.45",
269
- "loro-crdt": "^1.11.0",
269
+ "loro-crdt": "^1.11.1",
270
270
  "loro-prosemirror": "^0.4.3",
271
271
  "rehype-parse": "^9.0.1",
272
272
  "rehype-remark": "^10.0.1",
@@ -274,17 +274,17 @@
274
274
  "remark-html": "^16.0.1",
275
275
  "remark-parse": "^11.0.0",
276
276
  "remark-stringify": "^11.0.0",
277
- "tsdown": "^0.21.8",
278
- "type-fest": "^5.5.0",
277
+ "tsdown": "^0.21.10",
278
+ "type-fest": "^5.6.0",
279
279
  "typescript": "~5.9.3",
280
280
  "unified": "^11.0.5",
281
- "vitest": "^4.1.4",
281
+ "vitest": "^4.1.5",
282
282
  "vitest-browser-commands": "^0.2.0",
283
283
  "y-prosemirror": "^1.3.7",
284
284
  "yjs": "^13.6.30",
285
285
  "@prosekit/config-ts": "0.0.0",
286
- "@prosekit/config-vitest": "0.0.0",
287
- "@prosekit/config-tsdown": "0.0.0"
286
+ "@prosekit/config-tsdown": "0.0.0",
287
+ "@prosekit/config-vitest": "0.0.0"
288
288
  },
289
289
  "scripts": {
290
290
  "build:tsc": "tsc -b tsconfig.json",