From 39c19d2fe2d00623b90cf455e50845daf8fa2a24 Mon Sep 17 00:00:00 2001 From: vorotamoroz Date: Tue, 28 Mar 2023 17:25:11 +0900 Subject: [PATCH] Implemented: - React to interactions on limited elements. --- main.ts | 93 +++++++++++++++++++++++++++++++++++++++++++---- manifest.json | 2 +- package-lock.json | 4 +- package.json | 2 +- 4 files changed, 90 insertions(+), 11 deletions(-) diff --git a/main.ts b/main.ts index 601d469..572f22e 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { App, Plugin } from "obsidian"; +import { App, Plugin, PluginSettingTab, Setting } from "obsidian"; function waitForReflowComplete() { return new Promise((res) => { @@ -15,9 +15,11 @@ class NinjaCursorForWindow { app: App; bufferedDocument: Document; bufferedWindow: Window; + plugin: NinjaCursorPlugin; - constructor(app: App, aw: Window, ad: Document, registerDomEvent: CallableFunction) { - this.app = app; + constructor(plugin: NinjaCursorPlugin, aw: Window, ad: Document, registerDomEvent: CallableFunction) { + this.plugin = plugin; + this.app = plugin.app; // buffering this.bufferedWindow = aw; this.bufferedDocument = ad; @@ -41,6 +43,16 @@ class NinjaCursorForWindow { processing = false; } const __moveCursor = async (e?: Event, noAnimate?: boolean) => { + if ([ + !this.plugin.settings.reactToContentEditable && !this.plugin.settings.reactToInputElement && !this.plugin.settings.reactToVimMode, + this.plugin.settings.reactToContentEditable && e && e.target instanceof HTMLElement && e.target.isContentEditable, + this.plugin.settings.reactToInputElement && e && e.target instanceof HTMLElement && e.target.tagName == "INPUT", + this.plugin.settings.reactToVimMode && e && e.target instanceof HTMLElement && e.target.closest(".cm-vimMode") + ].every(e => !e)) { + // When anything configured and no matched elements. + // At here, do not hide the cursor for the smoother animation. + return; + } if (e && e.target instanceof HTMLElement && (e.target.isContentEditable || e.target.tagName == "INPUT")) { // If it caused by clicking an element and it is editable. datumElement = e.target; @@ -198,14 +210,16 @@ class NinjaCursorForWindow { export default class NinjaCursorPlugin extends Plugin { Cursors: NinjaCursorForWindow[] = []; + settings: NinjaCursorSettings; async onload() { + await this.loadSettings(); this.registerEvent(this.app.workspace.on("window-open", (win) => { console.log("Open by window-open") const exist = this.Cursors.find(e => e.bufferedWindow == win.win); if (!exist) { - const w = new NinjaCursorForWindow(app, win.win, win.doc, this.registerDomEvent.bind(this)); + const w = new NinjaCursorForWindow(this, win.win, win.doc, this.registerDomEvent.bind(this)); this.Cursors.push(w); } })); @@ -218,8 +232,9 @@ export default class NinjaCursorPlugin extends Plugin { })); console.log("Open by init") - const w = new NinjaCursorForWindow(app, window, document, this.registerDomEvent.bind(this)); + const w = new NinjaCursorForWindow(this, window, document, this.registerDomEvent.bind(this)); this.Cursors.push(w); + this.addSettingTab(new ObsidianLiveSyncSettingTab(this.app, this)); } onunload() { @@ -228,7 +243,71 @@ export default class NinjaCursorPlugin extends Plugin { } } - async loadSettings() { } + async loadSettings() { + const settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()) as NinjaCursorSettings; + this.settings = settings; + } + + async saveSettings() { + const settings = { ...this.settings }; + await this.saveData(settings); + } +} + +export class ObsidianLiveSyncSettingTab extends PluginSettingTab { + plugin: NinjaCursorPlugin; + selectedScreen = ""; + + constructor(app: App, plugin: NinjaCursorPlugin) { + super(app, plugin); + this.plugin = plugin; + } + display(): void { + const { containerEl } = this; + + containerEl.empty(); + + containerEl.createEl('h2', { text: 'Settings for Ninja-cursor' }); + containerEl.createEl('h3', { text: 'React to interactions on limited elements' }); + containerEl.createDiv("", el => { + el.textContent = "If nothing is configured, react to all."; + }); + + new Setting(containerEl) + .setName('React to editor-ish elements') + .addToggle(toggle => toggle + .setValue(this.plugin.settings.reactToContentEditable) + .onChange(async (value) => { + this.plugin.settings.reactToContentEditable = value; + await this.plugin.saveSettings(); + })); + new Setting(containerEl) + .setName('React to plain-text elements') + .addToggle(toggle => toggle + .setValue(this.plugin.settings.reactToInputElement) + .onChange(async (value) => { + this.plugin.settings.reactToInputElement = value; + await this.plugin.saveSettings(); + })); + new Setting(containerEl) + .setName('React to the editor which in a vim-mode') + .addToggle(toggle => toggle + .setValue(this.plugin.settings.reactToVimMode) + .onChange(async (value) => { + this.plugin.settings.reactToVimMode = value; + await this.plugin.saveSettings(); + })); + } +} + - async saveSettings() { } +type NinjaCursorSettings = { + reactToContentEditable: boolean, + reactToVimMode: boolean, + reactToInputElement: boolean, } +export const DEFAULT_SETTINGS: NinjaCursorSettings = { + reactToContentEditable: false, + reactToVimMode: false, + reactToInputElement: false, +}; diff --git a/manifest.json b/manifest.json index fdeb26b..1a82951 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "ninja-cursor", "name": "Ninja Cursor", - "version": "0.0.10", + "version": "0.0.11", "minAppVersion": "0.12.0", "description": "The plugin which enhance cursor visibility.", "author": "vorotamoroz", diff --git a/package-lock.json b/package-lock.json index a8e5f0c..baaadb2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ninja-cursor", - "version": "0.0.10", + "version": "0.0.11", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ninja-cursor", - "version": "0.0.10", + "version": "0.0.11", "license": "MIT", "devDependencies": { "@types/node": "^16.11.6", diff --git a/package.json b/package.json index 2a44c60..870a656 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ninja-cursor", - "version": "0.0.10", + "version": "0.0.11", "description": "The plugin which enhance cursor visibility.", "main": "main.js", "scripts": {