fix: fix lowlight decorations for vue 3
This commit is contained in:
@@ -16,10 +16,10 @@ import Text from '@tiptap/extension-text'
|
|||||||
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
|
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
|
||||||
import CodeBlockComponent from './CodeBlockComponent'
|
import CodeBlockComponent from './CodeBlockComponent'
|
||||||
|
|
||||||
// install all highlight.js languages
|
// load all highlight.js languages
|
||||||
import 'lowlight'
|
import lowlight from 'lowlight'
|
||||||
|
|
||||||
// or install specific languages only
|
// load specific languages only
|
||||||
// import lowlight from 'lowlight/lib/core'
|
// import lowlight from 'lowlight/lib/core'
|
||||||
// import javascript from 'highlight.js/lib/languages/javascript'
|
// import javascript from 'highlight.js/lib/languages/javascript'
|
||||||
// lowlight.registerLanguage('javascript', javascript)
|
// lowlight.registerLanguage('javascript', javascript)
|
||||||
@@ -41,11 +41,13 @@ export default {
|
|||||||
Document,
|
Document,
|
||||||
Paragraph,
|
Paragraph,
|
||||||
Text,
|
Text,
|
||||||
CodeBlockLowlight.extend({
|
CodeBlockLowlight
|
||||||
addNodeView() {
|
.extend({
|
||||||
return VueNodeViewRenderer(CodeBlockComponent)
|
addNodeView() {
|
||||||
},
|
return VueNodeViewRenderer(CodeBlockComponent)
|
||||||
}),
|
},
|
||||||
|
})
|
||||||
|
.configure({ lowlight }),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>
|
<p>
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ import Paragraph from '@tiptap/extension-paragraph'
|
|||||||
import Text from '@tiptap/extension-text'
|
import Text from '@tiptap/extension-text'
|
||||||
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
|
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
|
||||||
|
|
||||||
// install all highlight.js languages
|
// load all highlight.js languages
|
||||||
import 'lowlight'
|
import lowlight from 'lowlight'
|
||||||
|
|
||||||
// or install specific languages only
|
// load specific languages only
|
||||||
// import lowlight from 'lowlight/lib/core'
|
// import lowlight from 'lowlight/lib/core'
|
||||||
// import javascript from 'highlight.js/lib/languages/javascript'
|
// import javascript from 'highlight.js/lib/languages/javascript'
|
||||||
// lowlight.registerLanguage('javascript', javascript)
|
// lowlight.registerLanguage('javascript', javascript)
|
||||||
@@ -40,7 +40,9 @@ export default {
|
|||||||
Document,
|
Document,
|
||||||
Paragraph,
|
Paragraph,
|
||||||
Text,
|
Text,
|
||||||
CodeBlockLowlight,
|
CodeBlockLowlight.configure({
|
||||||
|
lowlight,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>
|
<p>
|
||||||
|
|||||||
@@ -1,11 +1,24 @@
|
|||||||
import CodeBlock from '@tiptap/extension-code-block'
|
import lowlight from 'lowlight/lib/core'
|
||||||
|
import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'
|
||||||
import { LowlightPlugin } from './lowlight-plugin'
|
import { LowlightPlugin } from './lowlight-plugin'
|
||||||
|
|
||||||
export const CodeBlockLowlight = CodeBlock.extend({
|
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
||||||
|
lowlight: any,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
||||||
|
defaultOptions: {
|
||||||
|
...CodeBlock.config.defaultOptions,
|
||||||
|
lowlight,
|
||||||
|
},
|
||||||
|
|
||||||
addProseMirrorPlugins() {
|
addProseMirrorPlugins() {
|
||||||
return [
|
return [
|
||||||
...this.parentConfig.addProseMirrorPlugins?.() || [],
|
...this.parentConfig.addProseMirrorPlugins?.() || [],
|
||||||
LowlightPlugin({ name: 'codeBlock' }),
|
LowlightPlugin({
|
||||||
|
name: 'codeBlock',
|
||||||
|
lowlight: this.options.lowlight,
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { Plugin, PluginKey } from 'prosemirror-state'
|
|||||||
import { Decoration, DecorationSet } from 'prosemirror-view'
|
import { Decoration, DecorationSet } from 'prosemirror-view'
|
||||||
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
||||||
import { findChildren } from '@tiptap/core'
|
import { findChildren } from '@tiptap/core'
|
||||||
import lowlight from 'lowlight/lib/core'
|
|
||||||
|
|
||||||
function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
|
function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
|
||||||
return nodes
|
return nodes
|
||||||
@@ -26,7 +25,7 @@ function parseNodes(nodes: any[], className: string[] = []): { text: string, cla
|
|||||||
.flat()
|
.flat()
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
|
function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: string, lowlight: any }) {
|
||||||
const decorations: Decoration[] = []
|
const decorations: Decoration[] = []
|
||||||
|
|
||||||
findChildren(doc, node => node.type.name === name)
|
findChildren(doc, node => node.type.name === name)
|
||||||
@@ -58,12 +57,12 @@ function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
|
|||||||
return DecorationSet.create(doc, decorations)
|
return DecorationSet.create(doc, decorations)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LowlightPlugin({ name }: { name: string }) {
|
export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any }) {
|
||||||
return new Plugin({
|
return new Plugin({
|
||||||
key: new PluginKey('lowlight'),
|
key: new PluginKey('lowlight'),
|
||||||
|
|
||||||
state: {
|
state: {
|
||||||
init: (_, { doc }) => getDecorations({ doc, name }),
|
init: (_, { doc }) => getDecorations({ doc, name, lowlight }),
|
||||||
apply: (transaction, decorationSet, oldState, newState) => {
|
apply: (transaction, decorationSet, oldState, newState) => {
|
||||||
const oldNodeName = oldState.selection.$head.parent.type.name
|
const oldNodeName = oldState.selection.$head.parent.type.name
|
||||||
const newNodeName = newState.selection.$head.parent.type.name
|
const newNodeName = newState.selection.$head.parent.type.name
|
||||||
@@ -95,7 +94,7 @@ export function LowlightPlugin({ name }: { name: string }) {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
return getDecorations({ doc: transaction.doc, name })
|
return getDecorations({ doc: transaction.doc, name, lowlight })
|
||||||
}
|
}
|
||||||
|
|
||||||
return decorationSet.map(transaction.mapping, transaction.doc)
|
return decorationSet.map(transaction.mapping, transaction.doc)
|
||||||
|
|||||||
Reference in New Issue
Block a user