;
+ private zoomHandler?: () => void;
+ private zoomInstance?: SvgPanZoom.Instance;
doRender(): void {
super.doRender();
- this.$renderContainer = $(``);
+ this.$renderContainer = $(``)
+ .addClass("render-container")
+ .css("height", "100%");
this.$preview.append(this.$renderContainer);
}
@@ -42,9 +46,15 @@ export default abstract class AbstractSvgSplitTypeWidget extends AbstractSplitTy
if (this.note) {
const svg = await this.renderSvg(content);
this.$renderContainer.html(svg);
+ await this.#setupPanZoom();
}
}
+ cleanup(): void {
+ this.#cleanUpZoom();
+ super.cleanup();
+ }
+
/**
* Called upon when the SVG preview needs refreshing, such as when the editor has switched to a new note or the content has switched.
*
@@ -54,4 +64,58 @@ export default abstract class AbstractSvgSplitTypeWidget extends AbstractSplitTy
*/
abstract renderSvg(content: string): Promise;
+ async #setupPanZoom() {
+ // Clean up
+ let pan = null;
+ let zoom = null;
+ if (this.zoomInstance) {
+ // Store pan and zoom for same note, when the user is editing the note.
+ pan = this.zoomInstance.getPan();
+ zoom = this.zoomInstance.getZoom();
+ this.#cleanUpZoom();
+ }
+
+ const $svgEl = this.$renderContainer.find("svg");
+
+ // Fit the image to bounds
+ $svgEl.attr("width", "100%")
+ .attr("height", "100%")
+ .css("max-width", "100%");
+
+ if (!$svgEl.length) {
+ return;
+ }
+
+ const svgPanZoom = (await import("svg-pan-zoom")).default;
+ const zoomInstance = svgPanZoom($svgEl[0], {
+ zoomEnabled: true,
+ controlIconsEnabled: true
+ });
+
+ if (pan && zoom) {
+ // Restore the pan and zoom.
+ zoomInstance.zoom(zoom);
+ zoomInstance.pan(pan);
+ } else {
+ // New instance, reposition properly.
+ zoomInstance.center();
+ zoomInstance.fit();
+ }
+
+ this.zoomHandler = () => {
+ zoomInstance.resize();
+ zoomInstance.fit();
+ zoomInstance.center();
+ };
+ this.zoomInstance = zoomInstance;
+ $(window).on("resize", this.zoomHandler);
+ }
+
+ #cleanUpZoom() {
+ if (this.zoomInstance) {
+ this.zoomInstance.destroy();
+ this.zoomInstance = undefined;
+ }
+ }
+
}