Commit 2e83a511 authored by chris's avatar chris

fix highlight plugin

parent c265998e
export {
default as TrackChangePlugin
} from "./src/trackChanges/TrackChangePlugin";
export { default as TrackChangePlugin } from './src/trackChanges/TrackChangePlugin';
export { default as CommentPlugin } from "./src/comments/CommentPlugin";
export { default as WaxSelectionPlugin } from "./src/WaxSelectionPlugin";
export { default as CommentPlugin } from './src/comments/CommentPlugin';
export { default as WaxSelectionPlugin } from './src/WaxSelectionPlugin';
export { default as highlightPlugin } from './src/highlightPlugin';
......@@ -16,6 +16,8 @@
"wax-prosemirror-components": "^0.0.13",
"wax-prosemirror-core": "^0.0.13",
"wax-prosemirror-layouts": "^0.0.13",
"wax-prosemirror-utilities": "^0.0.13"
"wax-prosemirror-utilities": "^0.0.13",
"highlight.js": "^10.1.1",
"prosemirror-highlightjs": "^0.2.0"
}
}
import hljs from 'highlight.js/lib/core';
import { getHighlightDecorations } from 'prosemirror-highlightjs';
import { DecorationSet } from 'prosemirror-view';
import { Plugin } from 'prosemirror-state';
const highlightPlugin = (nodeTypes = ['code_block']) => {
return new Plugin({
state: {
init(_, instance) {
const content = getHighlightDecorations(
instance.doc,
hljs,
nodeTypes,
() => undefined,
);
return DecorationSet.create(instance.doc, content);
},
apply(tr, set, state) {
if (!tr.docChanged) {
return set.map(tr.mapping, tr.doc);
}
const {
selection: { $from, $to },
doc,
} = state;
let codeBlock = false;
doc.nodesBetween($from.pos, $to.pos, (node, from) => {
if (node.type.name === 'code_block') {
codeBlock = true;
}
});
if (codeBlock) {
const content = getHighlightDecorations(
tr.doc,
hljs,
nodeTypes,
() => undefined,
);
return DecorationSet.create(tr.doc, content);
}
return set.map(tr.mapping, tr.doc);
},
},
props: {
decorations(state) {
return this.getState(state);
},
},
});
};
export default highlightPlugin;
......@@ -10,12 +10,10 @@
"build": "BABEL_ENV=production rollup -c"
},
"dependencies": {
"highlight.js": "^10.1.1",
"inversify": "^5.0.1",
"inversify-inject-decorators": "^3.1.0",
"lodash": "^4.17.4",
"prosemirror-commands": "^1.1.3",
"prosemirror-highlightjs": "^0.2.0",
"prosemirror-history": "^1.1.3",
"prosemirror-keymap": "^1.1.3",
"prosemirror-schema-list": "^1.1.2",
......@@ -27,7 +25,8 @@
"wax-prosemirror-core": "^0.0.13",
"wax-prosemirror-layouts": "^0.0.13",
"wax-prosemirror-schema": "^0.0.13",
"wax-prosemirror-utilities": "^0.0.13"
"wax-prosemirror-utilities": "^0.0.13",
"wax-prosemirror-plugins": "^0.0.13"
},
"peerDependencies": {
"react": "^16.13.1",
......
import hljs from 'highlight.js/lib/core';
import { highlightPlugin } from 'prosemirror-highlightjs';
import { highlightPlugin } from 'wax-prosemirror-plugins';
import 'highlight.js/styles/github.css';
import { codeBlockNode } from 'wax-prosemirror-schema';
......@@ -8,7 +7,7 @@ import CodeBlockTool from './CodeBlockTool';
export default class CodeBlockService extends Service {
boot() {
this.app.PmPlugins.add('highlightPlugin', highlightPlugin(hljs));
this.app.PmPlugins.add('highlightPlugin', highlightPlugin());
}
register() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment