Skip to content

Commit

Permalink
- update plugin for Obsidian 0.15.0
Browse files Browse the repository at this point in the history
- add caching to local processor
  • Loading branch information
joethei committed Jun 15, 2022
1 parent 69f47c8 commit dd91661
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 25 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "obsidian-plantuml",
"name": "PlantUML",
"version": "1.6.5",
"minAppVersion": "0.13.0",
"version": "1.6.6",
"minAppVersion": "0.15.0",
"description": "Render PlantUML Diagrams",
"author": "Johannes Theiner",
"authorUrl": "https://github.com/joethei/",
Expand Down
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-plantuml",
"version": "1.6.5",
"version": "1.6.6",
"description": "PlantUML rendering for Obsidian.md",
"main": "main.js",
"scripts": {
Expand All @@ -22,17 +22,15 @@
"esbuild": "0.14.2",
"eslint": "^7.22.0",
"node": "^17.2.0",
"obsidian": "^0.13.21",
"obsidian": "0.15.0",
"plantuml-encoder": "^1.4.0",
"tslib": "^2.3.1",
"typescript": "^4.5.3",
"uuid": "^8.3.2",
"compare-versions": "^4.1.3",
"@codemirror/gutter": "^0.19.9",
"@codemirror/search": "^0.19.8",
"@codemirror/view": "^0.19.44",
"@codemirror/commands": "^0.19.8",
"@codemirror/history": "^0.19.2",
"@codemirror/stream-parser": "https://github.com/lishid/stream-parser"
"@codemirror/search": "6.0.0",
"@codemirror/view": "6.0.0",
"@codemirror/commands": "6.0.0",
"@codemirror/language": "https://github.com/lishid/cm-language"
}
}
17 changes: 11 additions & 6 deletions src/PumlView.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import {debounce, Debouncer, Keymap, setIcon, TextFileView, ViewStateResult, WorkspaceLeaf} from "obsidian";
import PlantumlPlugin from "./main";
import {drawSelection, EditorView, highlightActiveLine, keymap} from "@codemirror/view";
import {
drawSelection,
EditorView,
highlightActiveLine,
highlightActiveLineGutter,
keymap,
lineNumbers
} from "@codemirror/view";
import {Annotation, EditorState, Extension, Transaction} from "@codemirror/state";
import {highlightActiveLineGutter, lineNumbers} from "@codemirror/gutter";
import {highlightSelectionMatches, search} from "@codemirror/search";
import {history} from "@codemirror/history";
import {defaultKeymap, indentWithTab} from "@codemirror/commands";
import {defaultKeymap, history, indentWithTab} from "@codemirror/commands";

export const VIEW_TYPE = "plantuml";

Expand Down Expand Up @@ -49,10 +54,10 @@ export class PumlView extends TextFileView {
keymap.of([...defaultKeymap, indentWithTab]),
history(),
search(),
EditorView.updateListener.of(v => {
EditorView.updateListener.of(async v => {
if(v.docChanged) {
this.requestSave();
this.renderPreview();
await this.renderPreview();
}
})
]
Expand Down
1 change: 1 addition & 0 deletions src/debouncedProcessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class DebouncedProcessors implements Processor {
const uuid = uuidv4();
el.dataset.plantumlDebouce = uuid;
this.debounceMap.set(uuid, func);

source = this.plugin.replacer.replaceNonBreakingSpaces(source);
source = this.plugin.replacer.replaceLinks(source, this.plugin.replacer.getPath(ctx), filetype);
source = this.plugin.settings.header + "\r\n" + source;
Expand Down
12 changes: 5 additions & 7 deletions src/decorations/EmbedDecoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {debounce} from "obsidian";
import {EditorView, Decoration, DecorationSet, ViewUpdate, ViewPlugin, WidgetType} from "@codemirror/view";
import {StateField, StateEffect, StateEffectType} from "@codemirror/state";
import {Range} from "@codemirror/rangeset";
import {syntaxTree} from "@codemirror/language";
import {tokenClassNodeProp} from "@codemirror/stream-parser";
import {syntaxTree, tokenClassNodeProp} from "@codemirror/language";
import PlantumlPlugin from "../main";
import {SyntaxNodeRef} from "@lezer/common";

//based on: https://gist.github.com/nothingislost/faa89aa723254883d37f45fd16162337

Expand Down Expand Up @@ -76,13 +76,11 @@ function buildViewPlugin(plugin: PlantumlPlugin) {

buildAsyncDecorations(view: EditorView) {
const targetElements: TokenSpec[] = [];
for (const {from, to} of view.visibleRanges) {
for (const {from} of view.visibleRanges) {
const tree = syntaxTree(view.state);
tree.iterate({
from,
to,
enter: (type, from, to) => {
const tokenProps = type.prop(tokenClassNodeProp);
enter: (node: SyntaxNodeRef) => {
const tokenProps = node.type.prop<string>(tokenClassNodeProp);
if (tokenProps) {
const props = new Set(tokenProps.split(" "));
const isEmbed = props.has("formatting-embed");
Expand Down
23 changes: 22 additions & 1 deletion src/localProcessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,31 @@ export class LocalProcessors implements Processor {

png = async(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const encodedDiagram = plantuml.encode(source);

if(localStorage.getItem(encodedDiagram + "-png")) {
const image = localStorage.getItem(encodedDiagram + "-png");
const map = localStorage.getItem(encodedDiagram + "-map");
insertImageWithMap(el,image, map, encodedDiagram);
return;
}

const path = this.plugin.replacer.getPath(ctx);
const image = await this.generateLocalImage(source, OutputType.PNG, path);
const map = await this.generateLocalMap(source, path);

localStorage.setItem(encodedDiagram + "-png", image);
localStorage.setItem(encodedDiagram + "-map", map);

insertImageWithMap(el, image, map, encodedDiagram);
}

svg = async(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const encodedDiagram = plantuml.encode(source);
if(localStorage.getItem(encodedDiagram + "-svg")) {
insertSvgImage(el, localStorage.getItem(encodedDiagram + "-svg"));
return;
}

const image = await this.generateLocalImage(source, OutputType.SVG, this.plugin.replacer.getPath(ctx));
insertSvgImage(el, image);
}
Expand Down Expand Up @@ -98,6 +116,9 @@ export class LocalProcessors implements Processor {
child.on("error", reject);

child.on("close", (code: any) => {
if(stdout === undefined) {
return;
}
if (code === 0) {
if (type === OutputType.PNG) {
const buf = new Buffer(stdout, 'binary');
Expand All @@ -107,7 +128,7 @@ export class LocalProcessors implements Processor {
resolve(stdout);
return;
} else if (code === 1) {
console.log(stdout);
console.error(stdout);
reject(new Error(stderr));
} else {
if (type === OutputType.PNG) {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"1.6.2": "0.13.0",
"1.6.3": "0.13.0",
"1.6.4": "0.13.0",
"1.6.5": "0.13.0"
"1.6.5": "0.13.0",
"1.6.6": "0.15.0"
}

0 comments on commit dd91661

Please sign in to comment.