Skip to content

Commit

Permalink
add: hover to show note link preview
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Oct 11, 2024
1 parent ef1bbe1 commit 13e172a
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
"typescript.tsdk": "node_modules/typescript/lib"
}
22 changes: 17 additions & 5 deletions addon/chrome/content/preferences.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@
native="true"
preference="__prefsPrefix__.workspace.outline.keepLinks"
/>
<checkbox
data-l10n-id="editor-noteLinkPreview"
native="true"
preference="__prefsPrefix__.editor.noteLinkPreview"
/>
<hbox align="center">
<html:label
data-l10n-id="editor-noteLinkPreviewType"
for="__addonRef__-editor-noteLinkPreviewType"
></html:label>
<radiogroup
id="__addonRef__-editor-noteLinkPreviewType"
preference="__prefsPrefix__.editor.noteLinkPreviewType"
orient="horizontal"
oncommand="Zotero.__addonInstance__.api.utils.requireRestart();"
>
<radio data-l10n-id="editor-noteLinkPreview-hover" value="hover" />
<radio data-l10n-id="editor-noteLinkPreview-ctrl" value="ctrl" />
<radio data-l10n-id="editor-noteLinkPreview-disable" value="disable" />
</radiogroup>
</hbox>
</groupbox>
<groupbox>
<label><html:h2 data-l10n-id="sync-title"></html:h2></label>
Expand All @@ -59,6 +70,7 @@
placeholder="-1 for disable"
id="__addonRef__-sync-period"
preference="__prefsPrefix__.syncPeriodSeconds"
onchange="Zotero.__addonInstance__.api.utils.requireRestart();"
></html:input>
</hbox>
<hbox align="center">
Expand Down
18 changes: 15 additions & 3 deletions addon/chrome/content/templateEditor.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,25 @@
color: var(--fill-primary);
}
.snippet.syntax {
background-color: color-mix(in srgb, var(--accent-yellow) 50%, transparent 50%);
background-color: color-mix(
in srgb,
var(--accent-yellow) 50%,
transparent 50%
);
}
.snippet.expression {
background-color: color-mix(in srgb, var(--accent-green) 50%, transparent 50%);
background-color: color-mix(
in srgb,
var(--accent-green) 50%,
transparent 50%
);
}
.snippet.variable {
background-color: color-mix(in srgb, var(--accent-azure) 50%, transparent 50%);
background-color: color-mix(
in srgb,
var(--accent-azure) 50%,
transparent 50%
);
}
.snippet:hover {
background-color: var(--fill-quinary);
Expand Down
12 changes: 10 additions & 2 deletions addon/locale/en-US/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ editor-title = Note Editor
editor-expandLevel-label = Outline expand to heading level
editor-keepLinks =
.label = Show note links in outline
editor-noteLinkPreview =
.label = Show note link preview on hover
editor-noteLinkPreviewType = Show preview for note link when:
editor-noteLinkPreview-hover =
.label = Hover
editor-noteLinkPreview-ctrl =
.label = Press { PLATFORM() ->
[macos]
*[other] Ctrl
}
editor-noteLinkPreview-disable =
.label = Never
sync-title = Sync
sync-period-label = Auto-sync period (seconds)
Expand Down
12 changes: 10 additions & 2 deletions addon/locale/it-IT/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ editor-title = Editor delle note
editor-expandLevel-label = Espansione dello schema al livello delle intestazioni
editor-keepLinks =
.label = Mostra i collegamenti delle note nello schema
editor-noteLinkPreview =
.label = Mostra l'anteprima del link alla nota al passaggio del mouse
editor-noteLinkPreviewType = Show preview for note link when:
editor-noteLinkPreview-hover =
.label = Hover
editor-noteLinkPreview-ctrl =
.label = Press { PLATFORM() ->
[macos]
*[other] Ctrl
}
editor-noteLinkPreview-disable =
.label = Never
sync-title = Sincronizzazione
sync-period-label = Intervallo della sincronizzazione automatica (secondi)
Expand Down
12 changes: 10 additions & 2 deletions addon/locale/ru-RU/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ editor-title = Note Editor
editor-expandLevel-label = Outline расширить до уровня заголовка
editor-keepLinks =
.label = Сохранить ссылки
editor-noteLinkPreview =
.label = Show note link preview on hover
editor-noteLinkPreviewType = Show preview for note link when:
editor-noteLinkPreview-hover =
.label = Hover
editor-noteLinkPreview-ctrl =
.label = Press { PLATFORM() ->
[macos]
*[other] Ctrl
}
editor-noteLinkPreview-disable =
.label = Never
sync-title = Синк
sync-period-label = Авто-синк период (сек)
Expand Down
12 changes: 10 additions & 2 deletions addon/locale/tr-TR/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ editor-title = Not Düzenleyici
editor-expandLevel-label = Anahatta gösterilecek başlık düzeyleri
editor-keepLinks =
.label = Not bağlantılarını anahatta göster
editor-noteLinkPreview =
.label = İmleci bağlantının üzerine getirdiğinde ön izlemeyi göster
editor-noteLinkPreviewType = Show preview for note link when:
editor-noteLinkPreview-hover =
.label = Hover
editor-noteLinkPreview-ctrl =
.label = Press { PLATFORM() ->
[macos]
*[other] Ctrl
}
editor-noteLinkPreview-disable =
.label = Never
sync-title = Eşitle
sync-period-label = Otomatik Eşitleme Sıklığı (saniye)
Expand Down
12 changes: 10 additions & 2 deletions addon/locale/zh-CN/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ editor-title = 笔记编辑器
editor-expandLevel-label = 大纲展开至标题层级
editor-keepLinks =
.label = 在大纲中显示笔记链接
editor-noteLinkPreview =
.label = 鼠标悬停时显示笔记链接预览
editor-noteLinkPreviewType = 笔记链接预览触发方式:
editor-noteLinkPreview-hover =
.label = 鼠标悬停
editor-noteLinkPreview-ctrl =
.label = 按下 { PLATFORM() ->
[macos]
*[other] Ctrl
}
editor-noteLinkPreview-disable =
.label = 从不
sync-title = 同步
sync-period-label = 自动同步周期 (秒)
Expand Down
2 changes: 1 addition & 1 deletion addon/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pref("__prefsPrefix__.exportNote", false);
pref("__prefsPrefix__.workspace.outline.expandLevel", 2);
pref("__prefsPrefix__.workspace.outline.keepLinks", true);

pref("__prefsPrefix__.editor.noteLinkPreview", true);
pref("__prefsPrefix__.editor.noteLinkPreviewType", "hover");

pref("__prefsPrefix__.openNote.takeover", true);
pref("__prefsPrefix__.openNote.defaultAsWindow", false);
Expand Down
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
} from "./utils/relation";
import { getWorkspaceByTabID, getWorkspaceByUID } from "./utils/workspace";
import { getString } from "./utils/locale";
import { showRestartHint } from "./utils/hint";

const workspace = {
getWorkspaceByTabID,
Expand Down Expand Up @@ -181,6 +182,7 @@ const relation = {

const utils = {
getString,
requireRestart: showRestartHint,
};

export default {
Expand Down
39 changes: 37 additions & 2 deletions src/extras/editor/linkPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface LinkPreviewOptions {
) => void;

openURL: (url: string) => void;

requireCtrl: boolean;
}

class LinkPreviewState {
Expand Down Expand Up @@ -67,7 +69,7 @@ class LinkPreviewState {
this.node = target;
this.currentLink = href;
this.hasHover = true;
this.tryOpenPopup();
this.tryOpenPopupByHover();
}
}
}
Expand All @@ -79,7 +81,24 @@ class LinkPreviewState {
}
};

tryOpenPopup() {
handleKeydown = async (event: KeyboardEvent) => {
if (!this.options.requireCtrl) {
return;
}
if (!this.hasHover || !this.currentLink) {
return;
}
const isMac =
typeof navigator != "undefined" ? /Mac/.test(navigator.platform) : false;
if ((isMac && event.metaKey) || (!isMac && event.ctrlKey)) {
this.tryTogglePopupByKey();
}
};

tryOpenPopupByHover() {
if (this.options.requireCtrl) {
return;
}
const href = this.currentLink!;
setTimeout(() => {
if (this.currentLink === href) {
Expand All @@ -88,6 +107,14 @@ class LinkPreviewState {
}, 300);
}

tryTogglePopupByKey() {
if (this._hasPopup()) {
this._closePopup();
} else {
this._openPopup();
}
}

_openPopup() {
console.log("Enter Link Preview", this.currentLink, this.options);
document.querySelectorAll(".link-preview").forEach((el) => el.remove());
Expand Down Expand Up @@ -158,6 +185,10 @@ class LinkPreviewState {
document.querySelectorAll(".link-preview").forEach((el) => el.remove());
this.popup = null;
}

_hasPopup() {
return !!document.querySelector(".link-preview");
}
}

function initLinkPreviewPlugin(options: LinkPreviewOptions) {
Expand Down Expand Up @@ -185,6 +216,10 @@ function initLinkPreviewPlugin(options: LinkPreviewOptions) {
pluginState.update(view.state);
pluginState.handleMouseMove(event);
},
keydown: (view, event) => {
const pluginState = key.getState(view.state) as LinkPreviewState;
pluginState.handleKeydown(event);
},
wheel: (view, event) => {
const pluginState = key.getState(view.state) as LinkPreviewState;
pluginState.popup?.layoutPopup(pluginState);
Expand Down
6 changes: 5 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import { showSyncDiff } from "./modules/sync/diffWindow";
import { showSyncInfo } from "./modules/sync/infoWindow";
import { showSyncManager } from "./modules/sync/managerWindow";
import { showTemplateEditor } from "./modules/template/editorWindow";
import { createNoteFromTemplate, createNoteFromMD, createNote } from "./modules/createNote";
import {
createNoteFromTemplate,
createNoteFromMD,
createNote,
} from "./modules/createNote";
import { createZToolkit } from "./utils/ztoolkit";
import { waitUtilAsync } from "./utils/wait";
import { initSyncList } from "./modules/sync/api";
Expand Down
4 changes: 3 additions & 1 deletion src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ function getTextBetweenLines(
}

function initLinkPreview(editor: Zotero.EditorInstance) {
if (!getPref("editor.noteLinkPreview")) {
const previewType = getPref("editor.noteLinkPreviewType") as string;
if (!["hover", "ctrl"].includes(previewType)) {
return;
}
const EditorAPI = getEditorAPI(editor);
Expand All @@ -469,6 +470,7 @@ function initLinkPreview(editor: Zotero.EditorInstance) {
openURL: (url: string) => {
Zotero.getActiveZoteroPane().loadURI(url);
},
requireCtrl: previewType === "ctrl",
},
editor._iframeWindow,
{ wrapReflectors: true, cloneFunctions: true },
Expand Down
25 changes: 24 additions & 1 deletion src/utils/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ async function showHintWithLink(
return progress;
}

export { showHint, showHintWithLink };
function showRestartHint() {
const ps = Services.prompt;
const buttonFlags =
ps.BUTTON_POS_0 * ps.BUTTON_TITLE_IS_STRING +
ps.BUTTON_POS_1 * ps.BUTTON_TITLE_IS_STRING;
const index = ps.confirmEx(
// @ts-ignore
null,
Zotero.getString("general.restartRequired"),
Zotero.getString("general.restartRequiredForChange", Zotero.appName),
buttonFlags,
Zotero.getString("general.restartNow"),
Zotero.getString("general.restartLater"),
null,
null,
{},
);

if (index == 0) {
Zotero.Utilities.Internal.quit(true);
}
}

export { showHint, showHintWithLink, showRestartHint };

0 comments on commit 13e172a

Please sign in to comment.