Skip to content

Commit

Permalink
ObsidianZoomPlugin.refreshZoom API (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko authored May 4, 2023
1 parent 6784b03 commit 8797eb1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/ObsidianZoomPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ export default class ObsidianZoomPlugin extends Plugin {
const pos = cm.state.doc.line(line + 1).from;
this.zoomFeature.zoomIn(cm, pos);
}

public refreshZoom(editor: Editor) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cm: EditorView = (editor as any).cm;
this.zoomFeature.refreshZoom(cm);
}
}
27 changes: 27 additions & 0 deletions src/features/ZoomFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ export class ZoomFeature implements Feature {
this.zoomOutCallbacks.push(cb);
}

public refreshZoom(view: EditorView) {
const prevRange =
this.keepOnlyZoomedContentVisible.calculateVisibleContentRange(
view.state
);

if (!prevRange) {
return;
}

const newRange = this.calculateRangeForZooming.calculateRangeForZooming(
view.state,
prevRange.from
);

if (!newRange) {
return;
}

this.keepOnlyZoomedContentVisible.keepOnlyZoomedContentVisible(
view,
newRange.from,
newRange.to,
{ scrollIntoView: false }
);
}

public zoomIn(view: EditorView, pos: number) {
const l = this.logger.bind("ZoomFeature:zoomIn");
l("zooming in");
Expand Down
22 changes: 14 additions & 8 deletions src/logic/KeepOnlyZoomedContentVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ export class KeepOnlyZoomedContentVisible {
public keepOnlyZoomedContentVisible(
view: EditorView,
from: number,
to: number
to: number,
options: { scrollIntoView?: boolean } = {}
) {
const { scrollIntoView } = { ...{ scrollIntoView: true }, ...options };

const effect = zoomInEffect.of({ from, to });

this.logger.log(
Expand All @@ -94,13 +97,16 @@ export class KeepOnlyZoomedContentVisible {
view.dispatch({
effects: [effect],
});
view.dispatch({
effects: [
EditorView.scrollIntoView(view.state.selection.main, {
y: "start",
}),
],
});

if (scrollIntoView) {
view.dispatch({
effects: [
EditorView.scrollIntoView(view.state.selection.main, {
y: "start",
}),
],
});
}
}

public showAllContent(view: EditorView) {
Expand Down

0 comments on commit 8797eb1

Please sign in to comment.