@gravity-ui/markdown-editor 15.3.0 → 15.3.1
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/extensions/additional/GPT/MarkupGpt/plugin.js +4 -1
- package/build/cjs/extensions/additional/GPT/MarkupGpt/plugin.js.map +1 -1
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js +41 -26
- package/build/cjs/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js.map +1 -1
- package/build/cjs/version.js +1 -1
- package/build/cjs/version.js.map +1 -1
- package/build/esm/extensions/additional/GPT/MarkupGpt/plugin.js +4 -1
- package/build/esm/extensions/additional/GPT/MarkupGpt/plugin.js.map +1 -1
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js +41 -26
- package/build/esm/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.js.map +1 -1
- package/build/esm/version.js +1 -1
- package/build/esm/version.js.map +1 -1
- package/package.json +1 -1
|
@@ -99,7 +99,10 @@ function mGptPlugin(gptProps) {
|
|
|
99
99
|
return (0, popup_1.renderPopup)(this._anchor, {
|
|
100
100
|
...gptProps,
|
|
101
101
|
disablePromptPresets: this.disablePromptPresets,
|
|
102
|
-
onClose: () =>
|
|
102
|
+
onClose: () => {
|
|
103
|
+
(0, commands_1.hideMarkupGpt)(this._view);
|
|
104
|
+
gptProps.onClose?.();
|
|
105
|
+
},
|
|
103
106
|
markup: this.markup,
|
|
104
107
|
onApplyResult: (changedMarkup) => this._onApplyResult(changedMarkup),
|
|
105
108
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"../../../../../../src","sources":["extensions/additional/GPT/MarkupGpt/plugin.ts"],"names":[],"mappings":";;AAsCA,gCAqIC;AA3KD,2CAA4C;AAG5C,iDAO6B;AAC7B,wDAAsD;AAEtD,+CAAoD;AACpD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AK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAClD,CAAC;QAED,kBAAkB;YACd,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACvB,CAAC;QAED,gBAAgB,CAAC,GAAW;YACxB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QACtB,CAAC;QAED,cAAc,CAAC,aAAqB;YAChC,MAAM,EAAC,IAAI,EAAE,EAAE,EAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC;YACzC,MAAM,OAAO,GAAG,CAAC,EAAC,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,aAAa,EAAC,CAAC,CAAC;YAE9D,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC;gBACxC,OAAO,EAAE,OAAO;gBAChB,OAAO,EAAE,CAAC,6BAAmB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;aAC1C,CAAC,CAAC;YAEH,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;QACrC,CAAC;KACJ,EACD;QACI,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK;KACtC,CACJ,CAAC;AACN,CAAC","sourcesContent":["import {WidgetType} from '@codemirror/view';\n\nimport type {GptWidgetOptions} from '../../..';\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n type PluginValue,\n ViewPlugin,\n type ViewUpdate,\n} from '../../../../cm/view';\nimport {ReactRendererFacet} from '../../../../markup';\nimport type {CommonAnswer} from '../ErrorScreen/types';\nimport {WIDGET_DECO_CLASS_NAME} from '../constants';\nimport {isEmptyGptPrompts} from '../utils';\n\nimport {hideMarkupGpt} from './commands';\nimport {HideMarkupGptEffect, ShowMarkupGptEffect} from './effects';\nimport {renderPopup} from './popup';\n\nclass SpanWidget extends WidgetType {\n private className = '';\n private textContent = '';\n\n constructor(className: string, textContent: string) {\n super();\n this.className = className;\n this.textContent = textContent;\n }\n\n toDOM() {\n const spanElem = document.createElement('span');\n spanElem.className = this.className;\n spanElem.textContent = this.textContent;\n return spanElem;\n }\n}\n\nexport function mGptPlugin<\n AnswerData extends CommonAnswer = CommonAnswer,\n PromptData extends unknown = unknown,\n>(gptProps: GptWidgetOptions<AnswerData, PromptData>) {\n return ViewPlugin.fromClass(\n class implements PluginValue {\n readonly _view: EditorView;\n readonly _renderItem;\n\n _anchor: Element | null = null;\n\n decos: DecorationSet = Decoration.none;\n disablePromptPresets = true;\n markup: string | null = null;\n\n selectedPosition = {\n from: 0,\n to: 0,\n };\n\n constructor(view: EditorView) {\n this._view = view;\n this._renderItem = view.state\n .facet(ReactRendererFacet)\n .createItem('gpt-in-markup-mode', () => this.renderPopup());\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged || update.selectionSet) {\n this.decos = Decoration.none;\n return;\n }\n\n this.decos = this.decos.map(update.changes);\n\n const {from, to} = update.state.selection.main;\n\n this.selectedPosition.from = from;\n this.selectedPosition.to = to;\n\n for (const tr of update.transactions) {\n for (const eff of tr.effects) {\n if (eff.is(ShowMarkupGptEffect)) {\n this._setSelectedText(this._getDecorationText(update, from, to));\n\n if (from === to) {\n this.disablePromptPresets = true;\n\n if (isEmptyGptPrompts(gptProps, true)) return;\n\n const decorationWidget = Decoration.widget({\n widget: new SpanWidget(WIDGET_DECO_CLASS_NAME, ' '),\n });\n\n this.decos = Decoration.set([decorationWidget.range(from)]);\n\n return;\n }\n\n this.disablePromptPresets = false;\n\n if (isEmptyGptPrompts(gptProps, false)) return;\n\n this.decos = Decoration.set([\n {\n from,\n to,\n value: Decoration.mark({class: WIDGET_DECO_CLASS_NAME}),\n },\n ]);\n }\n\n if (eff.is(HideMarkupGptEffect)) {\n this.decos = Decoration.none;\n }\n }\n }\n }\n\n docViewUpdate() {\n this._anchor = this._view.dom\n .getElementsByClassName(WIDGET_DECO_CLASS_NAME)\n .item(0);\n this._renderItem.rerender();\n }\n\n destroy() {\n this._clearSelectedText();\n this._renderItem.remove();\n }\n\n renderPopup() {\n if (!this._anchor || this.markup === null) {\n return null;\n }\n\n return renderPopup(this._anchor as HTMLElement, {\n ...gptProps,\n disablePromptPresets: this.disablePromptPresets,\n onClose: () => hideMarkupGpt(this._view),\n markup: this.markup,\n onApplyResult: (changedMarkup) => this._onApplyResult(changedMarkup),\n });\n }\n\n _getDecorationText(update: ViewUpdate, from: number, to: number): string {\n return update.state.doc.sliceString(from, to);\n }\n\n _clearSelectedText() {\n this.markup = null;\n }\n\n _setSelectedText(str: string) {\n this.markup = str;\n }\n\n _onApplyResult(changedMarkup: string) {\n const {from, to} = this.selectedPosition;\n const changes = [{from: from, to: to, insert: changedMarkup}];\n\n const transaction = this._view.state.update({\n changes: changes,\n effects: [HideMarkupGptEffect.of(null)],\n });\n\n this._view.dispatch(transaction);\n }\n },\n {\n decorations: (value) => value.decos,\n },\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"../../../../../../src","sources":["extensions/additional/GPT/MarkupGpt/plugin.ts"],"names":[],"mappings":";;AAsCA,gCAyIC;AA/KD,2CAA4C;AAG5C,iDAO6B;AAC7B,wDAAsD;AAEtD,+CAAoD;AACpD,uCAA2C;AAE3C,4CAAyC;AACzC,0CAAmE;AACnE,sCAAoC;AAEpC,MAAM,UAAW,SAAQ,iBAAU;IACvB,SAAS,GAAG,EAAE,CAAC;IACf,WAAW,GAAG,EAAE,CAAC;IAEzB,YAAY,SAAiB,EAAE,WAAmB;QAC9C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACnC,CAAC;IAED,KAAK;QACD,MAAM,QAAQ,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QAChD,QAAQ,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QACpC,QAAQ,CAAC,WAAW,G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EAAE,MAAM,EAAE,aAAa,EAAC,CAAC,CAAC;YAE9D,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC;gBACxC,OAAO,EAAE,OAAO;gBAChB,OAAO,EAAE,CAAC,6BAAmB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;aAC1C,CAAC,CAAC;YAEH,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;QACrC,CAAC;KACJ,EACD;QACI,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK;KACtC,CACJ,CAAC;AACN,CAAC","sourcesContent":["import {WidgetType} from '@codemirror/view';\n\nimport type {GptWidgetOptions} from '../../..';\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n type PluginValue,\n ViewPlugin,\n type ViewUpdate,\n} from '../../../../cm/view';\nimport {ReactRendererFacet} from '../../../../markup';\nimport type {CommonAnswer} from '../ErrorScreen/types';\nimport {WIDGET_DECO_CLASS_NAME} from '../constants';\nimport {isEmptyGptPrompts} from '../utils';\n\nimport {hideMarkupGpt} from './commands';\nimport {HideMarkupGptEffect, ShowMarkupGptEffect} from './effects';\nimport {renderPopup} from './popup';\n\nclass SpanWidget extends WidgetType {\n private className = '';\n private textContent = '';\n\n constructor(className: string, textContent: string) {\n super();\n this.className = className;\n this.textContent = textContent;\n }\n\n toDOM() {\n const spanElem = document.createElement('span');\n spanElem.className = this.className;\n spanElem.textContent = this.textContent;\n return spanElem;\n }\n}\n\nexport function mGptPlugin<\n AnswerData extends CommonAnswer = CommonAnswer,\n PromptData extends unknown = unknown,\n>(gptProps: GptWidgetOptions<AnswerData, PromptData>) {\n return ViewPlugin.fromClass(\n class implements PluginValue {\n readonly _view: EditorView;\n readonly _renderItem;\n\n _anchor: Element | null = null;\n\n decos: DecorationSet = Decoration.none;\n disablePromptPresets = true;\n markup: string | null = null;\n\n selectedPosition = {\n from: 0,\n to: 0,\n };\n\n constructor(view: EditorView) {\n this._view = view;\n this._renderItem = view.state\n .facet(ReactRendererFacet)\n .createItem('gpt-in-markup-mode', () => this.renderPopup());\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged || update.selectionSet) {\n this.decos = Decoration.none;\n return;\n }\n\n this.decos = this.decos.map(update.changes);\n\n const {from, to} = update.state.selection.main;\n\n this.selectedPosition.from = from;\n this.selectedPosition.to = to;\n\n for (const tr of update.transactions) {\n for (const eff of tr.effects) {\n if (eff.is(ShowMarkupGptEffect)) {\n this._setSelectedText(this._getDecorationText(update, from, to));\n\n if (from === to) {\n this.disablePromptPresets = true;\n\n if (isEmptyGptPrompts(gptProps, true)) return;\n\n const decorationWidget = Decoration.widget({\n widget: new SpanWidget(WIDGET_DECO_CLASS_NAME, ' '),\n });\n\n this.decos = Decoration.set([decorationWidget.range(from)]);\n\n return;\n }\n\n this.disablePromptPresets = false;\n\n if (isEmptyGptPrompts(gptProps, false)) return;\n\n this.decos = Decoration.set([\n {\n from,\n to,\n value: Decoration.mark({class: WIDGET_DECO_CLASS_NAME}),\n },\n ]);\n }\n\n if (eff.is(HideMarkupGptEffect)) {\n this.decos = Decoration.none;\n }\n }\n }\n }\n\n docViewUpdate() {\n this._anchor = this._view.dom\n .getElementsByClassName(WIDGET_DECO_CLASS_NAME)\n .item(0);\n this._renderItem.rerender();\n }\n\n destroy() {\n this._clearSelectedText();\n this._renderItem.remove();\n }\n\n renderPopup() {\n if (!this._anchor || this.markup === null) {\n return null;\n }\n\n return renderPopup(this._anchor as HTMLElement, {\n ...gptProps,\n disablePromptPresets: this.disablePromptPresets,\n onClose: () => {\n hideMarkupGpt(this._view);\n\n gptProps.onClose?.();\n },\n markup: this.markup,\n onApplyResult: (changedMarkup) => this._onApplyResult(changedMarkup),\n });\n }\n\n _getDecorationText(update: ViewUpdate, from: number, to: number): string {\n return update.state.doc.sliceString(from, to);\n }\n\n _clearSelectedText() {\n this.markup = null;\n }\n\n _setSelectedText(str: string) {\n this.markup = str;\n }\n\n _onApplyResult(changedMarkup: string) {\n const {from, to} = this.selectedPosition;\n const changes = [{from: from, to: to, insert: changedMarkup}];\n\n const transaction = this._view.state.update({\n changes: changes,\n effects: [HideMarkupGptEffect.of(null)],\n });\n\n this._view.dispatch(transaction);\n }\n },\n {\n decorations: (value) => value.decos,\n },\n );\n}\n"]}
|
|
@@ -14,41 +14,53 @@ const CodeBlockHighlight = (builder, opts) => {
|
|
|
14
14
|
let langs;
|
|
15
15
|
let lowlight;
|
|
16
16
|
let hljs;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
const loadModules = async () => {
|
|
18
|
+
try {
|
|
19
|
+
hljs = (await import('highlight.js/lib/core')).default;
|
|
20
|
+
const low = await import('lowlight');
|
|
21
|
+
const all = low.all;
|
|
22
|
+
const create = low.createLowlight;
|
|
23
|
+
langs = { ...all, ...opts.langs };
|
|
24
|
+
lowlight = create(langs);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
logger_1.globalLogger.info('Skip code_block highlighting');
|
|
29
|
+
builder.logger.log('Skip code_block highlighting');
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
30
33
|
builder.addPlugin(() => {
|
|
34
|
+
let modulesLoaded = false;
|
|
31
35
|
const selectItems = [];
|
|
32
36
|
const mapping = {};
|
|
33
|
-
for (const lang of Object.keys(langs)) {
|
|
34
|
-
const defs = langs[lang](hljs);
|
|
35
|
-
selectItems.push({
|
|
36
|
-
value: lang,
|
|
37
|
-
content: defs.name || (0, lodash_1.capitalize)(lang),
|
|
38
|
-
});
|
|
39
|
-
if (defs.aliases) {
|
|
40
|
-
for (const alias of defs.aliases) {
|
|
41
|
-
mapping[alias] = lang;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
37
|
// TODO: add TAB key handler
|
|
46
38
|
// TODO: Remove constant selection of block
|
|
47
39
|
return new prosemirror_state_1.Plugin({
|
|
48
40
|
key,
|
|
49
41
|
state: {
|
|
50
|
-
init: (_, state) =>
|
|
42
|
+
init: (_, state) => {
|
|
43
|
+
loadModules().then(() => {
|
|
44
|
+
modulesLoaded = true;
|
|
45
|
+
for (const lang of Object.keys(langs)) {
|
|
46
|
+
const defs = langs[lang](hljs);
|
|
47
|
+
selectItems.push({
|
|
48
|
+
value: lang,
|
|
49
|
+
content: defs.name || (0, lodash_1.capitalize)(lang),
|
|
50
|
+
});
|
|
51
|
+
if (defs.aliases) {
|
|
52
|
+
for (const alias of defs.aliases) {
|
|
53
|
+
mapping[alias] = lang;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return getDecorations(state.doc);
|
|
59
|
+
},
|
|
51
60
|
apply: (tr, decos, oldState, newState) => {
|
|
61
|
+
if (!modulesLoaded) {
|
|
62
|
+
return prosemirror_view_1.DecorationSet.empty;
|
|
63
|
+
}
|
|
52
64
|
if (tr.docChanged) {
|
|
53
65
|
const oldNodeName = oldState.selection.$head.parent.type.name;
|
|
54
66
|
const newNodeName = newState.selection.$head.parent.type.name;
|
|
@@ -123,6 +135,9 @@ const CodeBlockHighlight = (builder, opts) => {
|
|
|
123
135
|
});
|
|
124
136
|
function getDecorations(doc) {
|
|
125
137
|
const decos = [];
|
|
138
|
+
if (!lowlight) {
|
|
139
|
+
return prosemirror_view_1.DecorationSet.empty;
|
|
140
|
+
}
|
|
126
141
|
for (const { node, pos } of (0, prosemirror_utils_1.findChildrenByType)(doc, (0, CodeBlockSpecs_1.codeBlockType)(doc.type.schema), true)) {
|
|
127
142
|
let from = pos + 1;
|
|
128
143
|
let nodes;
|
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,uDAA2D;AAG3D,kDAA8C;AAC9C,kDAAgD;AAChD,+DAAsF;AAEtF,4DAAiE;AAYjE,MAAM,GAAG,GAAG,IAAI,6BAAS,CAAgB,sBAAsB,CAAC,CAAC;AAM1D,MAAM,kBAAkB,GAA6C,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IAC1F,IAAI,KAAoC,CAAC;IACzC,IAAI,QAAkB,CAAC;IACvB,IAAI,IAAiB,CAAC;IAEtB,IAAI,CAAC;QACD,IAAI,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;QACxC,MAAM,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;QAChC,MAAM,GAAG,GAAqB,GAAG,CAAC,GAAG,CAAC;QACtC,MAAM,MAAM,GAA0B,GAAG,CAAC,cAAc,CAAC;QACzD,KAAK,GAAG,EAAC,GAAG,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,EAAC,CAAC;QAChC,QAAQ,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,qBAAY,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;QAClD,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC;QACnD,OAAO;IACX,CAAC;IAED,OAAO,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,MAAM,WAAW,GAAqB,EAAE,CAAC;QACzC,MAAM,OAAO,GAA2B,EAAE,CAAC;QAC3C,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACpC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAA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C;YAC1B,OAAO,GAAG,OAAO,CAAC,MAAM,CAAE,IAAI,CAAC,UAAU,CAAC,SAAsB,IAAI,EAAE,CAAC,CAAC;YACxE,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO;YACH,IAAI,EAAE,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;YACvE,OAAO;SACV,CAAC;IACN,CAAC,CAAC;SACD,IAAI,EAAE,CAAC;AAChB,CAAC;AAED,SAAS,aAAa,CAAC,IAAU;IAC7B,mBAAmB;IACnB,OAAO,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ,CAAC;AACxE,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} from 'prosemirror-view';\n\nimport type {ExtensionAuto} from '../../../../core';\nimport {capitalize} from '../../../../lodash';\nimport {globalLogger} from '../../../../logger';\nimport {CodeBlockNodeAttr, codeBlockNodeName, codeBlockType} from '../CodeBlockSpecs';\n\nimport {codeLangSelectTooltipViewCreator} from './TooltipPlugin';\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 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 try {\n hljs = require('highlight.js/lib/core');\n const low = require('lowlight');\n const all: HighlightLangMap = low.all;\n const create: typeof createLowlight = low.createLowlight;\n langs = {...all, ...opts.langs};\n lowlight = create(langs);\n } catch (e) {\n globalLogger.info('Skip code_block highlighting');\n builder.logger.log('Skip code_block highlighting');\n return;\n }\n\n builder.addPlugin(() => {\n const selectItems: LangSelectItem[] = [];\n const mapping: Record<string, string> = {};\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 // TODO: add TAB key handler\n // TODO: Remove constant selection of block\n return new Plugin<DecorationSet>({\n key,\n state: {\n init: (_, state) => getDecorations(state.doc),\n apply: (tr, decos, oldState, newState) => {\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: (view) => codeLangSelectTooltipViewCreator(view, selectItems, mapping),\n props: {\n decorations: (state) => {\n return key.getState(state);\n },\n nodeViews: {\n [codeBlockNodeName]: (node) => {\n let prevLang = node.attrs[CodeBlockNodeAttr.Lang];\n\n const dom = document.createElement('pre');\n\n const contentDOM = document.createElement('code');\n contentDOM.classList.add('hljs');\n\n if (prevLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, prevLang);\n contentDOM.classList.add(prevLang);\n }\n\n dom.append(contentDOM);\n\n return {\n dom,\n contentDOM,\n update(newNode) {\n if (node.type !== newNode.type) return false;\n\n const newLang = newNode.attrs[CodeBlockNodeAttr.Lang];\n if (prevLang !== newLang) {\n contentDOM.className = 'hljs';\n if (newLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, newLang);\n contentDOM.classList.add(newLang);\n } else {\n dom.removeAttribute(CodeBlockNodeAttr.Lang);\n }\n prevLang = newLang;\n }\n\n return true;\n },\n };\n },\n },\n },\n });\n });\n\n function getDecorations(doc: Node) {\n const decos: Decoration[] = [];\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"]}
|
|
1
|
+
{"version":3,"file":"CodeBlockHighlight.js","sourceRoot":"../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.ts"],"names":[],"mappings":";;;AAKA,yDAAoD;AAEpD,oCAAoC;AACpC,yDAAqD;AACrD,uDAA2D;AAG3D,kDAA8C;AAC9C,kDAAgD;AAChD,+DAAsF;AAEtF,4DAAiE;AAYjE,MAAM,GAAG,GAAG,IAAI,6BAAS,CAAgB,sBAAsB,CAAC,CAAC;AAM1D,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,IAAI,CAAC;QAChB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,qBAAY,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;YAClD,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,8BAA8B,C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{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} from 'prosemirror-view';\n\nimport type {ExtensionAuto} from '../../../../core';\nimport {capitalize} from '../../../../lodash';\nimport {globalLogger} from '../../../../logger';\nimport {CodeBlockNodeAttr, codeBlockNodeName, codeBlockType} from '../CodeBlockSpecs';\n\nimport {codeLangSelectTooltipViewCreator} from './TooltipPlugin';\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 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 builder.addPlugin(() => {\n let modulesLoaded = false;\n\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(() => {\n modulesLoaded = true;\n\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 return getDecorations(state.doc);\n },\n apply: (tr, decos, oldState, newState) => {\n if (!modulesLoaded) {\n return DecorationSet.empty;\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: (view) => codeLangSelectTooltipViewCreator(view, selectItems, mapping),\n props: {\n decorations: (state) => {\n return key.getState(state);\n },\n nodeViews: {\n [codeBlockNodeName]: (node) => {\n let prevLang = node.attrs[CodeBlockNodeAttr.Lang];\n\n const dom = document.createElement('pre');\n\n const contentDOM = document.createElement('code');\n contentDOM.classList.add('hljs');\n\n if (prevLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, prevLang);\n contentDOM.classList.add(prevLang);\n }\n\n dom.append(contentDOM);\n\n return {\n dom,\n contentDOM,\n update(newNode) {\n if (node.type !== newNode.type) return false;\n\n const newLang = newNode.attrs[CodeBlockNodeAttr.Lang];\n if (prevLang !== newLang) {\n contentDOM.className = 'hljs';\n if (newLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, newLang);\n contentDOM.classList.add(newLang);\n } else {\n dom.removeAttribute(CodeBlockNodeAttr.Lang);\n }\n prevLang = newLang;\n }\n\n return true;\n },\n };\n },\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"]}
|
package/build/cjs/version.js
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.VERSION = void 0;
|
|
4
4
|
/** During build process, the current version will be injected here */
|
|
5
|
-
exports.VERSION = typeof '15.3.
|
|
5
|
+
exports.VERSION = typeof '15.3.1' !== 'undefined' ? '15.3.1' : 'unknown';
|
|
6
6
|
//# sourceMappingURL=version.js.map
|
package/build/cjs/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"../../src","sources":["version.ts"],"names":[],"mappings":";;;AAAA,sEAAsE;AACzD,QAAA,OAAO,GAAG,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC","sourcesContent":["/** During build process, the current version will be injected here */\nexport const VERSION = typeof '15.3.
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"../../src","sources":["version.ts"],"names":[],"mappings":";;;AAAA,sEAAsE;AACzD,QAAA,OAAO,GAAG,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC","sourcesContent":["/** During build process, the current version will be injected here */\nexport const VERSION = typeof '15.3.1' !== 'undefined' ? '15.3.1' : 'unknown';\n"]}
|
|
@@ -96,7 +96,10 @@ export function mGptPlugin(gptProps) {
|
|
|
96
96
|
return renderPopup(this._anchor, {
|
|
97
97
|
...gptProps,
|
|
98
98
|
disablePromptPresets: this.disablePromptPresets,
|
|
99
|
-
onClose: () =>
|
|
99
|
+
onClose: () => {
|
|
100
|
+
hideMarkupGpt(this._view);
|
|
101
|
+
gptProps.onClose?.();
|
|
102
|
+
},
|
|
100
103
|
markup: this.markup,
|
|
101
104
|
onApplyResult: (changedMarkup) => this._onApplyResult(changedMarkup),
|
|
102
105
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"../../../../../../src","sources":["extensions/additional/GPT/MarkupGpt/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAG5C,OAAO,EACH,UAAU,EAIV,UAAU,GAEb,+BAA4B;AAC7B,OAAO,EAAC,kBAAkB,EAAC,oCAA2B;AAEtD,OAAO,EAAC,sB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;aACvE,CAAC,CAAC;QACP,CAAC;QAED,kBAAkB,CAAC,MAAkB,EAAE,IAAY,EAAE,EAAU;YAC3D,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAClD,CAAC;QAED,kBAAkB;YACd,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACvB,CAAC;QAED,gBAAgB,CAAC,GAAW;YACxB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QACtB,CAAC;QAED,cAAc,CAAC,aAAqB;YAChC,MAAM,EAAC,IAAI,EAAE,EAAE,EAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC;YACzC,MAAM,OAAO,GAAG,CAAC,EAAC,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,aAAa,EAAC,CAAC,CAAC;YAE9D,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC;gBACxC,OAAO,EAAE,OAAO;gBAChB,OAAO,EAAE,CAAC,mBAAmB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;aAC1C,CAAC,CAAC;YAEH,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;QACrC,CAAC;KACJ,EACD;QACI,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK;KACtC,CACJ,CAAC;AACN,CAAC","sourcesContent":["import {WidgetType} from '@codemirror/view';\n\nimport type {GptWidgetOptions} from '../../..';\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n type PluginValue,\n ViewPlugin,\n type ViewUpdate,\n} from '../../../../cm/view';\nimport {ReactRendererFacet} from '../../../../markup';\nimport type {CommonAnswer} from '../ErrorScreen/types';\nimport {WIDGET_DECO_CLASS_NAME} from '../constants';\nimport {isEmptyGptPrompts} from '../utils';\n\nimport {hideMarkupGpt} from './commands';\nimport {HideMarkupGptEffect, ShowMarkupGptEffect} from './effects';\nimport {renderPopup} from './popup';\n\nclass SpanWidget extends WidgetType {\n private className = '';\n private textContent = '';\n\n constructor(className: string, textContent: string) {\n super();\n this.className = className;\n this.textContent = textContent;\n }\n\n toDOM() {\n const spanElem = document.createElement('span');\n spanElem.className = this.className;\n spanElem.textContent = this.textContent;\n return spanElem;\n }\n}\n\nexport function mGptPlugin<\n AnswerData extends CommonAnswer = CommonAnswer,\n PromptData extends unknown = unknown,\n>(gptProps: GptWidgetOptions<AnswerData, PromptData>) {\n return ViewPlugin.fromClass(\n class implements PluginValue {\n readonly _view: EditorView;\n readonly _renderItem;\n\n _anchor: Element | null = null;\n\n decos: DecorationSet = Decoration.none;\n disablePromptPresets = true;\n markup: string | null = null;\n\n selectedPosition = {\n from: 0,\n to: 0,\n };\n\n constructor(view: EditorView) {\n this._view = view;\n this._renderItem = view.state\n .facet(ReactRendererFacet)\n .createItem('gpt-in-markup-mode', () => this.renderPopup());\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged || update.selectionSet) {\n this.decos = Decoration.none;\n return;\n }\n\n this.decos = this.decos.map(update.changes);\n\n const {from, to} = update.state.selection.main;\n\n this.selectedPosition.from = from;\n this.selectedPosition.to = to;\n\n for (const tr of update.transactions) {\n for (const eff of tr.effects) {\n if (eff.is(ShowMarkupGptEffect)) {\n this._setSelectedText(this._getDecorationText(update, from, to));\n\n if (from === to) {\n this.disablePromptPresets = true;\n\n if (isEmptyGptPrompts(gptProps, true)) return;\n\n const decorationWidget = Decoration.widget({\n widget: new SpanWidget(WIDGET_DECO_CLASS_NAME, ' '),\n });\n\n this.decos = Decoration.set([decorationWidget.range(from)]);\n\n return;\n }\n\n this.disablePromptPresets = false;\n\n if (isEmptyGptPrompts(gptProps, false)) return;\n\n this.decos = Decoration.set([\n {\n from,\n to,\n value: Decoration.mark({class: WIDGET_DECO_CLASS_NAME}),\n },\n ]);\n }\n\n if (eff.is(HideMarkupGptEffect)) {\n this.decos = Decoration.none;\n }\n }\n }\n }\n\n docViewUpdate() {\n this._anchor = this._view.dom\n .getElementsByClassName(WIDGET_DECO_CLASS_NAME)\n .item(0);\n this._renderItem.rerender();\n }\n\n destroy() {\n this._clearSelectedText();\n this._renderItem.remove();\n }\n\n renderPopup() {\n if (!this._anchor || this.markup === null) {\n return null;\n }\n\n return renderPopup(this._anchor as HTMLElement, {\n ...gptProps,\n disablePromptPresets: this.disablePromptPresets,\n onClose: () => hideMarkupGpt(this._view),\n markup: this.markup,\n onApplyResult: (changedMarkup) => this._onApplyResult(changedMarkup),\n });\n }\n\n _getDecorationText(update: ViewUpdate, from: number, to: number): string {\n return update.state.doc.sliceString(from, to);\n }\n\n _clearSelectedText() {\n this.markup = null;\n }\n\n _setSelectedText(str: string) {\n this.markup = str;\n }\n\n _onApplyResult(changedMarkup: string) {\n const {from, to} = this.selectedPosition;\n const changes = [{from: from, to: to, insert: changedMarkup}];\n\n const transaction = this._view.state.update({\n changes: changes,\n effects: [HideMarkupGptEffect.of(null)],\n });\n\n this._view.dispatch(transaction);\n }\n },\n {\n decorations: (value) => value.decos,\n },\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"../../../../../../src","sources":["extensions/additional/GPT/MarkupGpt/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAG5C,OAAO,EACH,UAAU,EAIV,UAAU,GAEb,+BAA4B;AAC7B,OAAO,EAAC,kBAAkB,EAAC,oCAA2B;AAEtD,OAAO,EAAC,sBAAsB,EAAC,wBAAqB;AACpD,OAAO,EAAC,iBAAiB,EAAC,oBAAiB;AAE3C,OAAO,EAAC,aAAa,EAAC,sBAAmB;AACzC,OAAO,EAAC,mBAAmB,EAAE,mBAAmB,EAAC,qBAAkB;AACnE,OAAO,EAAC,WAAW,EAAC,mBAAgB;AAEpC,MAAM,UAAW,SAAQ,UAAU;IACvB,SAAS,GAAG,EAAE,CAAC;IACf,WAAW,GAAG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AAC,MAAM,GAAG,IAAI,CAAC;QACvB,CAAC;QAED,gBAAgB,CAAC,GAAW;YACxB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QACtB,CAAC;QAED,cAAc,CAAC,aAAqB;YAChC,MAAM,EAAC,IAAI,EAAE,EAAE,EAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC;YACzC,MAAM,OAAO,GAAG,CAAC,EAAC,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,aAAa,EAAC,CAAC,CAAC;YAE9D,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC;gBACxC,OAAO,EAAE,OAAO;gBAChB,OAAO,EAAE,CAAC,mBAAmB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;aAC1C,CAAC,CAAC;YAEH,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;QACrC,CAAC;KACJ,EACD;QACI,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK;KACtC,CACJ,CAAC;AACN,CAAC","sourcesContent":["import {WidgetType} from '@codemirror/view';\n\nimport type {GptWidgetOptions} from '../../..';\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n type PluginValue,\n ViewPlugin,\n type ViewUpdate,\n} from '../../../../cm/view';\nimport {ReactRendererFacet} from '../../../../markup';\nimport type {CommonAnswer} from '../ErrorScreen/types';\nimport {WIDGET_DECO_CLASS_NAME} from '../constants';\nimport {isEmptyGptPrompts} from '../utils';\n\nimport {hideMarkupGpt} from './commands';\nimport {HideMarkupGptEffect, ShowMarkupGptEffect} from './effects';\nimport {renderPopup} from './popup';\n\nclass SpanWidget extends WidgetType {\n private className = '';\n private textContent = '';\n\n constructor(className: string, textContent: string) {\n super();\n this.className = className;\n this.textContent = textContent;\n }\n\n toDOM() {\n const spanElem = document.createElement('span');\n spanElem.className = this.className;\n spanElem.textContent = this.textContent;\n return spanElem;\n }\n}\n\nexport function mGptPlugin<\n AnswerData extends CommonAnswer = CommonAnswer,\n PromptData extends unknown = unknown,\n>(gptProps: GptWidgetOptions<AnswerData, PromptData>) {\n return ViewPlugin.fromClass(\n class implements PluginValue {\n readonly _view: EditorView;\n readonly _renderItem;\n\n _anchor: Element | null = null;\n\n decos: DecorationSet = Decoration.none;\n disablePromptPresets = true;\n markup: string | null = null;\n\n selectedPosition = {\n from: 0,\n to: 0,\n };\n\n constructor(view: EditorView) {\n this._view = view;\n this._renderItem = view.state\n .facet(ReactRendererFacet)\n .createItem('gpt-in-markup-mode', () => this.renderPopup());\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged || update.selectionSet) {\n this.decos = Decoration.none;\n return;\n }\n\n this.decos = this.decos.map(update.changes);\n\n const {from, to} = update.state.selection.main;\n\n this.selectedPosition.from = from;\n this.selectedPosition.to = to;\n\n for (const tr of update.transactions) {\n for (const eff of tr.effects) {\n if (eff.is(ShowMarkupGptEffect)) {\n this._setSelectedText(this._getDecorationText(update, from, to));\n\n if (from === to) {\n this.disablePromptPresets = true;\n\n if (isEmptyGptPrompts(gptProps, true)) return;\n\n const decorationWidget = Decoration.widget({\n widget: new SpanWidget(WIDGET_DECO_CLASS_NAME, ' '),\n });\n\n this.decos = Decoration.set([decorationWidget.range(from)]);\n\n return;\n }\n\n this.disablePromptPresets = false;\n\n if (isEmptyGptPrompts(gptProps, false)) return;\n\n this.decos = Decoration.set([\n {\n from,\n to,\n value: Decoration.mark({class: WIDGET_DECO_CLASS_NAME}),\n },\n ]);\n }\n\n if (eff.is(HideMarkupGptEffect)) {\n this.decos = Decoration.none;\n }\n }\n }\n }\n\n docViewUpdate() {\n this._anchor = this._view.dom\n .getElementsByClassName(WIDGET_DECO_CLASS_NAME)\n .item(0);\n this._renderItem.rerender();\n }\n\n destroy() {\n this._clearSelectedText();\n this._renderItem.remove();\n }\n\n renderPopup() {\n if (!this._anchor || this.markup === null) {\n return null;\n }\n\n return renderPopup(this._anchor as HTMLElement, {\n ...gptProps,\n disablePromptPresets: this.disablePromptPresets,\n onClose: () => {\n hideMarkupGpt(this._view);\n\n gptProps.onClose?.();\n },\n markup: this.markup,\n onApplyResult: (changedMarkup) => this._onApplyResult(changedMarkup),\n });\n }\n\n _getDecorationText(update: ViewUpdate, from: number, to: number): string {\n return update.state.doc.sliceString(from, to);\n }\n\n _clearSelectedText() {\n this.markup = null;\n }\n\n _setSelectedText(str: string) {\n this.markup = str;\n }\n\n _onApplyResult(changedMarkup: string) {\n const {from, to} = this.selectedPosition;\n const changes = [{from: from, to: to, insert: changedMarkup}];\n\n const transaction = this._view.state.update({\n changes: changes,\n effects: [HideMarkupGptEffect.of(null)],\n });\n\n this._view.dispatch(transaction);\n }\n },\n {\n decorations: (value) => value.decos,\n },\n );\n}\n"]}
|
|
@@ -11,41 +11,53 @@ export const CodeBlockHighlight = (builder, opts) => {
|
|
|
11
11
|
let langs;
|
|
12
12
|
let lowlight;
|
|
13
13
|
let hljs;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
const loadModules = async () => {
|
|
15
|
+
try {
|
|
16
|
+
hljs = (await import('highlight.js/lib/core')).default;
|
|
17
|
+
const low = await import('lowlight');
|
|
18
|
+
const all = low.all;
|
|
19
|
+
const create = low.createLowlight;
|
|
20
|
+
langs = { ...all, ...opts.langs };
|
|
21
|
+
lowlight = create(langs);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
globalLogger.info('Skip code_block highlighting');
|
|
26
|
+
builder.logger.log('Skip code_block highlighting');
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
27
30
|
builder.addPlugin(() => {
|
|
31
|
+
let modulesLoaded = false;
|
|
28
32
|
const selectItems = [];
|
|
29
33
|
const mapping = {};
|
|
30
|
-
for (const lang of Object.keys(langs)) {
|
|
31
|
-
const defs = langs[lang](hljs);
|
|
32
|
-
selectItems.push({
|
|
33
|
-
value: lang,
|
|
34
|
-
content: defs.name || capitalize(lang),
|
|
35
|
-
});
|
|
36
|
-
if (defs.aliases) {
|
|
37
|
-
for (const alias of defs.aliases) {
|
|
38
|
-
mapping[alias] = lang;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
34
|
// TODO: add TAB key handler
|
|
43
35
|
// TODO: Remove constant selection of block
|
|
44
36
|
return new Plugin({
|
|
45
37
|
key,
|
|
46
38
|
state: {
|
|
47
|
-
init: (_, state) =>
|
|
39
|
+
init: (_, state) => {
|
|
40
|
+
loadModules().then(() => {
|
|
41
|
+
modulesLoaded = true;
|
|
42
|
+
for (const lang of Object.keys(langs)) {
|
|
43
|
+
const defs = langs[lang](hljs);
|
|
44
|
+
selectItems.push({
|
|
45
|
+
value: lang,
|
|
46
|
+
content: defs.name || capitalize(lang),
|
|
47
|
+
});
|
|
48
|
+
if (defs.aliases) {
|
|
49
|
+
for (const alias of defs.aliases) {
|
|
50
|
+
mapping[alias] = lang;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return getDecorations(state.doc);
|
|
56
|
+
},
|
|
48
57
|
apply: (tr, decos, oldState, newState) => {
|
|
58
|
+
if (!modulesLoaded) {
|
|
59
|
+
return DecorationSet.empty;
|
|
60
|
+
}
|
|
49
61
|
if (tr.docChanged) {
|
|
50
62
|
const oldNodeName = oldState.selection.$head.parent.type.name;
|
|
51
63
|
const newNodeName = newState.selection.$head.parent.type.name;
|
|
@@ -120,6 +132,9 @@ export const CodeBlockHighlight = (builder, opts) => {
|
|
|
120
132
|
});
|
|
121
133
|
function getDecorations(doc) {
|
|
122
134
|
const decos = [];
|
|
135
|
+
if (!lowlight) {
|
|
136
|
+
return DecorationSet.empty;
|
|
137
|
+
}
|
|
123
138
|
for (const { node, pos } of findChildrenByType(doc, codeBlockType(doc.type.schema), true)) {
|
|
124
139
|
let from = pos + 1;
|
|
125
140
|
let nodes;
|
package/build/esm/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,OAAO,EAAC,MAAM,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAEpD,oCAAoC;AACpC,OAAO,EAAC,kBAAkB,EAAC,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAC,UAAU,EAAE,aAAa,EAAC,MAAM,kBAAkB,CAAC;AAG3D,OAAO,EAAC,UAAU,EAAC,8BAA2B;AAC9C,OAAO,EAAC,YAAY,EAAC,8BAA2B;AAChD,OAAO,EAAC,iBAAiB,EAAE,iBAAiB,EAAE,aAAa,EAAC,mCAA0B;AAEtF,OAAO,EAAC,gCAAgC,EAAC,iCAAwB;AAYjE,MAAM,GAAG,GAAG,IAAI,SAAS,CAAgB,sBAAsB,CAAC,CAAC;AAMjE,MAAM,CAAC,MAAM,kBAAkB,GAA6C,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IAC1F,IAAI,KAAoC,CAAC;IACzC,IAAI,QAAkB,CAAC;IACvB,IAAI,IAAiB,CAAC;IAEtB,IAAI,CAAC;QACD,IAAI,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;QACxC,MAAM,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;QAChC,MAAM,GAAG,GAAqB,GAAG,CAAC,GAAG,CAAC;QACtC,MAAM,MAAM,GAA0B,GAAG,CAAC,cAAc,CAAC;QACzD,KAAK,GAAG,EAAC,GAAG,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,EAAC,CAAC;QAChC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3B,CAAC,CACL,CAAC;gBACN,CAAC;gBACD,IAAI,GAAG,EAAE,CAAC;YACd,CAAC;QACL,CAAC;QAED,OAAO,aAAa,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC;AACL,CAAC,CAAC;AAEF,SAAS,UAAU,CACf,KAAuB,EACvB,YAA+B,EAAE;IAEjC,OAAO,KAAK;SACP,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACV,IAAI,OAAO,GAAG,SAAS,CAAC;QACxB,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO,GAAG,OAAO,CAAC,MAAM,CAAE,IAAI,CAAC,UAAU,CAAC,SAAsB,IAAI,EAAE,CAAC,CAAC;YACxE,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO;YACH,IAAI,EAAE,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;YACvE,OAAO;SACV,CAAC;IACN,CAAC,CAAC;SACD,IAAI,EAAE,CAAC;AAChB,CAAC;AAED,SAAS,aAAa,CAAC,IAAU;IAC7B,mBAAmB;IACnB,OAAO,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ,CAAC;AACxE,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} from 'prosemirror-view';\n\nimport type {ExtensionAuto} from '../../../../core';\nimport {capitalize} from '../../../../lodash';\nimport {globalLogger} from '../../../../logger';\nimport {CodeBlockNodeAttr, codeBlockNodeName, codeBlockType} from '../CodeBlockSpecs';\n\nimport {codeLangSelectTooltipViewCreator} from './TooltipPlugin';\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 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 try {\n hljs = require('highlight.js/lib/core');\n const low = require('lowlight');\n const all: HighlightLangMap = low.all;\n const create: typeof createLowlight = low.createLowlight;\n langs = {...all, ...opts.langs};\n lowlight = create(langs);\n } catch (e) {\n globalLogger.info('Skip code_block highlighting');\n builder.logger.log('Skip code_block highlighting');\n return;\n }\n\n builder.addPlugin(() => {\n const selectItems: LangSelectItem[] = [];\n const mapping: Record<string, string> = {};\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 // TODO: add TAB key handler\n // TODO: Remove constant selection of block\n return new Plugin<DecorationSet>({\n key,\n state: {\n init: (_, state) => getDecorations(state.doc),\n apply: (tr, decos, oldState, newState) => {\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: (view) => codeLangSelectTooltipViewCreator(view, selectItems, mapping),\n props: {\n decorations: (state) => {\n return key.getState(state);\n },\n nodeViews: {\n [codeBlockNodeName]: (node) => {\n let prevLang = node.attrs[CodeBlockNodeAttr.Lang];\n\n const dom = document.createElement('pre');\n\n const contentDOM = document.createElement('code');\n contentDOM.classList.add('hljs');\n\n if (prevLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, prevLang);\n contentDOM.classList.add(prevLang);\n }\n\n dom.append(contentDOM);\n\n return {\n dom,\n contentDOM,\n update(newNode) {\n if (node.type !== newNode.type) return false;\n\n const newLang = newNode.attrs[CodeBlockNodeAttr.Lang];\n if (prevLang !== newLang) {\n contentDOM.className = 'hljs';\n if (newLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, newLang);\n contentDOM.classList.add(newLang);\n } else {\n dom.removeAttribute(CodeBlockNodeAttr.Lang);\n }\n prevLang = newLang;\n }\n\n return true;\n },\n };\n },\n },\n },\n });\n });\n\n function getDecorations(doc: Node) {\n const decos: Decoration[] = [];\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"]}
|
|
1
|
+
{"version":3,"file":"CodeBlockHighlight.js","sourceRoot":"../../../../../../src","sources":["extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,MAAM,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAEpD,oCAAoC;AACpC,OAAO,EAAC,kBAAkB,EAAC,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAC,UAAU,EAAE,aAAa,EAAC,MAAM,kBAAkB,CAAC;AAG3D,OAAO,EAAC,UAAU,EAAC,8BAA2B;AAC9C,OAAO,EAAC,YAAY,EAAC,8BAA2B;AAChD,OAAO,EAAC,iBAAiB,EAAE,iBAAiB,EAAE,aAAa,EAAC,mCAA0B;AAEtF,OAAO,EAAC,gCAAgC,EAAC,iCAAwB;AAYjE,MAAM,GAAG,GAAG,IAAI,SAAS,CAAgB,sBAAsB,CAAC,CAAC;AAMjE,MAAM,CAAC,MAAM,kBAAkB,GAA6C,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;IAC1F,IAAI,KAAoC,CAAC;I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AAO,CAAC,MAAM,CAAE,IAAI,CAAC,UAAU,CAAC,SAAsB,IAAI,EAAE,CAAC,CAAC;YACxE,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO;YACH,IAAI,EAAE,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;YACvE,OAAO;SACV,CAAC;IACN,CAAC,CAAC;SACD,IAAI,EAAE,CAAC;AAChB,CAAC;AAED,SAAS,aAAa,CAAC,IAAU;IAC7B,mBAAmB;IACnB,OAAO,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ,CAAC;AACxE,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} from 'prosemirror-view';\n\nimport type {ExtensionAuto} from '../../../../core';\nimport {capitalize} from '../../../../lodash';\nimport {globalLogger} from '../../../../logger';\nimport {CodeBlockNodeAttr, codeBlockNodeName, codeBlockType} from '../CodeBlockSpecs';\n\nimport {codeLangSelectTooltipViewCreator} from './TooltipPlugin';\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 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 builder.addPlugin(() => {\n let modulesLoaded = false;\n\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(() => {\n modulesLoaded = true;\n\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 return getDecorations(state.doc);\n },\n apply: (tr, decos, oldState, newState) => {\n if (!modulesLoaded) {\n return DecorationSet.empty;\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: (view) => codeLangSelectTooltipViewCreator(view, selectItems, mapping),\n props: {\n decorations: (state) => {\n return key.getState(state);\n },\n nodeViews: {\n [codeBlockNodeName]: (node) => {\n let prevLang = node.attrs[CodeBlockNodeAttr.Lang];\n\n const dom = document.createElement('pre');\n\n const contentDOM = document.createElement('code');\n contentDOM.classList.add('hljs');\n\n if (prevLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, prevLang);\n contentDOM.classList.add(prevLang);\n }\n\n dom.append(contentDOM);\n\n return {\n dom,\n contentDOM,\n update(newNode) {\n if (node.type !== newNode.type) return false;\n\n const newLang = newNode.attrs[CodeBlockNodeAttr.Lang];\n if (prevLang !== newLang) {\n contentDOM.className = 'hljs';\n if (newLang) {\n dom.setAttribute(CodeBlockNodeAttr.Lang, newLang);\n contentDOM.classList.add(newLang);\n } else {\n dom.removeAttribute(CodeBlockNodeAttr.Lang);\n }\n prevLang = newLang;\n }\n\n return true;\n },\n };\n },\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"]}
|
package/build/esm/version.js
CHANGED
package/build/esm/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"../../src","sources":["version.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC","sourcesContent":["/** During build process, the current version will be injected here */\nexport const VERSION = typeof '15.3.
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"../../src","sources":["version.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC","sourcesContent":["/** During build process, the current version will be injected here */\nexport const VERSION = typeof '15.3.1' !== 'undefined' ? '15.3.1' : 'unknown';\n"]}
|