generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 36
/
embeddedMap.ts
133 lines (121 loc) · 4.78 KB
/
embeddedMap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { App, Notice, MarkdownPostProcessorContext } from 'obsidian';
import { PluginSettings } from 'src/settings';
import MapViewPlugin from 'src/main';
import { MapContainer, ViewSettings } from 'src/mapContainer';
import { MapState, getCodeBlock } from 'src/mapState';
import { getEditor, findOpenMapView } from 'src/utils';
export class EmbeddedMap {
public mapContainer: MapContainer;
private resizeObserver: ResizeObserver;
private settings: PluginSettings;
private app: App;
private markdownContext: MarkdownPostProcessorContext;
private parentEl: HTMLElement;
private customViewSettings: Partial<ViewSettings>;
constructor(
parentEl: HTMLElement,
ctx: MarkdownPostProcessorContext,
app: App,
settings: PluginSettings,
plugin: MapViewPlugin,
customViewSettings: Partial<ViewSettings> = null
) {
this.app = app;
this.settings = settings;
this.markdownContext = ctx;
this.parentEl = parentEl;
this.customViewSettings = customViewSettings;
const viewSettings: ViewSettings = {
showZoomButtons: true,
showMapControls: true,
showFilters: false,
showView: true,
showLinks: false,
viewTabType: 'mini',
showEmbeddedControls: true,
showPresets: false,
showSearch: false,
showRealTimeButton: false,
showLockButton: true,
showOpenButton: true,
autoZoom: true,
emptyFitRevertsToDefault: true,
...customViewSettings,
};
this.mapContainer = new MapContainer(
parentEl,
settings,
viewSettings,
plugin,
plugin.app
);
this.mapContainer.updateCodeBlockCallback = async () => {
this.updateCodeBlockWithState(this.mapContainer.state);
};
this.mapContainer.updateCodeBlockFromMapViewCallback = async () => {
const view = findOpenMapView(this.app);
if (!view) {
new Notice(
"Can't find another Map View instance to copy the state from"
);
return;
}
const state = view.mapContainer.state;
const success = this.updateCodeBlockWithState(state);
if (success)
new Notice('Successfully copied another open Map View');
};
}
async updateCodeBlockWithState(state: MapState) {
const sectionInfo = this.markdownContext.getSectionInfo(this.parentEl);
if (!sectionInfo) {
new Notice('Unable to find section info');
return false;
}
const editor = getEditor(this.app);
if (!editor) {
new Notice('Unable to find the current editor');
return false;
} else {
const lastLineLength = editor.getLine(sectionInfo.lineEnd).length;
const newBlock = getCodeBlock(state, this.customViewSettings);
editor.replaceRange(
newBlock,
{ line: sectionInfo.lineStart, ch: 0 },
{ line: sectionInfo.lineEnd, ch: lastLineLength }
);
// If the cursor was in an invisible location of the document (e.g. above the current viewport),
// calling replaceRange above would scroll back to it. In order to prevent such a jump, and ensure
// the map stays in view after the replacement, we move the cursor to be next to it
let freeLine;
if (sectionInfo.lineEnd != editor.lastLine())
freeLine = sectionInfo.lineEnd + 1;
else freeLine = sectionInfo.lineStart - 1;
editor.setCursor({ line: freeLine, ch: 0 });
return true;
}
}
async open(state: MapState) {
this.mapContainer.defaultState = state;
await this.mapContainer.onOpen();
this.resizeObserver = new ResizeObserver(() => {
this.onResize();
});
this.resizeObserver.observe(this.mapContainer.display.mapDiv);
await this.mapContainer.highLevelSetViewStateAsync(state);
if (this.mapContainer.display?.controls) {
this.mapContainer.display.controls.markStateAsSaved(state);
this.mapContainer.display.controls.updateSaveButtonVisibility();
}
if (state.embeddedHeight)
this.parentEl.style.height = `${state.embeddedHeight}px`;
this.settings.mapControls.viewDisplayed = false;
}
async setState(state: Partial<MapState>): Promise<MapState> {
return this.mapContainer.highLevelSetViewState(state);
}
onResize() {
if (this.mapContainer.display.mapDiv)
this.mapContainer.display.map.invalidateSize();
}
}