1
1
import PlantumlPlugin from "./main" ;
2
2
import { Processor } from "./processor" ;
3
- import { MarkdownPostProcessorContext } from "obsidian" ;
3
+ import { MarkdownPostProcessorContext , moment } from "obsidian" ;
4
4
import * as plantuml from "plantuml-encoder" ;
5
5
import { insertAsciiImage , insertImageWithMap , insertSvgImage } from "./functions" ;
6
6
import { OutputType } from "./const" ;
7
+ import * as localforage from "localforage" ;
7
8
8
9
export class LocalProcessors implements Processor {
9
10
@@ -14,38 +15,52 @@ export class LocalProcessors implements Processor {
14
15
}
15
16
16
17
ascii = async ( source : string , el : HTMLElement , ctx : MarkdownPostProcessorContext ) => {
18
+ const encodedDiagram = plantuml . encode ( source ) ;
19
+ const item : string = await localforage . getItem ( 'ascii-' + encodedDiagram ) ;
20
+ if ( item ) {
21
+ insertAsciiImage ( el , item ) ;
22
+ await localforage . setItem ( 'ts-' + encodedDiagram , Date . now ( ) ) ;
23
+ return ;
24
+ }
25
+
17
26
const image = await this . generateLocalImage ( source , OutputType . ASCII , this . plugin . replacer . getPath ( ctx ) ) ;
18
27
insertAsciiImage ( el , image ) ;
28
+ await localforage . setItem ( 'ascii-' + encodedDiagram , image ) ;
29
+ await localforage . setItem ( 'ts-' + encodedDiagram , Date . now ( ) ) ;
19
30
}
20
31
21
32
png = async ( source : string , el : HTMLElement , ctx : MarkdownPostProcessorContext ) => {
22
33
const encodedDiagram = plantuml . encode ( source ) ;
23
-
24
- if ( localStorage . getItem ( encodedDiagram + "-png" ) ) {
25
- const image = localStorage . getItem ( encodedDiagram + "-png" ) ;
26
- const map = localStorage . getItem ( encodedDiagram + "- map" ) ;
27
- insertImageWithMap ( el , image , map , encodedDiagram ) ;
34
+ const item : string = await localforage . getItem ( 'png-' + encodedDiagram ) ;
35
+ if ( item ) {
36
+ const map : string = await localforage . getItem ( 'map-' + encodedDiagram ) ;
37
+ insertImageWithMap ( el , item , map , encodedDiagram ) ;
38
+ await localforage . setItem ( 'ts-' + encodedDiagram , Date . now ( ) ) ;
28
39
return ;
29
40
}
30
41
31
42
const path = this . plugin . replacer . getPath ( ctx ) ;
32
43
const image = await this . generateLocalImage ( source , OutputType . PNG , path ) ;
33
44
const map = await this . generateLocalMap ( source , path ) ;
34
45
35
- localStorage . setItem ( encodedDiagram + "-png" , image ) ;
36
- localStorage . setItem ( encodedDiagram + "-map" , map ) ;
46
+ await localforage . setItem ( 'png-' + encodedDiagram , image ) ;
47
+ await localforage . setItem ( 'map-' + encodedDiagram , map ) ;
48
+ await localforage . setItem ( 'ts-' + encodedDiagram , Date . now ( ) ) ;
37
49
38
50
insertImageWithMap ( el , image , map , encodedDiagram ) ;
39
51
}
40
52
41
53
svg = async ( source : string , el : HTMLElement , ctx : MarkdownPostProcessorContext ) => {
42
54
const encodedDiagram = plantuml . encode ( source ) ;
43
- if ( localStorage . getItem ( encodedDiagram + "-svg" ) ) {
44
- insertSvgImage ( el , localStorage . getItem ( encodedDiagram + "-svg" ) ) ;
55
+ const item : string = await localforage . getItem ( 'svg-' + encodedDiagram ) ;
56
+ if ( item ) {
57
+ insertSvgImage ( el , item ) ;
58
+ await localforage . setItem ( 'ts-' + encodedDiagram , Date . now ( ) ) ;
45
59
return ;
46
60
}
47
-
48
61
const image = await this . generateLocalImage ( source , OutputType . SVG , this . plugin . replacer . getPath ( ctx ) ) ;
62
+ await localforage . setItem ( 'svg-' + encodedDiagram , image ) ;
63
+ await localforage . setItem ( 'ts-' + encodedDiagram , Date . now ( ) ) ;
49
64
insertSvgImage ( el , image ) ;
50
65
}
51
66
0 commit comments