Skip to content

Commit

Permalink
Implemented:
Browse files Browse the repository at this point in the history
- React to interactions on limited elements.
  • Loading branch information
vrtmrz committed Mar 28, 2023
1 parent 24c8a3b commit 39c19d2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
93 changes: 86 additions & 7 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Plugin } from "obsidian";
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";

function waitForReflowComplete() {
return new Promise((res) => {
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}));
Expand All @@ -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() {
Expand All @@ -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,
};
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 39c19d2

Please sign in to comment.