Skip to content

Commit 39c19d2

Browse files
committed
Implemented:
- React to interactions on limited elements.
1 parent 24c8a3b commit 39c19d2

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

main.ts

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, Plugin } from "obsidian";
1+
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
22

33
function waitForReflowComplete() {
44
return new Promise((res) => {
@@ -15,9 +15,11 @@ class NinjaCursorForWindow {
1515
app: App;
1616
bufferedDocument: Document;
1717
bufferedWindow: Window;
18+
plugin: NinjaCursorPlugin;
1819

19-
constructor(app: App, aw: Window, ad: Document, registerDomEvent: CallableFunction) {
20-
this.app = app;
20+
constructor(plugin: NinjaCursorPlugin, aw: Window, ad: Document, registerDomEvent: CallableFunction) {
21+
this.plugin = plugin;
22+
this.app = plugin.app;
2123
// buffering
2224
this.bufferedWindow = aw;
2325
this.bufferedDocument = ad;
@@ -41,6 +43,16 @@ class NinjaCursorForWindow {
4143
processing = false;
4244
}
4345
const __moveCursor = async (e?: Event, noAnimate?: boolean) => {
46+
if ([
47+
!this.plugin.settings.reactToContentEditable && !this.plugin.settings.reactToInputElement && !this.plugin.settings.reactToVimMode,
48+
this.plugin.settings.reactToContentEditable && e && e.target instanceof HTMLElement && e.target.isContentEditable,
49+
this.plugin.settings.reactToInputElement && e && e.target instanceof HTMLElement && e.target.tagName == "INPUT",
50+
this.plugin.settings.reactToVimMode && e && e.target instanceof HTMLElement && e.target.closest(".cm-vimMode")
51+
].every(e => !e)) {
52+
// When anything configured and no matched elements.
53+
// At here, do not hide the cursor for the smoother animation.
54+
return;
55+
}
4456
if (e && e.target instanceof HTMLElement && (e.target.isContentEditable || e.target.tagName == "INPUT")) {
4557
// If it caused by clicking an element and it is editable.
4658
datumElement = e.target;
@@ -198,14 +210,16 @@ class NinjaCursorForWindow {
198210
export default class NinjaCursorPlugin extends Plugin {
199211

200212
Cursors: NinjaCursorForWindow[] = [];
213+
settings: NinjaCursorSettings;
201214

202215
async onload() {
216+
await this.loadSettings();
203217

204218
this.registerEvent(this.app.workspace.on("window-open", (win) => {
205219
console.log("Open by window-open")
206220
const exist = this.Cursors.find(e => e.bufferedWindow == win.win);
207221
if (!exist) {
208-
const w = new NinjaCursorForWindow(app, win.win, win.doc, this.registerDomEvent.bind(this));
222+
const w = new NinjaCursorForWindow(this, win.win, win.doc, this.registerDomEvent.bind(this));
209223
this.Cursors.push(w);
210224
}
211225
}));
@@ -218,8 +232,9 @@ export default class NinjaCursorPlugin extends Plugin {
218232
}));
219233

220234
console.log("Open by init")
221-
const w = new NinjaCursorForWindow(app, window, document, this.registerDomEvent.bind(this));
235+
const w = new NinjaCursorForWindow(this, window, document, this.registerDomEvent.bind(this));
222236
this.Cursors.push(w);
237+
this.addSettingTab(new ObsidianLiveSyncSettingTab(this.app, this));
223238
}
224239

225240
onunload() {
@@ -228,7 +243,71 @@ export default class NinjaCursorPlugin extends Plugin {
228243
}
229244
}
230245

231-
async loadSettings() { }
246+
async loadSettings() {
247+
const settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()) as NinjaCursorSettings;
248+
this.settings = settings;
249+
}
250+
251+
async saveSettings() {
252+
const settings = { ...this.settings };
253+
await this.saveData(settings);
254+
}
255+
}
256+
257+
export class ObsidianLiveSyncSettingTab extends PluginSettingTab {
258+
plugin: NinjaCursorPlugin;
259+
selectedScreen = "";
260+
261+
constructor(app: App, plugin: NinjaCursorPlugin) {
262+
super(app, plugin);
263+
this.plugin = plugin;
264+
}
265+
display(): void {
266+
const { containerEl } = this;
267+
268+
containerEl.empty();
269+
270+
containerEl.createEl('h2', { text: 'Settings for Ninja-cursor' });
271+
containerEl.createEl('h3', { text: 'React to interactions on limited elements' });
272+
containerEl.createDiv("", el => {
273+
el.textContent = "If nothing is configured, react to all.";
274+
});
275+
276+
new Setting(containerEl)
277+
.setName('React to editor-ish elements')
278+
.addToggle(toggle => toggle
279+
.setValue(this.plugin.settings.reactToContentEditable)
280+
.onChange(async (value) => {
281+
this.plugin.settings.reactToContentEditable = value;
282+
await this.plugin.saveSettings();
283+
}));
284+
new Setting(containerEl)
285+
.setName('React to plain-text elements')
286+
.addToggle(toggle => toggle
287+
.setValue(this.plugin.settings.reactToInputElement)
288+
.onChange(async (value) => {
289+
this.plugin.settings.reactToInputElement = value;
290+
await this.plugin.saveSettings();
291+
}));
292+
new Setting(containerEl)
293+
.setName('React to the editor which in a vim-mode')
294+
.addToggle(toggle => toggle
295+
.setValue(this.plugin.settings.reactToVimMode)
296+
.onChange(async (value) => {
297+
this.plugin.settings.reactToVimMode = value;
298+
await this.plugin.saveSettings();
299+
}));
300+
}
301+
}
302+
232303

233-
async saveSettings() { }
304+
type NinjaCursorSettings = {
305+
reactToContentEditable: boolean,
306+
reactToVimMode: boolean,
307+
reactToInputElement: boolean,
234308
}
309+
export const DEFAULT_SETTINGS: NinjaCursorSettings = {
310+
reactToContentEditable: false,
311+
reactToVimMode: false,
312+
reactToInputElement: false,
313+
};

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "ninja-cursor",
33
"name": "Ninja Cursor",
4-
"version": "0.0.10",
4+
"version": "0.0.11",
55
"minAppVersion": "0.12.0",
66
"description": "The plugin which enhance cursor visibility.",
77
"author": "vorotamoroz",

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ninja-cursor",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "The plugin which enhance cursor visibility.",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)