mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-09-30 09:01:30 +08:00
fix(mermaid): bring back rendering
This commit is contained in:
parent
8952ff512f
commit
67ab91267d
@ -7,12 +7,8 @@ import { DEFAULT_GUTTER_SIZE } from "../../services/resizer.js";
|
|||||||
|
|
||||||
const TPL = `\
|
const TPL = `\
|
||||||
<div class="note-detail-split note-detail-printable split-horizontal">
|
<div class="note-detail-split note-detail-printable split-horizontal">
|
||||||
<div class="note-detail-split-preview">
|
<div class="note-detail-split-preview"></div>
|
||||||
Preview goes here.
|
<div class="note-detail-split-editor"></div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="note-detail-split-editor">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.note-detail-split {
|
.note-detail-split {
|
||||||
@ -49,11 +45,11 @@ const TPL = `\
|
|||||||
*
|
*
|
||||||
* - The two panes are resizeable via a split, on desktop.
|
* - The two panes are resizeable via a split, on desktop.
|
||||||
*/
|
*/
|
||||||
export default class SplitTypeEditor extends TypeWidget {
|
export default abstract class AbstractSplitTypeWidget extends TypeWidget {
|
||||||
|
|
||||||
private splitInstance?: Split.Instance;
|
private splitInstance?: Split.Instance;
|
||||||
|
|
||||||
private $preview!: JQuery<HTMLElement>;
|
protected $preview!: JQuery<HTMLElement>;
|
||||||
private $editor!: JQuery<HTMLElement>;
|
private $editor!: JQuery<HTMLElement>;
|
||||||
private editorTypeWidget: EditableCodeTypeWidget;
|
private editorTypeWidget: EditableCodeTypeWidget;
|
||||||
|
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
import type FNote from "../../entities/fnote.js";
|
||||||
|
import AbstractSplitTypeWidget from "./abstract_split_type_widget.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A specialization of `SplitTypeWidget` meant for note types that have a SVG preview.
|
||||||
|
*
|
||||||
|
* This adds the following functionality:
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export default abstract class AbstractSvgSplitTypeWidget extends AbstractSplitTypeWidget {
|
||||||
|
|
||||||
|
private $renderContainer!: JQuery<HTMLElement>;
|
||||||
|
|
||||||
|
doRender(): void {
|
||||||
|
super.doRender();
|
||||||
|
this.$renderContainer = $(`<div class="render-container"></div>`);
|
||||||
|
this.$preview.append(this.$renderContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
async doRefresh(note: FNote | null | undefined) {
|
||||||
|
super.doRefresh(note);
|
||||||
|
|
||||||
|
if (note) {
|
||||||
|
const blob = await note.getBlob();
|
||||||
|
const content = blob?.content || "";
|
||||||
|
console.log(content);
|
||||||
|
|
||||||
|
const svg = await this.renderSvg(content);
|
||||||
|
this.$renderContainer.html(svg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called upon when the SVG preview needs refreshing, such as when the editor has switched to a new note or the content has switched.
|
||||||
|
*
|
||||||
|
* The method must return a valid SVG string that will be automatically displayed in the preview.
|
||||||
|
*
|
||||||
|
* @param content the content of the note, in plain text.
|
||||||
|
*/
|
||||||
|
abstract renderSvg(content: string): Promise<string>;
|
||||||
|
|
||||||
|
}
|
@ -1,9 +1,45 @@
|
|||||||
import SplitTypeEditor from "./abstract_split_type_widget.js";
|
import library_loader from "../../services/library_loader.js";
|
||||||
|
import { postprocessMermaidSvg } from "../../services/mermaid.js";
|
||||||
|
import AbstractSvgSplitTypeWidget from "./abstract_svg_split_type_widget.js";
|
||||||
|
|
||||||
export class MermaidTypeWidget extends SplitTypeEditor {
|
let idCounter = 1;
|
||||||
|
|
||||||
|
export class MermaidTypeWidget extends AbstractSvgSplitTypeWidget {
|
||||||
|
|
||||||
static getType() {
|
static getType() {
|
||||||
return "mermaid";
|
return "mermaid";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async renderSvg(content: string) {
|
||||||
|
await library_loader.requireLibrary(library_loader.MERMAID);
|
||||||
|
|
||||||
|
mermaid.mermaidAPI.initialize({
|
||||||
|
startOnLoad: false,
|
||||||
|
...(getMermaidConfig() as any)
|
||||||
|
});
|
||||||
|
|
||||||
|
idCounter++;
|
||||||
|
const { svg } = await mermaid.mermaidAPI.render(`mermaid-graph-${idCounter}`, content);
|
||||||
|
return postprocessMermaidSvg(svg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getMermaidConfig(): MermaidConfig {
|
||||||
|
const documentStyle = window.getComputedStyle(document.documentElement);
|
||||||
|
const mermaidTheme = documentStyle.getPropertyValue("--mermaid-theme");
|
||||||
|
|
||||||
|
return {
|
||||||
|
theme: mermaidTheme.trim(),
|
||||||
|
securityLevel: "antiscript",
|
||||||
|
// TODO: Are all these options correct?
|
||||||
|
flow: { useMaxWidth: false },
|
||||||
|
sequence: { useMaxWidth: false },
|
||||||
|
gantt: { useMaxWidth: false },
|
||||||
|
class: { useMaxWidth: false },
|
||||||
|
state: { useMaxWidth: false },
|
||||||
|
pie: { useMaxWidth: true },
|
||||||
|
journey: { useMaxWidth: false },
|
||||||
|
git: { useMaxWidth: false }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user