From 8a679fb9bae12ef059d5bd703c00122044cf64be Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Tue, 1 Jun 2021 13:37:27 -0700 Subject: [PATCH] Use modesubconfigs instead of preventautofocusmodes --- src/excmds.ts | 43 ++++++++++++++++++++++++++++++++++++++++++- src/lib/config.ts | 30 +++++++++++++++--------------- src/lib/dom.ts | 4 ++-- 3 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/excmds.ts b/src/excmds.ts index bd1db19e..696b1191 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -3778,6 +3778,28 @@ export function seturl(pattern: string, key: string, ...values: string[]) { return config.setURL(pattern, ...validateSetArgs(key, values)) } +/** + * Usage: `setmode mode key values` + * + * @param mode The Mode the setting should be set for, e.g. `insert` or `ignore`. + * @param key The name of the setting you want to set, e.g. `allowautofocus` + * @param values The value you wish for, e.g. `true` + * + * Currently this command is only supported for the following settings: + * - [[allowautofocus]] + * + * Example: + * - `setmode ignore allowautofocus true` + */ +//#content +export function setmode(mode: string, key: string, ...values: string[]) { + if (!mode || !key || !values.length) { + throw new Error("seturl syntax: mode key value") + } + + return config.set("modesubconfigs", mode, ...validateSetArgs(key, values)) +} + /** Set a key value pair in config. Use to set any values found [here](/static/docs/classes/_src_lib_config_.default_config.html). @@ -4007,7 +4029,7 @@ export async function unbind(...args: string[]) { * * This unbinds `I` in ignore mode on every website the URL of which contains `jupyter`, while keeping the binding active everywhere else. * - * Also see [[bind]], [[bindurl]], [[seturl]], [[unbind]], [[unseturl]] + * Also see [[bind]], [[bindurl]], [[seturl]], [[unbind]], [[unseturl]], [[setmode]], [[unsetmode]] */ //#background export async function unbindurl(pattern: string, mode: string, keys: string) { @@ -4043,6 +4065,8 @@ export async function reset(mode: string, key: string) { * - [[unbindurl]] * - [[seturl]] * - [[unseturl]] + * - [[setmode]] + * - [[unsetmode]] */ //#background export async function reseturl(pattern: string, mode: string, key: string) { @@ -4263,6 +4287,23 @@ export function unseturl(pattern: string, key: string) { return config.unsetURL(pattern, key.split(".")) } +/** + * Reset a mode-specific setting. + * + * usage: `unsetmode mode key` + * + * @param mode The mode the setting should be unset on, e.g. `insert`. + * @param key The key that should be unset. + * + * Example: `unsetmode ignore allowautofocus` + * + * Note that this removes a setting from the mode-specific config, it doesn't "invert" it. This means that if you have a setting set to `false` in your global config and the same setting set to `false` in a mode-specific setting, using `unseturl` will result in the setting still being set to `false`. + */ +//#content +export function unsetmode(mode: string, key: string) { + return config.unset("modesubconfigs", mode, ...key.split(".")) +} + /** * Reset a config setting to default */ diff --git a/src/lib/config.ts b/src/lib/config.ts index c55c4da9..db5080c3 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -94,6 +94,21 @@ export class default_config { }, } + /** + * Internal field to handle mode-specific configs. Use :setmode/:unsetmode to change these values. + * + * Changing this might do weird stuff. + */ + modesubconfigs: { [key: string]: DeepPartial } = { + "normal": {}, + "insert": {}, + "input": {}, + "ignore": {}, + "ex": {}, + "hint": {}, + "visual": {}, + } + /** * Internal field to handle site-specific config priorities. Use :seturl/:unseturl to change this value. */ @@ -749,21 +764,6 @@ export class default_config { */ allowautofocus: "true" | "false" = "true" - /** - * Controls modes which the [[allowautofocus]] setting will affect. - * - * This lets you suppress the effects of [[allowautofocus]] to allow page behavior in certain modes - by default this setting always allows the setting to take effect. - */ - preventautofocusmodes: { [key: string]: "true" | "false" } = { - normal: "true", - insert: "true", - input: "true", - ignore: "true", - ex: "true", - hint: "true", - visual: "true", - } - /** * Uses a loop to prevent focus until you interact with a page. Only recommended for use via `seturl` for problematic sites as it can be a little heavy on CPU if running on all tabs. Should be used in conjuction with [[allowautofocus]] */ diff --git a/src/lib/dom.ts b/src/lib/dom.ts index 4db3b00e..01bea414 100644 --- a/src/lib/dom.ts +++ b/src/lib/dom.ts @@ -538,8 +538,8 @@ function onPageFocus(elem: HTMLElement): boolean { if (isTextEditable(elem)) { LAST_USED_INPUT = elem } - const suppressSetting = config.get("preventautofocusmodes", contentState.mode) === "false" - return suppressSetting || config.get("allowautofocus") === "true" + const setting = config.get("modesubconfigs", contentState.mode, "allowautofocus") || config.get("allowautofocus") + return setting === "true" } async function setInput(el) {