Skip to content

Commit

Permalink
feat: use CodeMirror's save hotkey in storage->key
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Jul 7, 2024
1 parent db4ce8f commit cef7d72
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/options/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export const kOrigMatch = 'origMatch';
export const kStorageSize = 'storageSize';
export const kUpdateURL = 'updateURL';

export let K_SAVE; // deduced from the current CodeMirror keymap

export function inferSaveHotKey(hotkeys) {
K_SAVE = hotkeys.find(([, cmd]) => cmd === 'save')?.[0];
if (!K_SAVE) {
K_SAVE = 'Ctrl-S';
hotkeys.unshift([K_SAVE, 'save']);
}
}

export function markRemove(script, removed) {
return sendCmdDirectly('MarkRemoved', {
id: script.props.id,
Expand Down
13 changes: 4 additions & 9 deletions src/options/views/edit/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ import { keyboardService } from '@/common/keyboard';
import options from '@/common/options';
import { getUnloadSentry } from '@/common/router';
import {
kDownloadURL, kExclude, kExcludeMatch, kHomepageURL, kIcon, kInclude, kMatch, kName, kOrigExclude,
kOrigExcludeMatch, kOrigInclude, kOrigMatch, kUpdateURL,
kDownloadURL, kExclude, kExcludeMatch, kHomepageURL, kIcon, kInclude, kMatch, kName, kOrigExclude, kOrigExcludeMatch,
kOrigInclude, kOrigMatch, kUpdateURL,
} from '../../utils';
const urlMatching = 'https://violentmonkey.github.io/api/matching/';
Expand Down Expand Up @@ -157,7 +157,7 @@ import { computed, nextTick, onActivated, onDeactivated, onMounted, ref, watch }
import VmCode from '@/common/ui/code';
import VmExternals from '@/common/ui/externals';
import LocaleGroup from '@/common/ui/locale-group';
import { kStorageSize, store } from '../../utils';
import { inferSaveHotKey, K_SAVE, kStorageSize, store } from '../../utils';
import VmSettings from './settings';
import VMSettingsUpdate from './settings-update';
import VmValues from './values';
Expand All @@ -166,7 +166,6 @@ import VmHelp from './help';
let CM;
let $codeComp;
let disposeList;
let K_SAVE; // deduced from the current CodeMirror keymap
let savedCopy;
let shouldSavePositionOnSave;
let toggleUnloadSentry;
Expand Down Expand Up @@ -269,11 +268,7 @@ onMounted(() => {
...Object.entries($codeComp.expandKeyMap())
.sort((a, b) => compareString(a[1], b[1]) || compareString(a[0], b[0])),
];
K_SAVE = hk.find(([, cmd]) => cmd === 'save')?.[0];
if (!K_SAVE) {
K_SAVE = 'Ctrl-S';
hk.unshift([K_SAVE, 'save']);
}
if (!K_SAVE) inferSaveHotKey(hk);
});
onActivated(() => {
Expand Down
9 changes: 8 additions & 1 deletion src/options/views/edit/values.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<input type="text" v-model="current.key" :readOnly="!current.isNew || readOnly"
ref="$key"
spellcheck="false"
@keydown="onKeyDownInKeyInput"
@keydown.esc.exact.stop="onCancel">
</label>
<label>
Expand Down Expand Up @@ -120,12 +121,13 @@ import { handleTabNavigation, keyboardService } from '@/common/keyboard';
import { deepCopy, deepEqual, forEachEntry, mapEntry } from '@/common/object';
import { WATCH_STORAGE } from '@/common/consts';
import hookSetting from '@/common/hook-setting';
import CodeMirror from 'codemirror';
import Dropdown from 'vueleton/lib/dropdown';
import VmCode from '@/common/ui/code';
import Icon from '@/common/ui/icon';
import { getActiveElement, showMessage } from '@/common/ui';
import SettingText from '@/common/ui/setting-text';
import { kStorageSize, toggleBoolean } from '../../utils';
import { K_SAVE, kStorageSize, toggleBoolean } from '../../utils';
const props = defineProps({
/** @type {VMScript} */
Expand Down Expand Up @@ -446,6 +448,11 @@ function onChange(isChanged) {
}
cur.jsonPaused = performance.now() - t0 > MAX_JSON_DURATION;
}
function onKeyDownInKeyInput(evt) {
if (CodeMirror.keyName(evt) === K_SAVE) {
onSave();
}
}
function onStorageChanged(changes) {
const data = Object.values(changes)[0].newValue;
if (data) {
Expand Down

0 comments on commit cef7d72

Please sign in to comment.