@gravity-ui/markdown-editor 15.35.1 → 15.36.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.
- package/build/cjs/core/Editor.js +2 -0
- package/build/cjs/core/Editor.js.map +1 -1
- package/build/cjs/core/index.d.ts +1 -0
- package/build/cjs/core/index.js +3 -1
- package/build/cjs/core/index.js.map +1 -1
- package/build/cjs/core/markdown/MarkdownParser.d.ts +1 -0
- package/build/cjs/core/markdown/MarkdownParser.js +3 -0
- package/build/cjs/core/markdown/MarkdownParser.js.map +1 -1
- package/build/cjs/core/types/parser.d.ts +1 -0
- package/build/cjs/core/types/parser.js.map +1 -1
- package/build/cjs/core/utils/parser.d.ts +3 -0
- package/build/cjs/core/utils/parser.js +12 -0
- package/build/cjs/core/utils/parser.js.map +1 -0
- package/build/cjs/extensions/behavior/SelectionContext/tooltip.js +3 -0
- package/build/cjs/extensions/behavior/SelectionContext/tooltip.js.map +1 -1
- package/build/cjs/extensions/markdown/Bold/index.js +1 -1
- package/build/cjs/extensions/markdown/Bold/index.js.map +1 -1
- package/build/cjs/extensions/markdown/Code/index.js +1 -1
- package/build/cjs/extensions/markdown/Code/index.js.map +1 -1
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js +75 -66
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js.map +1 -1
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/plugins/codeBlockLineNumbersPlugin.js +42 -22
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/plugins/codeBlockLineNumbersPlugin.js.map +1 -1
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/utils.js +20 -0
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/utils.js.map +1 -1
- package/build/cjs/extensions/markdown/Italic/index.js +1 -1
- package/build/cjs/extensions/markdown/Italic/index.js.map +1 -1
- package/build/cjs/extensions/markdown/Mark/index.js +1 -1
- package/build/cjs/extensions/markdown/Mark/index.js.map +1 -1
- package/build/cjs/extensions/markdown/Strike/index.js +1 -1
- package/build/cjs/extensions/markdown/Strike/index.js.map +1 -1
- package/build/cjs/extensions/markdown/Underline/index.js +1 -1
- package/build/cjs/extensions/markdown/Underline/index.js.map +1 -1
- package/build/cjs/extensions/yfm/Monospace/index.js +1 -1
- package/build/cjs/extensions/yfm/Monospace/index.js.map +1 -1
- package/build/cjs/utils/actions.d.ts +6 -0
- package/build/cjs/utils/actions.js +24 -0
- package/build/cjs/utils/actions.js.map +1 -1
- package/build/cjs/utils/marks.d.ts +6 -0
- package/build/cjs/utils/marks.js +31 -0
- package/build/cjs/utils/marks.js.map +1 -1
- package/build/cjs/utils/transaction.d.ts +1 -0
- package/build/cjs/utils/transaction.js +40 -0
- package/build/cjs/utils/transaction.js.map +1 -0
- package/build/cjs/version.js +1 -1
- package/build/cjs/version.js.map +1 -1
- package/build/esm/core/Editor.js +2 -0
- package/build/esm/core/Editor.js.map +1 -1
- package/build/esm/core/index.d.ts +1 -0
- package/build/esm/core/index.js +1 -0
- package/build/esm/core/index.js.map +1 -1
- package/build/esm/core/markdown/MarkdownParser.d.ts +1 -0
- package/build/esm/core/markdown/MarkdownParser.js +3 -0
- package/build/esm/core/markdown/MarkdownParser.js.map +1 -1
- package/build/esm/core/types/parser.d.ts +1 -0
- package/build/esm/core/types/parser.js.map +1 -1
- package/build/esm/core/utils/parser.d.ts +3 -0
- package/build/esm/core/utils/parser.js +8 -0
- package/build/esm/core/utils/parser.js.map +1 -0
- package/build/esm/extensions/behavior/SelectionContext/tooltip.js +3 -0
- package/build/esm/extensions/behavior/SelectionContext/tooltip.js.map +1 -1
- package/build/esm/extensions/markdown/Bold/index.js +2 -2
- package/build/esm/extensions/markdown/Bold/index.js.map +1 -1
- package/build/esm/extensions/markdown/Code/index.js +2 -2
- package/build/esm/extensions/markdown/Code/index.js.map +1 -1
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js +75 -66
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js.map +1 -1
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/plugins/codeBlockLineNumbersPlugin.js +43 -23
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/plugins/codeBlockLineNumbersPlugin.js.map +1 -1
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/utils.js +20 -1
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/utils.js.map +1 -1
- package/build/esm/extensions/markdown/Italic/index.js +2 -2
- package/build/esm/extensions/markdown/Italic/index.js.map +1 -1
- package/build/esm/extensions/markdown/Mark/index.js +2 -2
- package/build/esm/extensions/markdown/Mark/index.js.map +1 -1
- package/build/esm/extensions/markdown/Strike/index.js +2 -2
- package/build/esm/extensions/markdown/Strike/index.js.map +1 -1
- package/build/esm/extensions/markdown/Underline/index.js +2 -2
- package/build/esm/extensions/markdown/Underline/index.js.map +1 -1
- package/build/esm/extensions/yfm/Monospace/index.js +2 -2
- package/build/esm/extensions/yfm/Monospace/index.js.map +1 -1
- package/build/esm/utils/actions.d.ts +6 -0
- package/build/esm/utils/actions.js +24 -1
- package/build/esm/utils/actions.js.map +1 -1
- package/build/esm/utils/marks.d.ts +6 -0
- package/build/esm/utils/marks.js +30 -0
- package/build/esm/utils/marks.js.map +1 -1
- package/build/esm/utils/transaction.d.ts +1 -0
- package/build/esm/utils/transaction.js +36 -0
- package/build/esm/utils/transaction.js.map +1 -0
- package/build/esm/version.js +1 -1
- package/build/esm/version.js.map +1 -1
- package/package.json +1 -1
|
@@ -13,8 +13,9 @@ const TooltipPlugin_1 = require("./TooltipPlugin/index.js");
|
|
|
13
13
|
const const_1 = require("./const.js");
|
|
14
14
|
const codeBlockLineNumbersPlugin_1 = require("./plugins/codeBlockLineNumbersPlugin.js");
|
|
15
15
|
const codeBlockLineWrappingPlugin_1 = require("./plugins/codeBlockLineWrappingPlugin.js");
|
|
16
|
+
const utils_1 = require("./utils.js");
|
|
16
17
|
require("./CodeBlockHighlight.css");
|
|
17
|
-
const
|
|
18
|
+
const pluginKey = new prosemirror_state_1.PluginKey('code_block_highlight');
|
|
18
19
|
const CodeBlockHighlight = (builder, opts) => {
|
|
19
20
|
let langs;
|
|
20
21
|
let lowlight;
|
|
@@ -48,7 +49,7 @@ const CodeBlockHighlight = (builder, opts) => {
|
|
|
48
49
|
// TODO: add TAB key handler
|
|
49
50
|
// TODO: Remove constant selection of block
|
|
50
51
|
return new prosemirror_state_1.Plugin({
|
|
51
|
-
key,
|
|
52
|
+
key: pluginKey,
|
|
52
53
|
state: {
|
|
53
54
|
init: (_, state) => {
|
|
54
55
|
loadModules().then((loaded) => {
|
|
@@ -68,47 +69,47 @@ const CodeBlockHighlight = (builder, opts) => {
|
|
|
68
69
|
}
|
|
69
70
|
selectItems.sort(sortLangs);
|
|
70
71
|
if (view && !view.isDestroyed) {
|
|
71
|
-
view.dispatch(view.state.tr.setMeta(
|
|
72
|
+
view.dispatch(view.state.tr.setMeta(pluginKey, { modulesLoaded }));
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
});
|
|
75
|
-
|
|
76
|
+
const cache = new WeakMap();
|
|
77
|
+
return {
|
|
78
|
+
cache,
|
|
79
|
+
decoSet: modulesLoaded
|
|
80
|
+
? prosemirror_view_1.DecorationSet.empty
|
|
81
|
+
: getDecorations(state.doc, cache),
|
|
82
|
+
};
|
|
76
83
|
},
|
|
77
|
-
apply: (tr,
|
|
84
|
+
apply: (tr, { cache, decoSet }) => {
|
|
78
85
|
if (!modulesLoaded) {
|
|
79
|
-
return prosemirror_view_1.DecorationSet.empty;
|
|
86
|
+
return { cache, decoSet: prosemirror_view_1.DecorationSet.empty };
|
|
80
87
|
}
|
|
81
|
-
if (tr.getMeta(
|
|
82
|
-
return getDecorations(tr.doc);
|
|
88
|
+
if (tr.getMeta(pluginKey)?.modulesLoaded) {
|
|
89
|
+
return { cache, decoSet: getDecorations(tr.doc, cache) };
|
|
83
90
|
}
|
|
84
|
-
if (tr.docChanged)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
// selection includes codeblock node,
|
|
91
|
-
return getDecorations(tr.doc);
|
|
91
|
+
if (!tr.docChanged)
|
|
92
|
+
return { cache, decoSet };
|
|
93
|
+
decoSet = (0, utils_1.processChangedCodeBlocks)(tr, decoSet, (node, pos, decoSet) => {
|
|
94
|
+
const lang = node.attrs[CodeBlockSpecs_1.CodeBlockNodeAttr.Lang];
|
|
95
|
+
if (!lang || !lowlight.registered(lang)) {
|
|
96
|
+
return decoSet.remove(decoSet.find(pos, pos + node.nodeSize));
|
|
92
97
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
newNodes.length !== oldNodes.length ||
|
|
99
|
-
// OR transaction has changes that completely encapsulate a node
|
|
100
|
-
// (for example, a transaction that affects the entire document).
|
|
101
|
-
// Such transactions can happen during collab syncing via y-prosemirror, for example.
|
|
102
|
-
tr.steps.some((step) => {
|
|
103
|
-
return (stepHasFromTo(step) &&
|
|
104
|
-
oldNodes.some((node) => node.pos >= step.from &&
|
|
105
|
-
node.pos + node.node.nodeSize <= step.to));
|
|
106
|
-
})) {
|
|
107
|
-
return getDecorations(tr.doc);
|
|
98
|
+
const cached = cache.get(node);
|
|
99
|
+
if (cached) {
|
|
100
|
+
// node is in cache, but decorations may be missing (for example, after undo)
|
|
101
|
+
if (!decoSet.find(pos, pos + node.nodeSize).length) {
|
|
102
|
+
return decoSet.add(tr.doc, renderTree(cached, pos + 1));
|
|
108
103
|
}
|
|
104
|
+
return decoSet;
|
|
109
105
|
}
|
|
110
|
-
|
|
111
|
-
|
|
106
|
+
decoSet = decoSet.remove(decoSet.find(pos, pos + node.nodeSize));
|
|
107
|
+
const ast = lowlight.highlight(lang, node.textContent);
|
|
108
|
+
const parsed = parseNodes(ast.children);
|
|
109
|
+
cache.set(node, parsed);
|
|
110
|
+
return decoSet.add(tr.doc, renderTree(parsed, pos + 1));
|
|
111
|
+
});
|
|
112
|
+
return { cache, decoSet };
|
|
112
113
|
},
|
|
113
114
|
},
|
|
114
115
|
view: (v) => {
|
|
@@ -119,8 +120,8 @@ const CodeBlockHighlight = (builder, opts) => {
|
|
|
119
120
|
});
|
|
120
121
|
},
|
|
121
122
|
props: {
|
|
122
|
-
decorations
|
|
123
|
-
return
|
|
123
|
+
decorations(state) {
|
|
124
|
+
return pluginKey.getState(state)?.decoSet;
|
|
124
125
|
},
|
|
125
126
|
nodeViews: {
|
|
126
127
|
[CodeBlockSpecs_1.codeBlockNodeName]: CodeBlockNodeView_1.CodeBlockNodeView.withOpts(opts),
|
|
@@ -128,53 +129,61 @@ const CodeBlockHighlight = (builder, opts) => {
|
|
|
128
129
|
},
|
|
129
130
|
});
|
|
130
131
|
});
|
|
131
|
-
function getDecorations(doc) {
|
|
132
|
-
const decos = [];
|
|
132
|
+
function getDecorations(doc, cache) {
|
|
133
133
|
if (!lowlight) {
|
|
134
134
|
return prosemirror_view_1.DecorationSet.empty;
|
|
135
135
|
}
|
|
136
|
+
const decos = [];
|
|
136
137
|
for (const { node, pos } of (0, prosemirror_utils_1.findChildrenByType)(doc, (0, CodeBlockSpecs_1.codeBlockType)(doc.type.schema), true)) {
|
|
137
|
-
let from = pos + 1;
|
|
138
|
-
let nodes;
|
|
139
138
|
const lang = node.attrs[CodeBlockSpecs_1.CodeBlockNodeAttr.Lang];
|
|
140
|
-
if (lang
|
|
141
|
-
nodes = lowlight.highlight(lang, node.textContent).children;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
139
|
+
if (!lang || !lowlight.registered(lang)) {
|
|
144
140
|
continue;
|
|
145
141
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
from = to;
|
|
142
|
+
// Try to get parsed result from cache using node as key
|
|
143
|
+
let parsedNodes = cache.get(node);
|
|
144
|
+
if (!parsedNodes) {
|
|
145
|
+
// Compute, parse and cache using the node itself as key
|
|
146
|
+
const nodes = lowlight.highlight(lang, node.textContent).children;
|
|
147
|
+
parsedNodes = parseNodes(nodes);
|
|
148
|
+
cache.set(node, parsedNodes);
|
|
154
149
|
}
|
|
150
|
+
decos.push(...renderTree(parsedNodes, pos + 1));
|
|
155
151
|
}
|
|
156
152
|
return prosemirror_view_1.DecorationSet.create(doc, decos);
|
|
157
153
|
}
|
|
158
154
|
};
|
|
159
155
|
exports.CodeBlockHighlight = CodeBlockHighlight;
|
|
156
|
+
function renderTree(parsedNodes, from) {
|
|
157
|
+
const decos = [];
|
|
158
|
+
for (const { text, classes } of parsedNodes) {
|
|
159
|
+
const to = from + text.length;
|
|
160
|
+
if (classes.length) {
|
|
161
|
+
decos.push(prosemirror_view_1.Decoration.inline(from, to, {
|
|
162
|
+
class: classes.join(' '),
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
from = to;
|
|
166
|
+
}
|
|
167
|
+
return decos;
|
|
168
|
+
}
|
|
160
169
|
function parseNodes(nodes, className = []) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
170
|
+
const result = [];
|
|
171
|
+
collectNodes(nodes, className, result);
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
function collectNodes(nodes, className, result) {
|
|
175
|
+
for (const node of nodes) {
|
|
164
176
|
if (node.type === 'element') {
|
|
165
|
-
classes =
|
|
166
|
-
|
|
177
|
+
const classes = className.concat(node.properties.className ?? []);
|
|
178
|
+
collectNodes(node.children, classes, result);
|
|
167
179
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
function stepHasFromTo(step) {
|
|
176
|
-
// @ts-expect-error
|
|
177
|
-
return typeof step.from === 'number' && typeof step.to === 'number';
|
|
180
|
+
else {
|
|
181
|
+
result.push({
|
|
182
|
+
text: node.type === 'comment' || node.type === 'text' ? node.value : '',
|
|
183
|
+
classes: className,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
178
187
|
}
|
|
179
188
|
function sortLangs(a, b) {
|
|
180
189
|
// plaintext always goes first
|
package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeBlockHighlight.js","sourceRoot":"../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.ts"],"names":[],"mappings":";;;AAKA,yDAAoD;AAEpD,oCAAoC;AACpC,yDAAqD;AACrD,uDAA4E;AAG5E,kDAA8C;AAC9C,kDAAgD;AAChD,+DAK2B;AAE3B,8DAAsD;AACtD,4DAAiE;AACjE,sCAAsC;AACtC,wFAAgF;AAChF,0FAAkF;AAElF,oCAAmC;AAYnC,MAAM,GAAG,GAAG,IAAI,6BAAS,CAAgB,sBAAsB,CAAC,CAAC;AAU1D,MAAM,kBAAkB,GAA6C,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IAC1F,IAAI,KAAoC,CAAC;IACzC,IAAI,QAAkB,CAAC;IACvB,IAAI,IAAiB,CAAC;IAEtB,MAAM,WAAW,GAAG,KAAK,IAAI,EAAE;QAC3B,IAAI,CAAC;YACD,IAAI,GAAG,CAAC,MAAM,MAAM,CAAC,uBAAuB,CAAC,CAAC,CAAC,OAAO,CAAC;YACvD,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;YAErC,MAAM,GAAG,GAAqB,GAAG,CAAC,GAAG,CAAC;YACtC,MAAM,MAAM,GAA0B,GAAG,CAAC,cAAc,CAAC;YACzD,KAAK,GAAG,EAAC,GAAG,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,EAAC,CAAC;YAChC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BAAa;QAAE,OAAO,CAAC,CAAC,CAAC;IACzC,IAAI,CAAC,CAAC,KAAK,KAAK,qBAAa;QAAE,OAAO,CAAC,CAAC;IACxC,OAAO,CAAC,CAAC;AACb,CAAC","sourcesContent":["import type {Options} from '@diplodoc/transform';\n// importing only type, because lowlight and highlight.js is optional deps\nimport type HLJS from 'highlight.js/lib/core';\nimport type {createLowlight} from 'lowlight' with {'resolution-mode': 'import'};\nimport type {Node} from 'prosemirror-model';\nimport {Plugin, PluginKey} from 'prosemirror-state';\nimport type {Step} from 'prosemirror-transform';\n// @ts-ignore // TODO: fix cjs build\nimport {findChildrenByType} from 'prosemirror-utils';\nimport {Decoration, DecorationSet, type EditorView} from 'prosemirror-view';\n\nimport type {ExtensionAuto} from '../../../../core';\nimport {capitalize} from '../../../../lodash';\nimport {globalLogger} from '../../../../logger';\nimport {\n CodeBlockNodeAttr,\n type LineNumbersOptions,\n codeBlockNodeName,\n codeBlockType,\n} from '../CodeBlockSpecs';\n\nimport {CodeBlockNodeView} from './CodeBlockNodeView';\nimport {codeLangSelectTooltipViewCreator} from './TooltipPlugin';\nimport {PlainTextLang} from './const';\nimport {codeBlockLineNumbersPlugin} from './plugins/codeBlockLineNumbersPlugin';\nimport {codeBlockLineWrappingPlugin} from './plugins/codeBlockLineWrappingPlugin';\n\nimport './CodeBlockHighlight.scss';\n\nexport type HighlightLangMap = Options['highlightLangs'];\n\ntype Lowlight = ReturnType<typeof createLowlight>;\ntype Root = ReturnType<Lowlight['highlight']>;\n\ntype LangSelectItem = {\n value: string;\n content: string;\n};\n\nconst key = new PluginKey<DecorationSet>('code_block_highlight');\n\nexport type CodeBlockHighlightOptions = {\n lineWrapping?: {\n enabled?: boolean;\n };\n lineNumbers?: LineNumbersOptions;\n langs?: HighlightLangMap;\n};\n\nexport const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (builder, opts) => {\n let langs: NonNullable<HighlightLangMap>;\n let lowlight: Lowlight;\n let hljs: typeof HLJS;\n\n const loadModules = async () => {\n try {\n hljs = (await import('highlight.js/lib/core')).default;\n const low = await import('lowlight');\n\n const all: HighlightLangMap = low.all;\n const create: typeof createLowlight = low.createLowlight;\n langs = {...all, ...opts.langs};\n lowlight = create(langs);\n return true;\n } catch (e) {\n globalLogger.info('Skip code_block highlighting');\n builder.logger.log('Skip code_block highlighting');\n return false;\n }\n };\n\n if (opts.lineWrapping?.enabled) builder.addPlugin(codeBlockLineWrappingPlugin);\n if (opts.lineNumbers?.enabled) builder.addPlugin(codeBlockLineNumbersPlugin);\n\n builder.addPlugin(() => {\n let modulesLoaded = false;\n let view: EditorView | null = null;\n\n // empty array by default, but is filled after loading modules\n const selectItems: LangSelectItem[] = [];\n const mapping: Record<string, string> = {};\n\n // TODO: add TAB key handler\n // TODO: Remove constant selection of block\n return new Plugin<DecorationSet>({\n key,\n state: {\n init: (_, state) => {\n loadModules().then((loaded) => {\n modulesLoaded = loaded;\n\n if (modulesLoaded) {\n for (const lang of Object.keys(langs)) {\n const defs = langs[lang](hljs);\n selectItems.push({\n value: lang,\n content: defs.name || capitalize(lang),\n });\n if (defs.aliases) {\n for (const alias of defs.aliases) {\n mapping[alias] = lang;\n }\n }\n }\n\n selectItems.sort(sortLangs);\n\n if (view && !view.isDestroyed) {\n view.dispatch(view.state.tr.setMeta(key, {modulesLoaded}));\n }\n }\n });\n return getDecorations(state.doc);\n },\n apply: (tr, decos, oldState, newState) => {\n if (!modulesLoaded) {\n return DecorationSet.empty;\n }\n\n if (tr.getMeta(key)?.modulesLoaded) {\n return getDecorations(tr.doc);\n }\n\n if (tr.docChanged) {\n const oldNodeName = oldState.selection.$head.parent.type.name;\n const newNodeName = newState.selection.$head.parent.type.name;\n\n // Apply decorations if:\n if (\n oldNodeName === codeBlockNodeName ||\n newNodeName === codeBlockNodeName\n ) {\n // selection includes codeblock node,\n return getDecorations(tr.doc);\n } else {\n const oldNodes = findChildrenByType(\n oldState.doc,\n codeBlockType(oldState.schema),\n );\n const newNodes = findChildrenByType(\n newState.doc,\n codeBlockType(newState.schema),\n );\n if (\n // OR transaction adds/removes codeblock nodes,\n newNodes.length !== oldNodes.length ||\n // OR transaction has changes that completely encapsulate a node\n // (for example, a transaction that affects the entire document).\n // Such transactions can happen during collab syncing via y-prosemirror, for example.\n tr.steps.some((step) => {\n return (\n stepHasFromTo(step) &&\n oldNodes.some(\n (node: {node: Node; pos: number}) =>\n node.pos >= step.from &&\n node.pos + node.node.nodeSize <= step.to,\n )\n );\n })\n ) {\n return getDecorations(tr.doc);\n }\n }\n }\n return decos.map(tr.mapping, tr.doc);\n },\n },\n view: (v) => {\n view = v;\n return codeLangSelectTooltipViewCreator(view, selectItems, mapping, {\n showCodeWrapping: Boolean(opts.lineWrapping?.enabled),\n showLineNumbers: Boolean(opts.lineNumbers?.enabled),\n });\n },\n props: {\n decorations: (state) => {\n return key.getState(state);\n },\n nodeViews: {\n [codeBlockNodeName]: CodeBlockNodeView.withOpts(opts),\n },\n },\n });\n });\n\n function getDecorations(doc: Node) {\n const decos: Decoration[] = [];\n\n if (!lowlight) {\n return DecorationSet.empty;\n }\n\n for (const {node, pos} of findChildrenByType(doc, codeBlockType(doc.type.schema), true)) {\n let from = pos + 1;\n let nodes: Root['children'];\n\n const lang: string | undefined = node.attrs[CodeBlockNodeAttr.Lang];\n if (lang && lowlight.registered(lang)) {\n nodes = lowlight.highlight(lang, node.textContent).children;\n } else {\n continue;\n }\n\n for (const {text, classes} of parseNodes(nodes)) {\n const to = from + text.length;\n if (classes.length) {\n decos.push(\n Decoration.inline(from, to, {\n class: classes.join(' '),\n }),\n );\n }\n from = to;\n }\n }\n\n return DecorationSet.create(doc, decos);\n }\n};\n\nfunction parseNodes(\n nodes: Root['children'],\n className: readonly string[] = [],\n): {text: string; classes: readonly string[]}[] {\n return nodes\n .map((node) => {\n let classes = className;\n if (node.type === 'element') {\n classes = classes.concat((node.properties.className as string[]) ?? []);\n return parseNodes(node.children, classes);\n }\n\n return {\n text: node.type === 'comment' || node.type === 'text' ? node.value : '',\n classes,\n };\n })\n .flat();\n}\n\nfunction stepHasFromTo(step: Step): step is Step & {from: number; to: number} {\n // @ts-expect-error\n return typeof step.from === 'number' && typeof step.to === 'number';\n}\n\nfunction sortLangs(a: LangSelectItem, b: LangSelectItem): number {\n // plaintext always goes first\n if (a.value === PlainTextLang) return -1;\n if (b.value === PlainTextLang) return 1;\n return 0;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"CodeBlockHighlight.js","sourceRoot":"../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.ts"],"names":[],"mappings":";;;AAKA,yDAAoD;AACpD,oCAAoC;AACpC,yDAAqD;AACrD,uDAA4E;AAG5E,kDAA8C;AAC9C,kDAAgD;AAChD,+DAK2B;AAE3B,8DAAsD;AACtD,4DAAiE;AACjE,sCAAsC;AACtC,wFAAgF;AAChF,0FAAkF;AAClF,sCAAiD;AAEjD,oCAAmC;AAYnC,MAAM,SAAS,GAAG,IAAI,6BAAS,CAAc,sBAAsB,CAAC,CAAC;AAoB9D,MAAM,kBAAkB,GAA6C,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IAC1F,IAAI,KAAoC,CAAC;IACzC,IAAI,QAAkB,CAAC;IACvB,IAAI,IAAiB,CAAC;IAEtB,MAAM,WAAW,GAAG,KAAK,IAAI,EAAE;QAC3B,IAAI,CAAC;YACD,IAAI,GAAG,CAAC,MAAM,MAAM,CAAC,uBAAuB,CAAC,CAAC,CAAC,OAAO,CAAC;YACvD,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;YAErC,MAAM,GAAG,GAAqB,GAAG,CAAC,GAAG,CAAC;YACtC,MAAM,MAAM,GAA0B,GAAG,CAAC,cAAc,CAAC;YACzD,KAAK,GAAG,EAAC,GAAG,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,EAAC,CAAC;YAChC,QAAQ,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;YACzB,OAAO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;AAClB,CAAC;AAED,SAAS,YAAY,CACjB,KAAuB,EACvB,SAA4B,EAC5B,MAA2B;IAE3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC1B,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAE,IAAI,CAAC,UAAU,CAAC,SAAsB,IAAI,EAAE,CAAC,CAAC;YAChF,YAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QACjD,CAAC;aAAM,CAAC;YACJ,MAAM,CAAC,IAAI,CAAC;gBACR,IAAI,EAAE,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;gBACvE,OAAO,EAAE,SAAS;aACrB,CAAC,CAAC;QACP,CAAC;IACL,CAAC;AACL,CAAC;AAED,SAAS,SAAS,CAAC,CAAiB,EAAE,CAAiB;IACnD,8BAA8B;IAC9B,IAAI,CAAC,CAAC,KAAK,KAAK,qBAAa;QAAE,OAAO,CAAC,CAAC,CAAC;IACzC,IAAI,CAAC,CAAC,KAAK,KAAK,qBAAa;QAAE,OAAO,CAAC,CAAC;IACxC,OAAO,CAAC,CAAC;AACb,CAAC","sourcesContent":["import type {Options} from '@diplodoc/transform';\n// importing only type, because lowlight and highlight.js is optional deps\nimport type HLJS from 'highlight.js/lib/core';\nimport type {createLowlight} from 'lowlight' with {'resolution-mode': 'import'};\nimport type {Node} from 'prosemirror-model';\nimport {Plugin, PluginKey} from 'prosemirror-state';\n// @ts-ignore // TODO: fix cjs build\nimport {findChildrenByType} from 'prosemirror-utils';\nimport {Decoration, DecorationSet, type EditorView} from 'prosemirror-view';\n\nimport type {ExtensionAuto} from '../../../../core';\nimport {capitalize} from '../../../../lodash';\nimport {globalLogger} from '../../../../logger';\nimport {\n CodeBlockNodeAttr,\n type LineNumbersOptions,\n codeBlockNodeName,\n codeBlockType,\n} from '../CodeBlockSpecs';\n\nimport {CodeBlockNodeView} from './CodeBlockNodeView';\nimport {codeLangSelectTooltipViewCreator} from './TooltipPlugin';\nimport {PlainTextLang} from './const';\nimport {codeBlockLineNumbersPlugin} from './plugins/codeBlockLineNumbersPlugin';\nimport {codeBlockLineWrappingPlugin} from './plugins/codeBlockLineWrappingPlugin';\nimport {processChangedCodeBlocks} from './utils';\n\nimport './CodeBlockHighlight.scss';\n\nexport type HighlightLangMap = Options['highlightLangs'];\n\ntype Lowlight = ReturnType<typeof createLowlight>;\ntype Root = ReturnType<Lowlight['highlight']>;\n\ntype LangSelectItem = {\n value: string;\n content: string;\n};\n\nconst pluginKey = new PluginKey<PluginState>('code_block_highlight');\n\n// Cache for parsed highlight results, using ProseMirror nodes as keys\ntype HighlightCache = WeakMap<Node, HighlightParsedTree>;\n\ntype HighlightParsedTree = {text: string; classes: readonly string[]}[];\n\ntype PluginState = {\n cache: HighlightCache;\n decoSet: DecorationSet;\n};\n\nexport type CodeBlockHighlightOptions = {\n lineWrapping?: {\n enabled?: boolean;\n };\n lineNumbers?: LineNumbersOptions;\n langs?: HighlightLangMap;\n};\n\nexport const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (builder, opts) => {\n let langs: NonNullable<HighlightLangMap>;\n let lowlight: Lowlight;\n let hljs: typeof HLJS;\n\n const loadModules = async () => {\n try {\n hljs = (await import('highlight.js/lib/core')).default;\n const low = await import('lowlight');\n\n const all: HighlightLangMap = low.all;\n const create: typeof createLowlight = low.createLowlight;\n langs = {...all, ...opts.langs};\n lowlight = create(langs);\n return true;\n } catch (e) {\n globalLogger.info('Skip code_block highlighting');\n builder.logger.log('Skip code_block highlighting');\n return false;\n }\n };\n\n if (opts.lineWrapping?.enabled) builder.addPlugin(codeBlockLineWrappingPlugin);\n if (opts.lineNumbers?.enabled) builder.addPlugin(codeBlockLineNumbersPlugin);\n\n builder.addPlugin(() => {\n let modulesLoaded = false;\n let view: EditorView | null = null;\n\n // empty array by default, but is filled after loading modules\n const selectItems: LangSelectItem[] = [];\n const mapping: Record<string, string> = {};\n\n // TODO: add TAB key handler\n // TODO: Remove constant selection of block\n return new Plugin<PluginState>({\n key: pluginKey,\n state: {\n init: (_, state) => {\n loadModules().then((loaded) => {\n modulesLoaded = loaded;\n\n if (modulesLoaded) {\n for (const lang of Object.keys(langs)) {\n const defs = langs[lang](hljs);\n selectItems.push({\n value: lang,\n content: defs.name || capitalize(lang),\n });\n if (defs.aliases) {\n for (const alias of defs.aliases) {\n mapping[alias] = lang;\n }\n }\n }\n\n selectItems.sort(sortLangs);\n\n if (view && !view.isDestroyed) {\n view.dispatch(view.state.tr.setMeta(pluginKey, {modulesLoaded}));\n }\n }\n });\n\n const cache: HighlightCache = new WeakMap();\n\n return {\n cache,\n decoSet: modulesLoaded\n ? DecorationSet.empty\n : getDecorations(state.doc, cache),\n };\n },\n apply: (tr, {cache, decoSet}) => {\n if (!modulesLoaded) {\n return {cache, decoSet: DecorationSet.empty};\n }\n\n if (tr.getMeta(pluginKey)?.modulesLoaded) {\n return {cache, decoSet: getDecorations(tr.doc, cache)};\n }\n\n if (!tr.docChanged) return {cache, decoSet};\n\n decoSet = processChangedCodeBlocks(tr, decoSet, (node, pos, decoSet) => {\n const lang: string | undefined = node.attrs[CodeBlockNodeAttr.Lang];\n\n if (!lang || !lowlight.registered(lang)) {\n return decoSet.remove(decoSet.find(pos, pos + node.nodeSize));\n }\n\n const cached = cache.get(node);\n if (cached) {\n // node is in cache, but decorations may be missing (for example, after undo)\n if (!decoSet.find(pos, pos + node.nodeSize).length) {\n return decoSet.add(tr.doc, renderTree(cached, pos + 1));\n }\n return decoSet;\n }\n\n decoSet = decoSet.remove(decoSet.find(pos, pos + node.nodeSize));\n const ast = lowlight.highlight(lang, node.textContent);\n const parsed = parseNodes(ast.children);\n cache.set(node, parsed);\n return decoSet.add(tr.doc, renderTree(parsed, pos + 1));\n });\n\n return {cache, decoSet};\n },\n },\n view: (v) => {\n view = v;\n return codeLangSelectTooltipViewCreator(view, selectItems, mapping, {\n showCodeWrapping: Boolean(opts.lineWrapping?.enabled),\n showLineNumbers: Boolean(opts.lineNumbers?.enabled),\n });\n },\n props: {\n decorations(state) {\n return pluginKey.getState(state)?.decoSet;\n },\n nodeViews: {\n [codeBlockNodeName]: CodeBlockNodeView.withOpts(opts),\n },\n },\n });\n });\n\n function getDecorations(doc: Node, cache: HighlightCache) {\n if (!lowlight) {\n return DecorationSet.empty;\n }\n\n const decos: Decoration[] = [];\n\n for (const {node, pos} of findChildrenByType(doc, codeBlockType(doc.type.schema), true)) {\n const lang: string | undefined = node.attrs[CodeBlockNodeAttr.Lang];\n if (!lang || !lowlight.registered(lang)) {\n continue;\n }\n\n // Try to get parsed result from cache using node as key\n let parsedNodes = cache.get(node);\n if (!parsedNodes) {\n // Compute, parse and cache using the node itself as key\n const nodes = lowlight.highlight(lang, node.textContent).children;\n parsedNodes = parseNodes(nodes);\n cache.set(node, parsedNodes);\n }\n\n decos.push(...renderTree(parsedNodes, pos + 1));\n }\n\n return DecorationSet.create(doc, decos);\n }\n};\n\nfunction renderTree(parsedNodes: HighlightParsedTree, from: number): Decoration[] {\n const decos: Decoration[] = [];\n\n for (const {text, classes} of parsedNodes) {\n const to = from + text.length;\n if (classes.length) {\n decos.push(\n Decoration.inline(from, to, {\n class: classes.join(' '),\n }),\n );\n }\n from = to;\n }\n\n return decos;\n}\n\nfunction parseNodes(\n nodes: Root['children'],\n className: readonly string[] = [],\n): HighlightParsedTree {\n const result: HighlightParsedTree = [];\n collectNodes(nodes, className, result);\n return result;\n}\n\nfunction collectNodes(\n nodes: Root['children'],\n className: readonly string[],\n result: HighlightParsedTree,\n): void {\n for (const node of nodes) {\n if (node.type === 'element') {\n const classes = className.concat((node.properties.className as string[]) ?? []);\n collectNodes(node.children, classes, result);\n } else {\n result.push({\n text: node.type === 'comment' || node.type === 'text' ? node.value : '',\n classes: className,\n });\n }\n }\n}\n\nfunction sortLangs(a: LangSelectItem, b: LangSelectItem): number {\n // plaintext always goes first\n if (a.value === PlainTextLang) return -1;\n if (b.value === PlainTextLang) return 1;\n return 0;\n}\n"]}
|
|
@@ -13,43 +13,63 @@ const codeBlockLineNumbersPlugin = () => {
|
|
|
13
13
|
key: pluginKey,
|
|
14
14
|
state: {
|
|
15
15
|
init(_config, state) {
|
|
16
|
-
|
|
16
|
+
const cache = new WeakSet();
|
|
17
|
+
return { cache, decoSet: buildAllDecorations(state.doc, cache) };
|
|
17
18
|
},
|
|
18
|
-
apply(tr,
|
|
19
|
-
if (tr.docChanged)
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
apply(tr, { cache, decoSet }) {
|
|
20
|
+
if (!tr.docChanged)
|
|
21
|
+
return { cache, decoSet };
|
|
22
|
+
decoSet = (0, utils_2.processChangedCodeBlocks)(tr, decoSet, (node, pos, decoSet) => {
|
|
23
|
+
if (!(0, utils_2.isLineNumbersVisible)(node)) {
|
|
24
|
+
return decoSet.remove(decoSet.find(pos, pos + node.nodeSize));
|
|
25
|
+
}
|
|
26
|
+
if (cache.has(node)) {
|
|
27
|
+
// node has not changed, but decorations may be missing (after undo/redo)
|
|
28
|
+
if (!decoSet.find(pos, pos + node.nodeSize).length) {
|
|
29
|
+
return decoSet.add(tr.doc, buildNodeDecorations(node, pos));
|
|
30
|
+
}
|
|
31
|
+
return decoSet;
|
|
32
|
+
}
|
|
33
|
+
decoSet = decoSet.remove(decoSet.find(pos, pos + node.nodeSize));
|
|
34
|
+
cache.add(node);
|
|
35
|
+
return decoSet.add(tr.doc, buildNodeDecorations(node, pos));
|
|
36
|
+
});
|
|
37
|
+
return { cache, decoSet };
|
|
23
38
|
},
|
|
24
39
|
},
|
|
25
40
|
props: {
|
|
26
41
|
decorations(state) {
|
|
27
|
-
return pluginKey.getState(state);
|
|
42
|
+
return pluginKey.getState(state)?.decoSet;
|
|
28
43
|
},
|
|
29
44
|
},
|
|
30
45
|
});
|
|
31
46
|
};
|
|
32
47
|
exports.codeBlockLineNumbersPlugin = codeBlockLineNumbersPlugin;
|
|
33
|
-
function
|
|
48
|
+
function buildAllDecorations(doc, cache) {
|
|
34
49
|
const decos = [];
|
|
35
50
|
for (const { node, pos } of (0, utils_1.findChildrenByType)(doc, (0, CodeBlockSpecs_1.codeBlockType)(doc.type.schema), true)) {
|
|
36
51
|
if (!(0, utils_2.isLineNumbersVisible)(node))
|
|
37
52
|
continue;
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
let shift = 0;
|
|
42
|
-
for (let i = 0; i < contentByLines.length; i++) {
|
|
43
|
-
const line = contentByLines[i];
|
|
44
|
-
{
|
|
45
|
-
const elem = document.createElement('span');
|
|
46
|
-
elem.classList.add('yfm-line-number');
|
|
47
|
-
elem.textContent = String(i + 1).padStart(maxDigits, ' ');
|
|
48
|
-
decos.push(view_1.Decoration.widget(pos + shift + 1, elem, { side: 1, ignoreSelection: true }));
|
|
49
|
-
}
|
|
50
|
-
shift += line.length + 1;
|
|
51
|
-
}
|
|
53
|
+
const nodeDecos = buildNodeDecorations(node, pos);
|
|
54
|
+
cache.add(node);
|
|
55
|
+
decos.push(...nodeDecos);
|
|
52
56
|
}
|
|
53
57
|
return view_1.DecorationSet.create(doc, decos);
|
|
54
58
|
}
|
|
59
|
+
function buildNodeDecorations(node, pos) {
|
|
60
|
+
const decos = [];
|
|
61
|
+
const codeContent = node.textContent;
|
|
62
|
+
const contentByLines = codeContent.split('\n');
|
|
63
|
+
const maxDigits = String(contentByLines.length).length;
|
|
64
|
+
let shift = 0;
|
|
65
|
+
for (let i = 0; i < contentByLines.length; i++) {
|
|
66
|
+
const line = contentByLines[i];
|
|
67
|
+
const elem = document.createElement('span');
|
|
68
|
+
elem.classList.add('yfm-line-number');
|
|
69
|
+
elem.textContent = String(i + 1).padStart(maxDigits, ' ');
|
|
70
|
+
decos.push(view_1.Decoration.widget(pos + shift + 1, elem, { side: 1, ignoreSelection: true }));
|
|
71
|
+
shift += line.length + 1;
|
|
72
|
+
}
|
|
73
|
+
return decos;
|
|
74
|
+
}
|
|
55
75
|
//# sourceMappingURL=codeBlockLineNumbersPlugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codeBlockLineNumbersPlugin.js","sourceRoot":"../../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/plugins/codeBlockLineNumbersPlugin.ts"],"names":[],"mappings":";;;AACA,sDAA4C;AAC5C,sDAA6C;AAC7C,oDAAmD;AAEnD,kEAAmD;AACnD,
|
|
1
|
+
{"version":3,"file":"codeBlockLineNumbersPlugin.js","sourceRoot":"../../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/plugins/codeBlockLineNumbersPlugin.ts"],"names":[],"mappings":";;;AACA,sDAA4C;AAC5C,sDAA6C;AAC7C,oDAAmD;AAEnD,kEAAmD;AACnD,uCAAwE;AASxE,MAAM,SAAS,GAAG,IAAI,iBAAS,CAAc,qCAAqC,CAAC,CAAC;AAEpF,gBAAgB;AACT,MAAM,0BAA0B,GAAG,GAAG,EAAE;IAC3C,OAAO,IAAI,cAAM,CAAc;QAC3B,GAAG,EAAE,SAAS;QACd,KAAK,EAAE;YACH,IAAI,CAAC,OAAO,EAAE,KAAK;gBACf,MAAM,KAAK,GAAoB,IAAI,OAAO,EAAE,CAAC;gBAC7C,OAAO,EAAC,KAAK,EAAE,OAAO,EAAE,mBAAmB,CAAC,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,EAAC,CAAC;YACnE,CAAC;YACD,KAAK,CAAC,EAAE,EAAE,EAAC,KAAK,EAAE,OAAO,EAAC;gBACtB,IAAI,CAAC,EAAE,CAAC,UAAU;oBAAE,OAAO,EAAC,KAAK,EAAE,OAAO,EAAC,CAAC;gBAE5C,OAAO,GAAG,IAAA,gCAAwB,EAAC,EAAE,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG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,KAAK,CAAC,IAAI,CAAC,iBAAU,CAAC,MAAM,CAAC,GAAG,GAAG,KAAK,GAAG,CAAC,EAAE,IAAI,EAAE,EAAC,IAAI,EAAE,CAAC,EAAE,eAAe,EAAE,IAAI,EAAC,CAAC,CAAC,CAAC;QAEvF,KAAK,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import type {Node} from '#pm/model';\nimport {Plugin, PluginKey} from '#pm/state';\nimport {findChildrenByType} from '#pm/utils';\nimport {Decoration, DecorationSet} from '#pm/view';\n\nimport {codeBlockType} from '../../CodeBlockSpecs';\nimport {isLineNumbersVisible, processChangedCodeBlocks} from '../utils';\n\ntype LineNumberCache = WeakSet<Node>;\n\ntype PluginState = {\n cache: LineNumberCache;\n decoSet: DecorationSet;\n};\n\nconst pluginKey = new PluginKey<PluginState>('code_block_line_numbers_decorations');\n\n/** @internal */\nexport const codeBlockLineNumbersPlugin = () => {\n return new Plugin<PluginState>({\n key: pluginKey,\n state: {\n init(_config, state) {\n const cache: LineNumberCache = new WeakSet();\n return {cache, decoSet: buildAllDecorations(state.doc, cache)};\n },\n apply(tr, {cache, decoSet}) {\n if (!tr.docChanged) return {cache, decoSet};\n\n decoSet = processChangedCodeBlocks(tr, decoSet, (node, pos, decoSet) => {\n if (!isLineNumbersVisible(node)) {\n return decoSet.remove(decoSet.find(pos, pos + node.nodeSize));\n }\n\n if (cache.has(node)) {\n // node has not changed, but decorations may be missing (after undo/redo)\n if (!decoSet.find(pos, pos + node.nodeSize).length) {\n return decoSet.add(tr.doc, buildNodeDecorations(node, pos));\n }\n return decoSet;\n }\n\n decoSet = decoSet.remove(decoSet.find(pos, pos + node.nodeSize));\n cache.add(node);\n return decoSet.add(tr.doc, buildNodeDecorations(node, pos));\n });\n\n return {cache, decoSet};\n },\n },\n props: {\n decorations(state) {\n return pluginKey.getState(state)?.decoSet;\n },\n },\n });\n};\n\nfunction buildAllDecorations(doc: Node, cache: LineNumberCache) {\n const decos: Decoration[] = [];\n\n for (const {node, pos} of findChildrenByType(doc, codeBlockType(doc.type.schema), true)) {\n if (!isLineNumbersVisible(node)) continue;\n\n const nodeDecos = buildNodeDecorations(node, pos);\n cache.add(node);\n decos.push(...nodeDecos);\n }\n\n return DecorationSet.create(doc, decos);\n}\n\nfunction buildNodeDecorations(node: Node, pos: number): Decoration[] {\n const decos: Decoration[] = [];\n const codeContent = node.textContent;\n const contentByLines = codeContent.split('\\n');\n const maxDigits = String(contentByLines.length).length;\n\n let shift = 0;\n for (let i = 0; i < contentByLines.length; i++) {\n const line = contentByLines[i];\n\n const elem = document.createElement('span');\n elem.classList.add('yfm-line-number');\n elem.textContent = String(i + 1).padStart(maxDigits, ' ');\n\n decos.push(Decoration.widget(pos + shift + 1, elem, {side: 1, ignoreSelection: true}));\n\n shift += line.length + 1;\n }\n\n return decos;\n}\n"]}
|
|
@@ -1,9 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isLineNumbersVisible = isLineNumbersVisible;
|
|
4
|
+
exports.processChangedCodeBlocks = processChangedCodeBlocks;
|
|
5
|
+
const transaction_1 = require("../../../../utils/transaction.js");
|
|
4
6
|
const CodeBlockSpecs_1 = require("../CodeBlockSpecs/index.js");
|
|
5
7
|
/** @internal */
|
|
6
8
|
function isLineNumbersVisible(node) {
|
|
7
9
|
return node.attrs[CodeBlockSpecs_1.CodeBlockNodeAttr.ShowLineNumbers] === 'true';
|
|
8
10
|
}
|
|
11
|
+
/** @internal */
|
|
12
|
+
function processChangedCodeBlocks(tr, decoSet, onCodeBlock) {
|
|
13
|
+
decoSet = decoSet.map(tr.mapping, tr.doc);
|
|
14
|
+
(0, transaction_1.forEachChangedNode)(tr, (node, pos) => {
|
|
15
|
+
if (node.type.name !== CodeBlockSpecs_1.codeBlockNodeName) {
|
|
16
|
+
if (node.isTextblock) {
|
|
17
|
+
// Remove stale decorations from non-code_block textblocks
|
|
18
|
+
// (e.g. when a code_block is converted to a paragraph)
|
|
19
|
+
decoSet = decoSet.remove(decoSet.find(pos, pos + node.nodeSize));
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
decoSet = onCodeBlock(node, pos, decoSet);
|
|
25
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
return decoSet;
|
|
28
|
+
}
|
|
9
29
|
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/utils.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/utils.ts"],"names":[],"mappings":";;AAQA,oDAEC;AAGD,4DAsBC;AAhCD,kEAAyD;AAEzD,+DAAuE;AAEvE,gBAAgB;AAChB,SAAgB,oBAAoB,CAAC,IAAU;IAC3C,OAAO,IAAI,CAAC,KAAK,CAAC,kCAAiB,CAAC,eAAe,CAAC,KAAK,MAAM,CAAC;AACpE,CAAC;AAED,gBAAgB;AAChB,SAAgB,wBAAwB,CACpC,EAAe,EACf,OAAsB,EACtB,WAA+E;IAE/E,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC;IAE1C,IAAA,gCAAkB,EAAC,EAAE,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;QACjC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,kCAAiB,EAAE,CAAC;YACvC,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;gBACnB,0DAA0D;gBAC1D,uDAAuD;gBACvD,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;gBACjE,OAAO,KAAK,CAAC;YACjB,CAAC;YACD,OAAO,IAAI,CAAC;QAChB,CAAC;QACD,OAAO,GAAG,WAAW,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;QAC1C,OAAO,KAAK,CAAC;IACjB,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AACnB,CAAC","sourcesContent":["import type {Node} from '#pm/model';\nimport type {Transaction} from '#pm/state';\nimport type {DecorationSet} from '#pm/view';\nimport {forEachChangedNode} from 'src/utils/transaction';\n\nimport {CodeBlockNodeAttr, codeBlockNodeName} from '../CodeBlockSpecs';\n\n/** @internal */\nexport function isLineNumbersVisible(node: Node): boolean {\n return node.attrs[CodeBlockNodeAttr.ShowLineNumbers] === 'true';\n}\n\n/** @internal */\nexport function processChangedCodeBlocks(\n tr: Transaction,\n decoSet: DecorationSet,\n onCodeBlock: (node: Node, pos: number, decoSet: DecorationSet) => DecorationSet,\n): DecorationSet {\n decoSet = decoSet.map(tr.mapping, tr.doc);\n\n forEachChangedNode(tr, (node, pos) => {\n if (node.type.name !== codeBlockNodeName) {\n if (node.isTextblock) {\n // Remove stale decorations from non-code_block textblocks\n // (e.g. when a code_block is converted to a paragraph)\n decoSet = decoSet.remove(decoSet.find(pos, pos + node.nodeSize));\n return false;\n }\n return true;\n }\n decoSet = onCodeBlock(node, pos, decoSet);\n return false;\n });\n\n return decoSet;\n}\n"]}
|
|
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "italicType", { enumerable: true, get: function (
|
|
|
12
12
|
const iAction = 'italic';
|
|
13
13
|
const Italic = (builder, opts) => {
|
|
14
14
|
builder.use(ItalicSpecs_1.ItalicSpecs);
|
|
15
|
-
builder.addAction(iAction, ({ schema }) => (0, actions_1.
|
|
15
|
+
builder.addAction(iAction, ({ schema }) => (0, actions_1.createMarkdownInlineMarkAction)((0, ItalicSpecs_1.italicType)(schema)));
|
|
16
16
|
builder.addInputRules(({ schema }) => ({
|
|
17
17
|
rules: [
|
|
18
18
|
(0, inputrules_1.markInputRule)({ open: '*', close: '*', ignoreBetween: '*' }, (0, ItalicSpecs_1.italicType)(schema)),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Italic/index.ts"],"names":[],"mappings":";;;AAAA,+DAAgD;AAGhD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Italic/index.ts"],"names":[],"mappings":";;;AAAA,+DAAgD;AAGhD,uDAAsE;AACtE,6DAAwD;AACxD,qDAAoD;AAEpD,wDAAsD;AAEtD,sDAAyD;AAAjD,6GAAA,cAAc,OAAA;AAAE,yGAAA,UAAU,OAAA;AAClC,MAAM,OAAO,GAAG,QAAQ,CAAC;AAMlB,MAAM,MAAM,GAAiC,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IAClE,OAAO,CAAC,GAAG,CAAC,yBAAW,CAAC,CAAC;IAEzB,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,IAAA,wCAA8B,EAAC,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAE7F,OAAO,CAAC,aAAa,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;QACjC,KAAK,EAAE;YACH,IAAA,0BAAa,EAAC,EAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,GAAG,EAAC,EAAE,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC;YAC9E,IAAA,0BAAa,EAAC,EAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,GAAG,EAAC,EAAE,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC;SACjF;KACJ,CAAC,CAAC,CAAC;IAEJ,IAAI,IAAI,EAAE,SAAS,EAAE,CAAC;QAClB,MAAM,EAAC,SAAS,EAAC,GAAG,IAAI,CAAC;QACzB,OAAO,CAAC,SAAS,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;YAC7B,CAAC,SAAS,CAAC,EAAE,IAAA,sBAAa,EAAC,QAAQ,EAAE,IAAA,iCAAU,EAAC,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC,CAAC;SACvE,CAAC,CAAC,CAAC;IACR,CAAC;AACL,CAAC,CAAC;AAlBW,QAAA,MAAM,UAkBjB","sourcesContent":["import {toggleMark} from 'prosemirror-commands';\n\nimport type {Action, ExtensionAuto} from '../../../core';\nimport {createMarkdownInlineMarkAction} from '../../../utils/actions';\nimport {markInputRule} from '../../../utils/inputrules';\nimport {withLogAction} from '../../../utils/keymap';\n\nimport {ItalicSpecs, italicType} from './ItalicSpecs';\n\nexport {italicMarkName, italicType} from './ItalicSpecs';\nconst iAction = 'italic';\n\nexport type ItalicOptions = {\n italicKey?: string | null;\n};\n\nexport const Italic: ExtensionAuto<ItalicOptions> = (builder, opts) => {\n builder.use(ItalicSpecs);\n\n builder.addAction(iAction, ({schema}) => createMarkdownInlineMarkAction(italicType(schema)));\n\n builder.addInputRules(({schema}) => ({\n rules: [\n markInputRule({open: '*', close: '*', ignoreBetween: '*'}, italicType(schema)),\n markInputRule({open: '_', close: '_', ignoreBetween: '_'}, italicType(schema)),\n ],\n }));\n\n if (opts?.italicKey) {\n const {italicKey} = opts;\n builder.addKeymap(({schema}) => ({\n [italicKey]: withLogAction('italic', toggleMark(italicType(schema))),\n }));\n }\n};\n\ndeclare global {\n namespace WysiwygEditor {\n interface Actions {\n [iAction]: Action;\n }\n }\n}\n"]}
|
|
@@ -11,7 +11,7 @@ const mAction = 'mark';
|
|
|
11
11
|
const Mark = (builder) => {
|
|
12
12
|
builder.use(MarkSpecs_1.MarkSpecs);
|
|
13
13
|
builder
|
|
14
|
-
.addAction(mAction, ({ schema }) => (0, actions_1.
|
|
14
|
+
.addAction(mAction, ({ schema }) => (0, actions_1.createMarkdownInlineMarkAction)((0, MarkSpecs_1.markMarkType)(schema)))
|
|
15
15
|
.addInputRules(({ schema }) => ({
|
|
16
16
|
rules: [
|
|
17
17
|
(0, inputrules_1.markInputRule)({ open: '==', close: '==', ignoreBetween: '=' }, (0, MarkSpecs_1.markMarkType)(schema)),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Mark/index.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Mark/index.ts"],"names":[],"mappings":";;;AACA,uDAAsE;AACtE,6DAAwD;AAExD,oDAAoD;AAEpD,kDAAuD;AAA/C,yGAAA,YAAY,OAAA;AAAE,yGAAA,YAAY,OAAA;AAClC,MAAM,OAAO,GAAG,MAAM,CAAC;AAEhB,MAAM,IAAI,GAAkB,CAAC,OAAO,EAAE,EAAE;IAC3C,OAAO,CAAC,GAAG,CAAC,qBAAS,CAAC,CAAC;IAEvB,OAAO;SACF,SAAS,CAAC,OAAO,EAAE,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,IAAA,wCAA8B,EAAC,IAAA,wBAAY,EAAC,MAAM,CAAC,CAAC,CAAC;SACtF,aAAa,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,KAAK,EAAE;YACH,IAAA,0BAAa,EAAC,EAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,EAAC,EAAE,IAAA,wBAAY,EAAC,MAAM,CAAC,CAAC;SACrF;KACJ,CAAC,CAAC,CAAC;AACZ,CAAC,CAAC;AAVW,QAAA,IAAI,QAUf","sourcesContent":["import type {Action, ExtensionAuto} from '../../../core';\nimport {createMarkdownInlineMarkAction} from '../../../utils/actions';\nimport {markInputRule} from '../../../utils/inputrules';\n\nimport {MarkSpecs, markMarkType} from './MarkSpecs';\n\nexport {markMarkName, markMarkType} from './MarkSpecs';\nconst mAction = 'mark';\n\nexport const Mark: ExtensionAuto = (builder) => {\n builder.use(MarkSpecs);\n\n builder\n .addAction(mAction, ({schema}) => createMarkdownInlineMarkAction(markMarkType(schema)))\n .addInputRules(({schema}) => ({\n rules: [\n markInputRule({open: '==', close: '==', ignoreBetween: '='}, markMarkType(schema)),\n ],\n }));\n};\n\ndeclare global {\n namespace WysiwygEditor {\n interface Actions {\n [mAction]: Action;\n }\n }\n}\n"]}
|
|
@@ -13,7 +13,7 @@ const sAction = 'strike';
|
|
|
13
13
|
const Strike = (builder, opts) => {
|
|
14
14
|
builder.use(StrikeSpecs_1.StrikeSpecs);
|
|
15
15
|
builder
|
|
16
|
-
.addAction(sAction, ({ schema }) => (0, actions_1.
|
|
16
|
+
.addAction(sAction, ({ schema }) => (0, actions_1.createMarkdownInlineMarkAction)((0, StrikeSpecs_1.strikeType)(schema)))
|
|
17
17
|
.addInputRules(({ schema }) => ({
|
|
18
18
|
rules: [
|
|
19
19
|
(0, inputrules_1.markInputRule)({ open: '~~', close: '~~', ignoreBetween: '~' }, (0, StrikeSpecs_1.strikeType)(schema)),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Strike/index.ts"],"names":[],"mappings":";;;AAAA,+DAAgD;AAGhD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Strike/index.ts"],"names":[],"mappings":";;;AAAA,+DAAgD;AAGhD,uDAAsE;AACtE,6DAAwD;AACxD,qDAAoD;AAEpD,wDAAsD;AAEtD,sDAAyD;AAAjD,6GAAA,cAAc,OAAA;AAAE,yGAAA,UAAU,OAAA;AAClC,MAAM,OAAO,GAAG,QAAQ,CAAC;AAMlB,MAAM,MAAM,GAAiC,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IAClE,OAAO,CAAC,GAAG,CAAC,yBAAW,CAAC,CAAC;IAEzB,OAAO;SACF,SAAS,CAAC,OAAO,EAAE,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,IAAA,wCAA8B,EAAC,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC,CAAC;SACpF,aAAa,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,KAAK,EAAE;YACH,IAAA,0BAAa,EAAC,EAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,EAAC,EAAE,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC;SACnF;KACJ,CAAC,CAAC,CAAC;IAER,IAAI,IAAI,EAAE,SAAS,EAAE,CAAC;QAClB,MAAM,EAAC,SAAS,EAAC,GAAG,IAAI,CAAC;QACzB,OAAO,CAAC,SAAS,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;YAC7B,CAAC,SAAS,CAAC,EAAE,IAAA,sBAAa,EAAC,QAAQ,EAAE,IAAA,iCAAU,EAAC,IAAA,wBAAU,EAAC,MAAM,CAAC,CAAC,CAAC;SACvE,CAAC,CAAC,CAAC;IACR,CAAC;AACL,CAAC,CAAC;AAjBW,QAAA,MAAM,UAiBjB","sourcesContent":["import {toggleMark} from 'prosemirror-commands';\n\nimport type {Action, ExtensionAuto} from '../../../core';\nimport {createMarkdownInlineMarkAction} from '../../../utils/actions';\nimport {markInputRule} from '../../../utils/inputrules';\nimport {withLogAction} from '../../../utils/keymap';\n\nimport {StrikeSpecs, strikeType} from './StrikeSpecs';\n\nexport {strikeMarkName, strikeType} from './StrikeSpecs';\nconst sAction = 'strike';\n\nexport type StrikeOptions = {\n strikeKey?: string | null;\n};\n\nexport const Strike: ExtensionAuto<StrikeOptions> = (builder, opts) => {\n builder.use(StrikeSpecs);\n\n builder\n .addAction(sAction, ({schema}) => createMarkdownInlineMarkAction(strikeType(schema)))\n .addInputRules(({schema}) => ({\n rules: [\n markInputRule({open: '~~', close: '~~', ignoreBetween: '~'}, strikeType(schema)),\n ],\n }));\n\n if (opts?.strikeKey) {\n const {strikeKey} = opts;\n builder.addKeymap(({schema}) => ({\n [strikeKey]: withLogAction('strike', toggleMark(strikeType(schema))),\n }));\n }\n};\n\ndeclare global {\n namespace WysiwygEditor {\n interface Actions {\n [sAction]: Action;\n }\n }\n}\n"]}
|
|
@@ -13,7 +13,7 @@ const undAction = 'underline';
|
|
|
13
13
|
const Underline = (builder, opts) => {
|
|
14
14
|
builder.use(UnderlineSpecs_1.UnderlineSpecs);
|
|
15
15
|
builder
|
|
16
|
-
.addAction(undAction, ({ schema }) => (0, actions_1.
|
|
16
|
+
.addAction(undAction, ({ schema }) => (0, actions_1.createMarkdownInlineMarkAction)((0, UnderlineSpecs_1.underlineType)(schema)))
|
|
17
17
|
.addInputRules(({ schema }) => ({
|
|
18
18
|
rules: [
|
|
19
19
|
(0, inputrules_1.markInputRule)({ open: '++', close: '++', ignoreBetween: '+' }, (0, UnderlineSpecs_1.underlineType)(schema)),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Underline/index.ts"],"names":[],"mappings":";;;AAAA,+DAAgD;AAGhD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/markdown/Underline/index.ts"],"names":[],"mappings":";;;AAAA,+DAAgD;AAGhD,uDAAsE;AACtE,6DAAwD;AACxD,qDAAoD;AAEpD,8DAA+D;AAE/D,4DAAkE;AAA1D,mHAAA,iBAAiB,OAAA;AAAE,+GAAA,aAAa,OAAA;AACxC,MAAM,SAAS,GAAG,WAAW,CAAC;AAMvB,MAAM,SAAS,GAAoC,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IACxE,OAAO,CAAC,GAAG,CAAC,+BAAc,CAAC,CAAC;IAE5B,OAAO;SACF,SAAS,CAAC,SAAS,EAAE,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,IAAA,wCAA8B,EAAC,IAAA,8BAAa,EAAC,MAAM,CAAC,CAAC,CAAC;SACzF,aAAa,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,KAAK,EAAE;YACH,IAAA,0BAAa,EAAC,EAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,EAAC,EAAE,IAAA,8BAAa,EAAC,MAAM,CAAC,CAAC;SACtF;KACJ,CAAC,CAAC,CAAC;IAER,IAAI,IAAI,EAAE,YAAY,EAAE,CAAC;QACrB,MAAM,EAAC,YAAY,EAAC,GAAG,IAAI,CAAC;QAC5B,OAAO,CAAC,SAAS,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;YAC7B,CAAC,YAAY,CAAC,EAAE,IAAA,sBAAa,EAAC,WAAW,EAAE,IAAA,iCAAU,EAAC,IAAA,8BAAa,EAAC,MAAM,CAAC,CAAC,CAAC;SAChF,CAAC,CAAC,CAAC;IACR,CAAC;AACL,CAAC,CAAC;AAjBW,QAAA,SAAS,aAiBpB","sourcesContent":["import {toggleMark} from 'prosemirror-commands';\n\nimport type {Action, ExtensionAuto} from '../../../core';\nimport {createMarkdownInlineMarkAction} from '../../../utils/actions';\nimport {markInputRule} from '../../../utils/inputrules';\nimport {withLogAction} from '../../../utils/keymap';\n\nimport {UnderlineSpecs, underlineType} from './UnderlineSpecs';\n\nexport {underlineMarkName, underlineType} from './UnderlineSpecs';\nconst undAction = 'underline';\n\nexport type UnderlineOptions = {\n underlineKey?: string | null;\n};\n\nexport const Underline: ExtensionAuto<UnderlineOptions> = (builder, opts) => {\n builder.use(UnderlineSpecs);\n\n builder\n .addAction(undAction, ({schema}) => createMarkdownInlineMarkAction(underlineType(schema)))\n .addInputRules(({schema}) => ({\n rules: [\n markInputRule({open: '++', close: '++', ignoreBetween: '+'}, underlineType(schema)),\n ],\n }));\n\n if (opts?.underlineKey) {\n const {underlineKey} = opts;\n builder.addKeymap(({schema}) => ({\n [underlineKey]: withLogAction('underline', toggleMark(underlineType(schema))),\n }));\n }\n};\n\ndeclare global {\n namespace WysiwygEditor {\n interface Actions {\n [undAction]: Action;\n }\n }\n}\n"]}
|
|
@@ -11,7 +11,7 @@ const monoAction = 'mono';
|
|
|
11
11
|
const Monospace = (builder) => {
|
|
12
12
|
builder.use(MonospaceSpecs_1.MonospaceSpecs);
|
|
13
13
|
builder
|
|
14
|
-
.addAction(monoAction, ({ schema }) => (0, actions_1.
|
|
14
|
+
.addAction(monoAction, ({ schema }) => (0, actions_1.createMarkdownInlineMarkAction)((0, MonospaceSpecs_1.monospaceType)(schema)))
|
|
15
15
|
.addInputRules(({ schema }) => ({
|
|
16
16
|
rules: [
|
|
17
17
|
(0, inputrules_1.markInputRule)({ open: '##', close: '##', ignoreBetween: '#' }, (0, MonospaceSpecs_1.monospaceType)(schema)),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/yfm/Monospace/index.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../../../../../src","sources":["extensions/yfm/Monospace/index.ts"],"names":[],"mappings":";;;AACA,uDAAsE;AACtE,6DAAwD;AAExD,8DAA+D;AAE/D,4DAAkE;AAA1D,mHAAA,iBAAiB,OAAA;AAAE,+GAAA,aAAa,OAAA;AACxC,MAAM,UAAU,GAAG,MAAM,CAAC;AAEnB,MAAM,SAAS,GAAkB,CAAC,OAAO,EAAE,EAAE;IAChD,OAAO,CAAC,GAAG,CAAC,+BAAc,CAAC,CAAC;IAE5B,OAAO;SACF,SAAS,CAAC,UAAU,EAAE,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,IAAA,wCAA8B,EAAC,IAAA,8BAAa,EAAC,MAAM,CAAC,CAAC,CAAC;SAC1F,aAAa,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,KAAK,EAAE;YACH,IAAA,0BAAa,EAAC,EAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,EAAC,EAAE,IAAA,8BAAa,EAAC,MAAM,CAAC,CAAC;SACtF;KACJ,CAAC,CAAC,CAAC;AACZ,CAAC,CAAC;AAVW,QAAA,SAAS,aAUpB","sourcesContent":["import type {Action, ExtensionAuto} from '../../../core';\nimport {createMarkdownInlineMarkAction} from '../../../utils/actions';\nimport {markInputRule} from '../../../utils/inputrules';\n\nimport {MonospaceSpecs, monospaceType} from './MonospaceSpecs';\n\nexport {monospaceMarkName, monospaceType} from './MonospaceSpecs';\nconst monoAction = 'mono';\n\nexport const Monospace: ExtensionAuto = (builder) => {\n builder.use(MonospaceSpecs);\n\n builder\n .addAction(monoAction, ({schema}) => createMarkdownInlineMarkAction(monospaceType(schema)))\n .addInputRules(({schema}) => ({\n rules: [\n markInputRule({open: '##', close: '##', ignoreBetween: '#'}, monospaceType(schema)),\n ],\n }));\n};\n\ndeclare global {\n namespace WysiwygEditor {\n interface Actions {\n [monoAction]: Action;\n }\n }\n}\n"]}
|
|
@@ -2,3 +2,9 @@ import type { MarkType } from 'prosemirror-model';
|
|
|
2
2
|
import type { ActionSpec } from "../core/index.js";
|
|
3
3
|
export declare function defineActions<Keys extends string>(actions: Record<Keys, ActionSpec>): Record<Keys, ActionSpec>;
|
|
4
4
|
export declare function createToggleMarkAction(markType: MarkType): ActionSpec;
|
|
5
|
+
/**
|
|
6
|
+
* Like createToggleMarkAction, but blocks applying the mark when the selection
|
|
7
|
+
* boundaries would produce markdown that cannot round-trip (e.g. `word**,**`).
|
|
8
|
+
* Removing the mark (toggling off) is always allowed.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createMarkdownInlineMarkAction(markType: MarkType): ActionSpec;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defineActions = defineActions;
|
|
4
4
|
exports.createToggleMarkAction = createToggleMarkAction;
|
|
5
|
+
exports.createMarkdownInlineMarkAction = createMarkdownInlineMarkAction;
|
|
5
6
|
const prosemirror_commands_1 = require("prosemirror-commands");
|
|
6
7
|
const marks_1 = require("./marks.js");
|
|
7
8
|
function defineActions(actions) {
|
|
@@ -15,4 +16,27 @@ function createToggleMarkAction(markType) {
|
|
|
15
16
|
run: command,
|
|
16
17
|
};
|
|
17
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Like createToggleMarkAction, but blocks applying the mark when the selection
|
|
21
|
+
* boundaries would produce markdown that cannot round-trip (e.g. `word**,**`).
|
|
22
|
+
* Removing the mark (toggling off) is always allowed.
|
|
23
|
+
*/
|
|
24
|
+
function createMarkdownInlineMarkAction(markType) {
|
|
25
|
+
const base = createToggleMarkAction(markType);
|
|
26
|
+
return {
|
|
27
|
+
isActive: base.isActive,
|
|
28
|
+
isEnable: (state, dispatch, view, attrs) => {
|
|
29
|
+
const isBlocked = !(0, marks_1.isMarkActive)(state, markType) && !(0, marks_1.canApplyInlineMarkInMarkdown)(state);
|
|
30
|
+
if (isBlocked)
|
|
31
|
+
return false;
|
|
32
|
+
return base.isEnable(state, dispatch, view, attrs);
|
|
33
|
+
},
|
|
34
|
+
run: (state, dispatch, view, attrs) => {
|
|
35
|
+
const isBlocked = !(0, marks_1.isMarkActive)(state, markType) && !(0, marks_1.canApplyInlineMarkInMarkdown)(state);
|
|
36
|
+
if (isBlocked)
|
|
37
|
+
return;
|
|
38
|
+
base.run(state, dispatch, view, attrs);
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
18
42
|
//# sourceMappingURL=actions.js.map
|