mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 11:35:12 +08:00
# Conflicts: # jsowell-ui/.env.development # jsowell-ui/.env.staging # jsowell-ui/bin/build-sit.bat # jsowell-ui/bin/build.bat # jsowell-ui/src/api/adapayMember/adapayMember.js # jsowell-ui/src/api/pile/merchant.js # jsowell-ui/src/router/index.js # jsowell-ui/src/views/financial/financeDetail.vue # jsowell-ui/src/views/financial/merchant.vue # jsowell-ui/src/views/homeIndex/homeIndex.vue # jsowell-ui/src/views/login.vue # jsowell-ui/src/views/pile/basic/detail.vue # jsowell-ui/src/views/pile/station/components/SiteInfo.vue # jsowell-ui/src/views/pile/station/detail.vue # jsowell-ui/src/views/pile/station/orderReport.vue
82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
import Parchment from 'parchment';
|
|
import Quill from '../core/quill';
|
|
import Module from '../core/module';
|
|
import CodeBlock from '../formats/code';
|
|
|
|
|
|
class SyntaxCodeBlock extends CodeBlock {
|
|
replaceWith(block) {
|
|
this.domNode.textContent = this.domNode.textContent;
|
|
this.attach();
|
|
super.replaceWith(block);
|
|
}
|
|
|
|
highlight(highlight) {
|
|
let text = this.domNode.textContent;
|
|
if (this.cachedText !== text) {
|
|
if (text.trim().length > 0 || this.cachedText == null) {
|
|
this.domNode.innerHTML = highlight(text);
|
|
this.domNode.normalize();
|
|
this.attach();
|
|
}
|
|
this.cachedText = text;
|
|
}
|
|
}
|
|
}
|
|
SyntaxCodeBlock.className = 'ql-syntax';
|
|
|
|
|
|
let CodeToken = new Parchment.Attributor.Class('token', 'hljs', {
|
|
scope: Parchment.Scope.INLINE
|
|
});
|
|
|
|
|
|
class Syntax extends Module {
|
|
static register() {
|
|
Quill.register(CodeToken, true);
|
|
Quill.register(SyntaxCodeBlock, true);
|
|
}
|
|
|
|
constructor(quill, options) {
|
|
super(quill, options);
|
|
if (typeof this.options.highlight !== 'function') {
|
|
throw new Error('Syntax module requires highlight.js. Please include the library on the page before Quill.');
|
|
}
|
|
let timer = null;
|
|
this.quill.on(Quill.events.SCROLL_OPTIMIZE, () => {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(() => {
|
|
this.highlight();
|
|
timer = null;
|
|
}, this.options.interval);
|
|
});
|
|
this.highlight();
|
|
}
|
|
|
|
highlight() {
|
|
if (this.quill.selection.composing) return;
|
|
this.quill.update(Quill.sources.USER);
|
|
let range = this.quill.getSelection();
|
|
this.quill.scroll.descendants(SyntaxCodeBlock).forEach((code) => {
|
|
code.highlight(this.options.highlight);
|
|
});
|
|
this.quill.update(Quill.sources.SILENT);
|
|
if (range != null) {
|
|
this.quill.setSelection(range, Quill.sources.SILENT);
|
|
}
|
|
}
|
|
}
|
|
Syntax.DEFAULTS = {
|
|
highlight: (function() {
|
|
if (window.hljs == null) return null;
|
|
return function(text) {
|
|
let result = window.hljs.highlightAuto(text);
|
|
return result.value;
|
|
};
|
|
})(),
|
|
interval: 1000
|
|
};
|
|
|
|
|
|
export { SyntaxCodeBlock as CodeBlock, CodeToken, Syntax as default};
|