|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var obsidian = require('obsidian'); |
| 4 | +var i18n = require('./i18n'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Open a settings context menu on empty area of Session Manager / Quick Switcher. |
| 8 | + * |
| 9 | + * @param {Object} options |
| 10 | + * @param {Object} options.plugin - plugin instance |
| 11 | + * @param {Object} options.app - Obsidian app |
| 12 | + * @param {MouseEvent} options.event - right-click event |
| 13 | + * @param {boolean} [options.showResetOverlay] - show "Reset position and size" (Quick Switcher only) |
| 14 | + * @param {Function} [options.onResetOverlay] - callback for reset position/size |
| 15 | + * @param {Function} [options.onChanged] - callback after any setting toggled (for UI refresh) |
| 16 | + */ |
| 17 | +function openSettingsContextMenu(options) { |
| 18 | + var L = i18n.L; |
| 19 | + options = options || {}; |
| 20 | + var plugin = options.plugin; |
| 21 | + var app = options.app || (plugin ? plugin.app : null); |
| 22 | + if (!plugin || !app) return; |
| 23 | + |
| 24 | + var menu = new obsidian.Menu(); |
| 25 | + |
| 26 | + // --- Auto-save section --- |
| 27 | + var autoSaveOn = plugin.isAutoSaveOnSwitchEnabled(); |
| 28 | + |
| 29 | + menu.addItem(function (mi) { |
| 30 | + mi.setTitle(L.settingsAutoSaveOnSwitch); |
| 31 | + mi.setIcon('save'); |
| 32 | + if (autoSaveOn) mi.setChecked(true); |
| 33 | + mi.onClick(function () { |
| 34 | + plugin.setAutoSaveOnSwitch(!autoSaveOn, { notify: true }).then(function () { |
| 35 | + if (typeof options.onChanged === 'function') options.onChanged(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + if (!autoSaveOn) { |
| 41 | + menu.addItem(function (mi) { |
| 42 | + mi.setTitle(L.settingsWarnUnsavedSwitch); |
| 43 | + mi.setIcon('alert-triangle'); |
| 44 | + if (plugin.isWarnOnUnsavedSwitchEnabled()) mi.setChecked(true); |
| 45 | + mi.onClick(function () { |
| 46 | + plugin.data.warnOnUnsavedSwitch = !plugin.isWarnOnUnsavedSwitchEnabled(); |
| 47 | + plugin.persistData(); |
| 48 | + if (typeof options.onChanged === 'function') options.onChanged(); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + menu.addItem(function (mi) { |
| 53 | + mi.setTitle(L.settingsConfirmQuickActions); |
| 54 | + mi.setIcon('check-circle'); |
| 55 | + if (plugin.data.confirmQuickActions) mi.setChecked(true); |
| 56 | + mi.onClick(function () { |
| 57 | + plugin.data.confirmQuickActions = !plugin.data.confirmQuickActions; |
| 58 | + plugin.persistData(); |
| 59 | + if (typeof options.onChanged === 'function') options.onChanged(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + menu.addItem(function (mi) { |
| 65 | + mi.setTitle(L.settingsConfirmDelete); |
| 66 | + mi.setIcon('shield'); |
| 67 | + if (plugin.data.confirmDeleteByHotkey !== false) mi.setChecked(true); |
| 68 | + mi.onClick(function () { |
| 69 | + plugin.data.confirmDeleteByHotkey = !(plugin.data.confirmDeleteByHotkey !== false); |
| 70 | + plugin.persistData(); |
| 71 | + if (typeof options.onChanged === 'function') options.onChanged(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + menu.addSeparator(); |
| 76 | + |
| 77 | + // --- Feature toggles --- |
| 78 | + menu.addItem(function (mi) { |
| 79 | + mi.setTitle(L.settingsVersionHistoryEnabled); |
| 80 | + mi.setIcon('history'); |
| 81 | + if (plugin.isVersionHistoryEnabled()) mi.setChecked(true); |
| 82 | + mi.onClick(function () { |
| 83 | + var next = !plugin.isVersionHistoryEnabled(); |
| 84 | + plugin.data.versionHistoryEnabled = next; |
| 85 | + plugin.persistData(); |
| 86 | + if (next) { |
| 87 | + plugin.startHistorySnapshotTimer(); |
| 88 | + } else { |
| 89 | + plugin.stopHistorySnapshotTimer(); |
| 90 | + } |
| 91 | + if (typeof options.onChanged === 'function') options.onChanged(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + menu.addItem(function (mi) { |
| 96 | + mi.setTitle(L.contextToggleGroups); |
| 97 | + mi.setIcon('folder'); |
| 98 | + if (plugin.isGroupFeatureEnabled()) mi.setChecked(true); |
| 99 | + mi.onClick(function () { |
| 100 | + plugin.setGroupFeatureEnabled(!plugin.isGroupFeatureEnabled()).then(function () { |
| 101 | + if (typeof options.onChanged === 'function') options.onChanged(); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + menu.addSeparator(); |
| 107 | + |
| 108 | + // --- Actions --- |
| 109 | + menu.addItem(function (mi) { |
| 110 | + mi.setTitle(L.rotationBackupCreate); |
| 111 | + mi.setIcon('archive'); |
| 112 | + mi.onClick(function () { |
| 113 | + var sessionData = plugin.extractSessionData(plugin.data); |
| 114 | + sessionData._wppSavedAt = Date.now(); |
| 115 | + plugin.ensureDir(plugin.getBackupsDirPath()) |
| 116 | + .then(function () { |
| 117 | + return plugin.copyFileIfExists( |
| 118 | + plugin.getRotationBackupPath(2), |
| 119 | + plugin.getRotationBackupPath(3) |
| 120 | + ); |
| 121 | + }) |
| 122 | + .then(function () { |
| 123 | + return plugin.copyFileIfExists( |
| 124 | + plugin.getRotationBackupPath(1), |
| 125 | + plugin.getRotationBackupPath(2) |
| 126 | + ); |
| 127 | + }) |
| 128 | + .then(function () { |
| 129 | + return plugin.writeJson( |
| 130 | + plugin.getRotationBackupPath(1), |
| 131 | + sessionData |
| 132 | + ); |
| 133 | + }) |
| 134 | + .then(function () { |
| 135 | + plugin._lastRotationBackupAt = Date.now(); |
| 136 | + new obsidian.Notice(L.rotationBackupCreated); |
| 137 | + }) |
| 138 | + .catch(function () { |
| 139 | + new obsidian.Notice(L.rotationBackupFailed); |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + menu.addItem(function (mi) { |
| 145 | + mi.setTitle(L.settingsHotkeysBtn); |
| 146 | + mi.setIcon('keyboard'); |
| 147 | + mi.onClick(function () { |
| 148 | + app.setting.open(); |
| 149 | + app.setting.openTabById('hotkeys'); |
| 150 | + var sc = app.setting.activeTab.searchComponent; |
| 151 | + var pluginName = (plugin.manifest && plugin.manifest.name) |
| 152 | + ? plugin.manifest.name |
| 153 | + : 'Workspace++'; |
| 154 | + sc.setValue(pluginName); |
| 155 | + sc.inputEl.dispatchEvent(new Event('input')); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + menu.addItem(function (mi) { |
| 160 | + mi.setTitle(L.contextOpenSettings); |
| 161 | + mi.setIcon('settings'); |
| 162 | + mi.onClick(function () { |
| 163 | + app.setting.open(); |
| 164 | + app.setting.openTabById(plugin.manifest.id); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + // --- Quick Switcher only: Reset position --- |
| 169 | + if (options.showResetOverlay) { |
| 170 | + menu.addSeparator(); |
| 171 | + menu.addItem(function (mi) { |
| 172 | + mi.setTitle(L.contextResetOverlayPosition); |
| 173 | + mi.setIcon('rotate-ccw'); |
| 174 | + mi.onClick(function () { |
| 175 | + if (typeof options.onResetOverlay === 'function') options.onResetOverlay(); |
| 176 | + }); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + menu.showAtMouseEvent(options.event); |
| 181 | +} |
| 182 | + |
| 183 | +module.exports = { |
| 184 | + openSettingsContextMenu: openSettingsContextMenu, |
| 185 | +}; |
0 commit comments