Skip to content

Commit

Permalink
feat: 1.简化hover editor
Browse files Browse the repository at this point in the history
  • Loading branch information
JuckZ committed May 6, 2023
1 parent f9a7fd6 commit cb61326
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 218 deletions.
9 changes: 1 addition & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import {
WorkspaceContainer,
WorkspaceItem,
WorkspaceLeaf,
WorkspaceTabs,
WorkspaceWindow,
debounce,
normalizePath,
requireApiVersion,
} from 'obsidian';
import { ref } from 'vue';
import type { Database } from 'sql.js';
import { expandEmmetAbbreviation } from './utils/emmet';
import { usePomodoroStore, useSystemStore } from '@/stores';
import Replacer from '@/Replacer';
import Process from '@/process/Process';
import { checkInDefaultPath, checkInList, customSnippetPath } from '@/utils/constants';
Expand All @@ -50,7 +49,6 @@ import '@/main.scss';
import { NotifyUtil } from '@/utils/notify';
import { EditorUtil, EditorUtils } from '@/utils/editor';
import t from '@/i18n';
import { usePomodoroStore, useSystemStore } from '@/stores';
import { UpdateModal } from '@/ui/modal/UpdateModal';
import { HoverEditor, type HoverEditorParent } from '@/popover';

Expand Down Expand Up @@ -328,10 +326,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
this.announceUpdate();
}

get activePopovers(): HoverEditor[] {
return HoverEditor.activePopovers();
}

patchWorkspaceLeaf() {
this.register(
around(WorkspaceLeaf.prototype, {
Expand Down Expand Up @@ -770,7 +764,6 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
}

override async onunload(): Promise<void> {
HoverEditor.activePopovers().forEach(popover => popover.hide());
EditorUtil.unload();
NotifyUtil.onload();
toggleBlast('0');
Expand Down
187 changes: 3 additions & 184 deletions src/popover.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import {
Component,
type EphemeralState,
HoverPopover,
MarkdownView,
type MousePos,
PopoverState,
TFile,
View,
Workspace,
WorkspaceLeaf,
WorkspaceSplit,
WorkspaceTabs,
requireApiVersion,
resolveSubpath,
setIcon,
} from 'obsidian';
import HoverEditorPlugin from './main';
import { useSystemStore } from '@/stores';

import { isA } from '@/utils/misc';
import { genId } from '@/utils/common';
import { SETTINGS } from '@/settings';

Expand Down Expand Up @@ -47,8 +43,6 @@ export class HoverEditor extends nosuper(HoverPopover) {

shownPos: MousePos | null;

abortController? = this.addChild(new Component());

detaching = false;

opening = false;
Expand All @@ -69,10 +63,6 @@ export class HoverEditor extends nosuper(HoverPopover) {

id = genId(8);

onMouseIn: (event: MouseEvent) => void;

onMouseOut: (event: MouseEvent) => void;

originalPath: string; // these are kept to avoid adopting targets w/a different link
originalLinkText: string;

Expand All @@ -89,26 +79,6 @@ export class HoverEditor extends nosuper(HoverPopover) {
return windows;
}

static containerForDocument(doc: Document) {
if (doc !== document && app.workspace.floatingSplit)
for (const container of app.workspace.floatingSplit.children) {
if (container.doc === doc) return container;
}
return app.workspace.rootSplit;
}

static activePopovers() {
return this.activeWindows().flatMap(this.popoversForWindow);
}

static popoversForWindow(win?: Window) {
return (
Array.prototype.slice.call(win?.document?.body.querySelectorAll('.hover-popover') ?? []) as HTMLElement[]
)
.map(el => popovers.get(el)!)
.filter(he => he);
}

static forLeaf(leaf: WorkspaceLeaf | undefined) {
// leaf can be null such as when right clicking on an internal link
const el = leaf && document.body.matchParent.call(leaf.containerEl, '.hover-popover'); // work around matchParent race condition
Expand Down Expand Up @@ -140,30 +110,7 @@ export class HoverEditor extends nosuper(HoverPopover) {
this.parent = parent;
this.waitTime = waitTime;
this.state = PopoverState.Showing;
const { hoverEl } = this;
this.onMouseIn = this._onMouseIn.bind(this);
this.onMouseOut = this._onMouseOut.bind(this);
this.abortController!.load();

if (targetEl) {
targetEl.addEventListener('mouseover', this.onMouseIn);
targetEl.addEventListener('mouseout', this.onMouseOut);
}

hoverEl.addEventListener('mouseover', event => {
if (mouseIsOffTarget(event, hoverEl)) {
this.onHover = true;
this.onTarget = false;
this.transition();
}
});
hoverEl.addEventListener('mouseout', event => {
if (mouseIsOffTarget(event, hoverEl)) {
this.onHover = false;
this.onTarget = false;
this.transition();
}
});
this.timer = window.setTimeout(this.show.bind(this), waitTime);

// custom logic begin
Expand Down Expand Up @@ -191,7 +138,6 @@ export class HoverEditor extends nosuper(HoverPopover) {

attachLeaf(): WorkspaceLeaf {
this.rootSplit.getRoot = () => app.workspace[this.document === document ? 'rootSplit' : 'floatingSplit']!;
this.rootSplit.getContainer = () => HoverEditor.containerForDocument(this.document);
this.titleEl.insertAdjacentElement('afterend', this.rootSplit.containerEl);
const leaf = this.plugin.app.workspace.createLeafInParent(this.rootSplit, 0);
this.updateLeaves();
Expand Down Expand Up @@ -272,40 +218,9 @@ export class HoverEditor extends nosuper(HoverPopover) {
}

transition() {
if (this.shouldShow()) {
if (this.state === PopoverState.Hiding) {
this.state = PopoverState.Shown;
clearTimeout(this.timer);
}
} else {
if (this.state === PopoverState.Showing) {
this.hide();
} else {
if (this.state === PopoverState.Shown) {
this.state = PopoverState.Hiding;
this.timer = window.setTimeout(() => {
if (this.shouldShow()) {
this.transition();
} else {
this.hide();
}
}, this.waitTime);
}
}
}
}

_onMouseIn(event: MouseEvent) {
if (!(this.targetEl && !mouseIsOffTarget(event, this.targetEl))) {
this.onTarget = true;
this.transition();
}
}

_onMouseOut(event: MouseEvent) {
if (!(this.targetEl && !mouseIsOffTarget(event, this.targetEl))) {
this.onTarget = false;
this.transition();
if (this.state === PopoverState.Hiding) {
this.state = PopoverState.Shown;
clearTimeout(this.timer);
}
}

Expand Down Expand Up @@ -334,46 +249,6 @@ export class HoverEditor extends nosuper(HoverPopover) {

this.document.body.appendChild(this.hoverEl);
positionEl(rect, this.hoverEl, { gap: 10 }, this.document);

// custom hover editor logic
if (pos) {
// give positionEl a chance to adjust the position before we read the coords
setTimeout(() => {
const left = parseFloat(this.hoverEl.style.left);
const top = parseFloat(this.hoverEl.style.top);
this.hoverEl.setAttribute('data-x', String(left));
this.hoverEl.setAttribute('data-y', String(top));
}, 0);
}
}

shouldShow() {
return this.shouldShowSelf() || this.shouldShowChild();
}

shouldShowChild(): boolean {
return HoverEditor.activePopovers().some(popover => {
if (popover !== this && popover.targetEl && this.hoverEl.contains(popover.targetEl)) {
return popover.shouldShow();
}
return false;
});
}

shouldShowSelf() {
// Don't let obsidian show() us if we've already started closing
// return !this.detaching && (this.onTarget || this.onHover);
return (
!this.detaching &&
!!(
this.onTarget ||
this.onHover ||
this.state == PopoverState.Shown ||
this.document.querySelector(
`body>.modal-container, body > #he${this.id} ~ .menu, body > #he${this.id} ~ .suggestion-container`,
)
)
);
}

show() {
Expand Down Expand Up @@ -442,8 +317,6 @@ export class HoverEditor extends nosuper(HoverPopover) {
});
} else {
this.parent = null;
this.abortController?.unload();
this.abortController = undefined;
return this.nativeHide();
}
}
Expand All @@ -458,8 +331,6 @@ export class HoverEditor extends nosuper(HoverPopover) {
if (targetEl) {
const parent = targetEl.matchParent('.hover-popover');
if (parent) popovers.get(parent)?.transition();
targetEl.removeEventListener('mouseover', this.onMouseIn);
targetEl.removeEventListener('mouseout', this.onMouseOut);
}

this.onHide();
Expand Down Expand Up @@ -542,55 +413,3 @@ export function positionEl(
vresult: vresult,
};
}

/**
* "Get the position of an element relative to a parent element."
*
* The function takes two arguments:
*
* el: The element whose position we want to get.
* parentEl: The parent element to which we want to get the relative position.
* The function returns an object with two properties:
*
* top: The top position of the element relative to the parent element.
* left: The left position of the element relative to the parent element.
*
* The function works by looping through the offsetParent chain of the element and subtracting the
* scrollTop and scrollLeft values of the parent elements
* @param {HTMLElement | null} el - The element you want to get the relative position of.
* @param {HTMLElement | null} parentEl - The parent element that you want to get the relative position
* of.
* @returns An object with two properties, top and left.
*/
function getRelativePos(el: HTMLElement | null, parentEl: HTMLElement | null) {
let top = 0,
left = 0;
for (let nextParentEl = parentEl ? parentEl.offsetParent : null; el && el !== parentEl && el !== nextParentEl; ) {
top += el.offsetTop;
left += el.offsetLeft;
const offsetParent = el.offsetParent as HTMLElement | null;

for (let parent = el.parentElement; parent && parent !== offsetParent; ) {
top -= parent.scrollTop;
left -= parent.scrollLeft;
parent = parent.parentElement;
}

if (offsetParent && offsetParent !== parentEl && offsetParent !== nextParentEl) {
top -= offsetParent.scrollTop;
left -= offsetParent.scrollLeft;
}

el = offsetParent;
}

return {
top,
left,
};
}

function mouseIsOffTarget(event: MouseEvent, el: Element) {
const relatedTarget = event.relatedTarget;
return !(isA(relatedTarget, Node) && el.contains(relatedTarget));
}
2 changes: 0 additions & 2 deletions src/types/obsidian.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// https://github.com/nothingislost/obsidian-hover-editor/blob/master/src/types/obsidian.d.ts
import type { EditorView } from '@codemirror/view';
import { Plugin, SuggestModal, TFile, View, WorkspaceLeaf } from 'obsidian';
// import { HoverEditorParent } from '@/ui/modal/popover';

interface InternalPlugins {
switcher: QuickSwitcherPlugin;
Expand Down Expand Up @@ -198,7 +197,6 @@ declare module 'obsidian' {
type?: string;
}
interface HoverPopover {
// parent: HoverEditorParent | null;
targetEl: HTMLElement;
hoverEl: HTMLElement;
position(pos?: MousePos): void;
Expand Down
Loading

0 comments on commit cb61326

Please sign in to comment.