import type { Node } from '@milkdown/prose/model' import type { MarkdownSource } from '@milkdown/transformer' import { createSlice } from '@milkdown/ctx' import { ctxCallOutOfScope } from '@milkdown/exception' import { Plugin, PluginKey } from '@milkdown/prose/state' import type { ParsedSources } from '../__internal__' /// A slice which turns on source preservation. It is `false` by default. /// /// The serializer normalizes what it writes: `- a` becomes `* a`, a table /// gets padded, a reference link becomes an inline link. With this slice /// set to `true`, the editor remembers the markdown that the document was /// loaded from. `getMarkdown` and the `markdownUpdated` listener then keep /// as much of that markdown as they can: /// /// - A document that was not edited comes back as the markdown it was /// loaded from, byte for byte. /// - In a document that was edited, the serializer writes the top-level /// blocks that changed. Every other block keeps the text it had where /// that is possible, and so do the blank lines between two blocks that /// are both kept. Text that has no node of its own, such as a link /// definition, stays as long as a kept block needs it. /// /// A block is written again when its place in the markdown is not /// known, or when its text would read differently next to a new /// neighbor. When no result reads back as the document, the whole /// document is written in its normalized form. /// /// The markdown that comes out depends on the markdown that the editor /// was loaded from. Editors that share a document through a collaboration /// provider can each have another source, or none. Leave this off there, /// or give every editor the same source with `setMarkdownSource`. /// /// The editor reads this slice once, when it creates the editor state. /// Set it in `editor.config`: /// /// ```typescript /// Editor.make().config((ctx) => { /// ctx.set(preserveSourceCtx, true) /// }) /// ``` export const preserveSourceCtx = createSlice(false, 'preserveSource') /// The markdown sources that an editor state keeps for its document. export interface MarkdownSourceState { /// The markdown that the document was last loaded from. readonly current: MarkdownSource | null /// The markdown that the document was loaded from before that. An undo /// can bring that document back. readonly previous: MarkdownSource | null } /// The transaction meta that replaces the markdown source of a document. /// Set it with `markdownSourceKey` as the key. A `source` of `null` makes /// the editor forget every source. /// /// ```typescript /// const { doc, source } = ctx.get(sourceParserCtx)(markdown) /// tr.setMeta(markdownSourceKey, { source }) /// ``` export interface MarkdownSourceMeta { source: MarkdownSource | null } /// The key of the prosemirror plugin that keeps the markdown sources. /// The plugin exists only when `preserveSourceCtx` is `true`. /// /// ```typescript /// const sources = markdownSourceKey.getState(view.state) /// ``` export const markdownSourceKey = new PluginKey( 'MILKDOWN_MARKDOWN_SOURCE' ) /// What the source parser gives for a markdown text. export interface ParsedMarkdown { /// The document, the same one that the parser in `parserCtx` gives. doc: Node /// The markdown source of the document. It is `null` when source /// preservation is off, and for an input that is not a string. source: MarkdownSource | null } /// The source parser type. It parses markdown the way the parser in /// `parserCtx` does, and it also tells the markdown source of the result. export type SourceParser = (markdown: string) => ParsedMarkdown const outOfScope = (() => { throw ctxCallOutOfScope() }) as SourceParser /// A slice which contains the source parser. The editor loads markdown /// with it. Use it instead of the parser in `parserCtx` to load markdown /// in a way of your own, when the editor should keep that markdown. The /// parser in `parserCtx` keeps nothing: it also parses text that never /// becomes the document, such as a paste or an inserted snippet. /// /// The sources belong to one editor. Another editor on the same page has /// a source parser and sources of its own. /// /// ```typescript /// const { doc, source } = ctx.get(sourceParserCtx)(markdown) /// view.dispatch( /// view.state.tr /// .replace(0, view.state.doc.content.size, new Slice(doc.content, 0, 0)) /// .setMeta(markdownSourceKey, { source }) /// ) /// ``` export const sourceParserCtx = createSlice( outOfScope, 'sourceParser' ) const none: MarkdownSourceState = { current: null, previous: null } /// @internal /// The plugin never changes the document. `sources` has the markdown of /// the documents of this editor. export function createMarkdownSourcePlugin(sources: ParsedSources) { return new Plugin({ key: markdownSourceKey, state: { init: (_, instance) => { const current = sources.get(instance.doc) return current ? { current, previous: null } : none }, apply: (tr, value) => { const meta = tr.getMeta(markdownSourceKey) as | MarkdownSourceMeta | undefined if (!meta) return value // A new object every time, so the view below sees each request // to forget, also when there was no current source. if (!meta.source) return { current: null, previous: null } return { current: meta.source, // An undo can only bring back a document that a change replaced. // A source that is set for the document as it is replaces none. previous: tr.docChanged ? value.current : value.previous, } }, }, view: () => ({ update: (view, prevState) => { const { state } = view const now = markdownSourceKey.getState(state) // The plugin state only changes when a transaction names a source. if (!now || now === markdownSourceKey.getState(prevState)) return // Without a source the editor forgets all of them: the ones of // documents that an undo could bring back, and the ones of // documents that were parsed but never shown. if (!now.current) sources.clear() // A source can be set for a document that stays as it is, with // `setMarkdownSource`. The nodes of the document then stand for // that source, and no longer for the markdown they were parsed // from. else if (state.doc === prevState.doc) sources.attach(state.doc, now.current) }, }), }) }