Skip to content

Commit

Permalink
feat: 1.简化悬浮窗逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
JuckZ committed Oct 12, 2023
1 parent 8e0441f commit 6685295
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 124 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -141,7 +141,6 @@
"husky": "^8.0.3",
"jsdom": "^22.1.0",
"lint-staged": "^14.0.1",
"monkey-around": "^2.3.0",
"npm-run-all": "^4.1.5",
"obsidian": "^1.4.11",
"obsidian-dataview": "^0.5.56",
Expand Down
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 29 additions & 115 deletions src/main.ts
@@ -1,10 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import 'virtual:uno.css';
import { around } from 'monkey-around';
import {
App,
Editor,
type EphemeralState,
type MarkdownFileInfo,
MarkdownPreviewRenderer,
MarkdownView,
Expand All @@ -14,16 +12,14 @@ import {
TAbstractFile,
TFile,
Tasks,
type ViewState,
WorkspaceContainer,
WorkspaceItem,
WorkspaceLeaf,
WorkspaceWindow,
debounce,
normalizePath,
} from 'obsidian';
import { ref } from 'vue';
import type { Database } from 'sql.js';
import { HoverEditor, type HoverEditorParent } from '@/ui/popover';
import { expandEmmetAbbreviation } from '@/utils/emmet';
import { usePomodoroStore, useSystemStore } from '@/stores';
import Replacer from '@/Replacer';
Expand Down Expand Up @@ -52,7 +48,6 @@ import { NotifyUtil } from '@/utils/ntfy/notify';
import { EditorUtil, EditorUtils } from '@/utils/editor';
import t from '@/i18n';
import { UpdateModal } from '@/ui/modal/UpdateModal';
import { HoverEditor, type HoverEditorParent } from '@/popover';

// import { initWorker } from '@/web-worker';

Expand All @@ -66,7 +61,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
private pomodoroHistoryView: PomodoroHistoryView | null;
resizeFunction: () => any;
clickFunction: (evt: MouseEvent) => any;
activeLeafChangeFunction: (leaf: WorkspaceLeaf) => any;
fileOpenFunction: (file: TFile | null) => any;
layoutChangeFunction: () => any;
windowOpenFunction: (win: WorkspaceWindow, window: Window) => any;
Expand All @@ -75,9 +69,9 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
fileMenuFunction: (menu: Menu, file: TAbstractFile, source: string, leaf?: WorkspaceLeaf) => any;
editorMenuFunction: (menu: Menu, editor: Editor, info: MarkdownView | MarkdownFileInfo) => any;
editorChangeFunction: (editor: Editor, info: MarkdownView | MarkdownFileInfo) => any;
editorPasteFunction: (evt: ClipboardEvent, editor: Editor, info: MarkdownView) => any;
editorPasteFunction: (evt: ClipboardEvent, editor: Editor, info: MarkdownView | MarkdownFileInfo) => any;
editorDropFunction: (evt: DragEvent, editor: Editor, info: MarkdownView | MarkdownFileInfo) => any;
codemirrorFunction: (cm: CodeMirror.Editor, info: MarkdownView) => any;
codemirrorFunction: (cm: CodeMirror.Editor) => any;
quitFunction: (tasks: Tasks) => any;

vaultCreateFunction: (file: TAbstractFile) => any;
Expand Down Expand Up @@ -111,7 +105,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
this.editorChangeFunction = this.customizeEditorChange.bind(this);
this.editorPasteFunction = this.customizeEditorPaste.bind(this);
this.fileMenuFunction = this.customizeFileMenu.bind(this);
this.activeLeafChangeFunction = this.customizeActiveLeafChange.bind(this);
this.codemirrorFunction = this.customizeCodeMirror.bind(this);
this.vaultCreateFunction = this.customizeVaultCreate.bind(this);
this.vaultModifyFunction = this.customizeVaultModify.bind(this);
Expand Down Expand Up @@ -243,7 +236,11 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
onCodeMirrorChange(editor);
}

async customizeEditorPaste(evt: ClipboardEvent, editor: Editor, markdownView: MarkdownView): Promise<void> {
async customizeEditorPaste(
evt: ClipboardEvent,
editor: Editor,
info: MarkdownView | MarkdownFileInfo,
): Promise<void> {
// LoggerUtil.log('');
}

Expand All @@ -257,26 +254,7 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
});
}

async customizeActiveLeafChange(leaf: WorkspaceLeaf): Promise<void> {
HoverEditor.activePopover?.hoverEl.removeClass('is-active');
const hoverEditor = (HoverEditor.activePopover = leaf ? HoverEditor.forLeaf(leaf) : undefined);
if (hoverEditor && leaf) {
hoverEditor.hoverEl.addClass('is-active');
const titleEl = hoverEditor.hoverEl.querySelector('.popover-title');
if (!titleEl) return;
titleEl.textContent = leaf.view?.getDisplayText();
if (leaf?.view?.getViewType()) {
hoverEditor.hoverEl.setAttribute('data-active-view-type', leaf.view.getViewType());
}
if (leaf.view?.file?.path) {
titleEl.setAttribute('data-path', leaf.view.file.path);
} else {
titleEl.removeAttribute('data-path');
}
}
}

async customizeCodeMirror(cm: CodeMirror.Editor, view: MarkdownView): Promise<void> {
async customizeCodeMirror(cm: CodeMirror.Editor): Promise<void> {
// LoggerUtil.log('');
}

Expand All @@ -299,7 +277,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
override async onload(): Promise<void> {
await this.pluginDataIO.load();
LoggerUtil.init(SETTINGS.debugEnable);
this.patchWorkspaceLeaf();
DBUtil.init(this, () => {
usePomodoroStore().loadPomodoroData();
this.startPomodoroTask();
Expand Down Expand Up @@ -330,72 +307,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
this.announceUpdate();
}

patchWorkspaceLeaf() {
this.register(
around(WorkspaceLeaf.prototype, {
getRoot(old) {
return function () {
const top = old.call(this);
top.getRoot === this.getRoot ? top : top.getRoot();
// bugfix make.md冲突,不能使用ctrl+o打开文件 #bug
return top;
};
},
onResize(old) {
return function () {
this.view?.onResize();
};
},
setViewState(old) {
return async function (viewState: ViewState, eState?: unknown) {
const result = await old.call(this, viewState, eState);
// try and catch files that are opened from outside of the
// HoverEditor class so that we can update the popover title bar
try {
const he = HoverEditor.forLeaf(this);
if (he) {
if (viewState.type) he.hoverEl.setAttribute('data-active-view-type', viewState.type);
const titleEl = he.hoverEl.querySelector('.popover-title');
if (titleEl) {
titleEl.textContent = this.view?.getDisplayText();
if (this.view?.file?.path) {
titleEl.setAttribute('data-path', this.view.file.path);
} else {
titleEl.removeAttribute('data-path');
}
}
}
} catch {
// ignore
}
return result;
};
},
setEphemeralState(old) {
return function (state: EphemeralState) {
old.call(this, state);
if (state.focus && this.view?.getViewType() === 'empty') {
// Force empty (no-file) view to have focus so dialogs don't reset active pane
this.view.contentEl.tabIndex = -1;
this.view.contentEl.focus();
}
};
},
}),
);
this.register(
around(WorkspaceItem.prototype, {
getContainer(old) {
return function () {
if (!old) return; // 0.14.x doesn't have this
if (!this.parentSplit || this instanceof WorkspaceContainer) return old.call(this);
return this.parentSplit.getContainer();
};
},
}),
);
}

private startPomodoroTask() {
// 进来就找到ing任务,如果有,则开始interval任务,倒计时准备弹窗提醒
// 监听数据库变化事件,若变化,则刷新监听的任务
Expand Down Expand Up @@ -517,13 +428,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
});
}

spawnPopover(initiatingEl?: HTMLElement, onShowCallback?: () => unknown): WorkspaceLeaf {
const parent = this.app.workspace.activeLeaf as unknown as HoverEditorParent;
if (!initiatingEl) initiatingEl = parent.containerEl;
const hoverPopover = new HoverEditor(parent, initiatingEl!, this, undefined, onShowCallback);
return hoverPopover.attachLeaf();
}

private setupCommands() {
this.addCommand({
id: 'cut-line',
Expand Down Expand Up @@ -729,9 +633,10 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
// window.addEventListener('languagechange', () => {
// console.log('languagechange event detected!');
// });
this.registerDomEvent(activeDocument, 'selectionchange', async (e: MouseEvent) => {
EditorUtil.changeToolbarPopover(e, SETTINGS.toolbar);
});
const selectionChangeCallback = async (e: Event) => {
EditorUtil.changeToolbarPopover(e as MouseEvent, SETTINGS.toolbar);
};
this.registerDomEvent(activeDocument, 'selectionchange', selectionChangeCallback);
this.registerDomEvent(activeDocument, 'click', async (e: MouseEvent) => {
toggleMouseClickEffects(e, SETTINGS.clickString);
});
Expand All @@ -742,16 +647,26 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
});
};
const previewCursorCallback = (e: CustomEvent) => {
const newLeaf = this.spawnPopover(undefined, () => this.app.workspace.setActiveLeaf(newLeaf, false, true));
newLeaf.openLinkText(e.detail.cursorTarget.title, e.detail.cursorTarget.path);
const newLeaf = new HoverEditor(
this.app.workspace.activeLeaf as unknown as HoverEditorParent,
this.app.workspace.activeLeaf!.containerEl,
this,
300,
() => {
this.app.workspace.setActiveLeaf(newLeaf, false, true);
},
).attachLeaf();
newLeaf!.openLinkText(e.detail.cursorTarget.title, e.detail.cursorTarget.path);
};
window.addEventListener('mousemove', mouseMoveCallback);
this.register(() => window.removeEventListener('mousemove', mouseMoveCallback));
window.addEventListener(eventTypes.previewCursor, previewCursorCallback);
this.register(() => window.removeEventListener(eventTypes.previewCursor, previewCursorCallback));
window.addEventListener(eventTypes.previewCursor, previewCursorCallback as EventListener);
this.register(() =>
window.removeEventListener(eventTypes.previewCursor, previewCursorCallback as EventListener),
);
const openBrowserCallback = this.openBrowserHandle.bind(this);
window.addEventListener(eventTypes.openBrowser, openBrowserCallback);
this.register(() => window.removeEventListener(eventTypes.openBrowser, openBrowserCallback));
window.addEventListener(eventTypes.openBrowser, openBrowserCallback as EventListener);
this.register(() => window.removeEventListener(eventTypes.openBrowser, openBrowserCallback as EventListener));
[
this.app.workspace.on('dataview:refresh-views', this.maybeRefresh),
this.app.workspace.on('codemirror', this.codemirrorFunction),
Expand All @@ -761,7 +676,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
this.app.workspace.on('editor-paste', this.editorPasteFunction),
this.app.workspace.on('file-menu', this.fileMenuFunction),
this.app.workspace.on('editor-menu', this.editorMenuFunction),
this.app.workspace.on('active-leaf-change', this.activeLeafChangeFunction),
this.app.vault.on('create', this.vaultCreateFunction),
this.app.vault.on('modify', this.vaultModifyFunction),
this.app.vault.on('delete', this.vaultDeleteFunction),
Expand Down
8 changes: 7 additions & 1 deletion src/types/obsidian.d.ts
Expand Up @@ -79,6 +79,7 @@ declare module 'obsidian' {
viewRegistry: ViewRegistry;
openWithDefaultApp(path: string): void;
}

interface Workspace {
activeLeaf: WorkspaceLeaf;
floatingSplit: any;
Expand All @@ -89,6 +90,7 @@ declare module 'obsidian' {
interface WorkspaceTabs {
children: any;
}

interface ViewRegistry {
typeByExtension: Record<string, string>; // file extensions to view types
viewByType: Record<string, (leaf: WorkspaceLeaf) => View>; // file extensions to view types
Expand All @@ -103,7 +105,10 @@ declare module 'obsidian' {
containerEl: HTMLElement;
}
interface MarkdownView {
editMode: { cm: EditorView };
editMode: {
reinit(): unknown;
cm: EditorView;
};
}
interface MarkdownEditView {
editorEl: HTMLElement;
Expand All @@ -122,6 +127,7 @@ declare module 'obsidian' {
}
interface Workspace {
/** Sent to rendered dataview components to tell them to possibly refresh */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
on(name: 'dataview:refresh-views', callback: () => void, ctx?: any): EventRef;
recordHistory(leaf: WorkspaceLeaf, pushHistory: boolean): void;
iterateLeaves(
Expand Down

0 comments on commit 6685295

Please sign in to comment.