Skip to content

Commit

Permalink
Public API
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Jan 19, 2022
1 parent 0d78219 commit 0a6e289
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
66 changes: 53 additions & 13 deletions src/ObsidianZoomPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Notice, Plugin, addIcon } from "obsidian";
import { Editor, Notice, Plugin, addIcon } from "obsidian";

import { EditorView } from "@codemirror/view";

import { Feature } from "./features/Feature";
import { HeaderNavigationFeature } from "./features/HeaderNavigationFeature";
Expand All @@ -21,6 +23,7 @@ addIcon(
);

export default class ObsidianZoomPlugin extends Plugin {
protected zoomFeature: ZoomFeature;
protected features: Feature[];

async onload() {
Expand All @@ -43,39 +46,39 @@ export default class ObsidianZoomPlugin extends Plugin {
const logger = new LoggerService(settings);

const settingsTabFeature = new SettingsTabFeature(this, settings);
const zoomFeature = new ZoomFeature(this, logger);
this.zoomFeature = new ZoomFeature(this, logger);
const limitSelectionFeature = new LimitSelectionFeature(
this,
logger,
zoomFeature
this.zoomFeature
);
const resetZoomWhenVisibleContentBoundariesViolatedFeature =
new ResetZoomWhenVisibleContentBoundariesViolatedFeature(
this,
logger,
zoomFeature,
zoomFeature
this.zoomFeature,
this.zoomFeature
);
const headerNavigationFeature = new HeaderNavigationFeature(
this,
logger,
zoomFeature,
zoomFeature,
zoomFeature,
zoomFeature,
zoomFeature,
zoomFeature
this.zoomFeature,
this.zoomFeature,
this.zoomFeature,
this.zoomFeature,
this.zoomFeature,
this.zoomFeature
);
const zoomOnClickFeature = new ZoomOnClickFeature(
this,
settings,
zoomFeature
this.zoomFeature
);
const listsStylesFeature = new ListsStylesFeature(settings);

this.features = [
settingsTabFeature,
zoomFeature,
this.zoomFeature,
limitSelectionFeature,
resetZoomWhenVisibleContentBoundariesViolatedFeature,
headerNavigationFeature,
Expand All @@ -99,6 +102,43 @@ export default class ObsidianZoomPlugin extends Plugin {
}
}

public getZoomRange(editor: Editor) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cm: EditorView = (editor as any).cm;
const range = this.zoomFeature.calculateVisibleContentRange(cm.state);

if (!range) {
return null;
}

const from = cm.state.doc.lineAt(range.from);
const to = cm.state.doc.lineAt(range.to);

return {
from: {
line: from.number - 1,
ch: range.from - from.from,
},
to: {
line: to.number - 1,
ch: range.to - to.from,
},
};
}

public zoomOut(editor: Editor) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cm: EditorView = (editor as any).cm;
this.zoomFeature.zoomOut(cm);
}

public zoomIn(editor: Editor, line: number) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cm: EditorView = (editor as any).cm;
const pos = cm.state.doc.line(line + 1).from;
this.zoomFeature.zoomIn(cm, pos);
}

private isLegacyEditorEnabled() {
const config: { legacyEditor: boolean } = {
legacyEditor: true,
Expand Down
9 changes: 2 additions & 7 deletions src/ObsidianZoomPluginWithTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { EditorSelection, StateField } from "@codemirror/state";
import { EditorView, runScopeHandlers } from "@codemirror/view";

import ObsidianZoomPlugin from "./ObsidianZoomPlugin";
import { ZoomFeature } from "./features/ZoomFeature";
import { zoomOutEffect } from "./logic/utils/effects";

const keysMap: { [key: string]: number } = {
Expand Down Expand Up @@ -204,13 +203,9 @@ export default class ObsidianZoomPluginWithTests extends ObsidianZoomPlugin {
getCurrentState(): IState {
const hidden: number[] = [];

const f = this.features.find(
(f): f is ZoomFeature => f instanceof ZoomFeature
const hiddenRanges = this.zoomFeature.calculateHiddenContentRanges(
this.editorView.state
);

const hiddenRanges = f
? f.calculateHiddenContentRanges(this.editorView.state)
: [];
for (const i of hiddenRanges) {
const lineFrom = this.editorView.state.doc.lineAt(i.from).number - 1;
const lineTo = this.editorView.state.doc.lineAt(i.to).number - 1;
Expand Down

0 comments on commit 0a6e289

Please sign in to comment.