Migrate display and link settings declaratively

This commit is contained in:
zsviczian committed 2026-08-23 13:17:32 +02:00
1 parent 9f182616a5
commit b429e41f62
13 files changed
+1600 -798

No files matched your search

+115 -94
View File
@@ -8,17 +8,26 @@ import {
} from "src/utils/modifierkeyHelper";
import { setStyle } from "src/utils/styleUtils";
type ModifierKeyCategories = Partial<{
[modifierSetType in ModifierSetType]: string;
}>;
const CATEGORIES: ModifierKeyCategories = {
const CATEGORIES: Record<ModifierSetType, string> = {
WebBrowserDragAction: t("WEB_BROWSER_DRAG_ACTION"),
LocalFileDragAction: t("LOCAL_FILE_DRAG_ACTION"),
InternalDragAction: t("INTERNAL_DRAG_ACTION"),
LinkClickAction: t("PANE_TARGET"),
};
/** Modifier-key categories rendered by the settings component. */
export const MODIFIER_KEY_CATEGORIES = [
"WebBrowserDragAction",
"LocalFileDragAction",
"InternalDragAction",
"LinkClickAction",
] as const satisfies readonly ModifierSetType[];
/** Returns the localized heading for one modifier-key category. */
export const getModifierKeyCategoryName = (
modifierSetType: ModifierSetType,
): string => CATEGORIES[modifierSetType];
export class ModifierKeySettingsComponent {
private isMacOS: boolean;
@@ -28,12 +37,22 @@ export class ModifierKeySettingsComponent {
Mac: Record<string, ModifierKeySet>;
Win: Record<string, ModifierKeySet>;
},
private update?: () => void,
private update: () => void,
) {
this.isMacOS = DEVICE.isMacOS || DEVICE.isIOS;
}
render() {
render(): void {
MODIFIER_KEY_CATEGORIES.forEach((modifierSetType) =>
this.renderCategory(modifierSetType, true),
);
}
/** Renders one modifier-key category, optionally with its legacy fold. */
renderCategory(
modifierSetType: ModifierSetType,
includeSummary: boolean,
): void {
const platform = this.isMacOS ? "Mac" : "Win";
const modifierKeysConfig = this.modifierKeyConfig[platform];
const tooltipMessages = modifierKeyTooltipMessages() as Record<
@@ -41,102 +60,104 @@ export class ModifierKeySettingsComponent {
Record<string, string>
>;
Object.entries(CATEGORIES).forEach(([modifierSetType, label]) => {
const detailsEl = this.contentEl.createEl("details");
const detailsEl = includeSummary
? this.contentEl.createEl("details")
: this.contentEl;
if (includeSummary) {
detailsEl.createEl("summary", {
text: label,
text: getModifierKeyCategoryName(modifierSetType),
cls: "excalidraw-setting-h4",
});
}
const modifierKeys = modifierKeysConfig[modifierSetType];
detailsEl.createDiv({
text:
t("DEFAULT_ACTION_DESC") +
(tooltipMessages[modifierSetType]?.[modifierKeys.defaultAction] ??
""),
cls: "setting-item-description",
});
// Ensure all LinkClickAction rules have ctrl_cmd enabled
if (modifierSetType === "LinkClickAction") {
let dirty = false;
modifierKeys.rules.forEach((rule) => {
if (!rule.ctrl_cmd) {
rule.ctrl_cmd = true;
dirty = true;
}
});
if (dirty) {
this.update?.();
const modifierKeys = modifierKeysConfig[modifierSetType];
detailsEl.createDiv({
text:
t("DEFAULT_ACTION_DESC") +
(tooltipMessages[modifierSetType]?.[modifierKeys.defaultAction] ??
""),
cls: "setting-item-description",
});
// Ensure all LinkClickAction rules have ctrl_cmd enabled
if (modifierSetType === "LinkClickAction") {
let dirty = false;
modifierKeys.rules.forEach((rule) => {
if (!rule.ctrl_cmd) {
rule.ctrl_cmd = true;
dirty = true;
}
});
if (dirty) {
this.update();
}
}
// Column header row
const headerSetting = new Setting(detailsEl);
headerSetting.settingEl.addClass("modifier-key-header-row");
headerSetting.infoEl.remove();
[
"SHIFT",
this.isMacOS ? "CMD" : "CTRL",
this.isMacOS ? "OPT" : "ALT",
this.isMacOS ? "CTRL" : "META",
].forEach((key) => {
headerSetting.controlEl.createSpan({
text: key,
cls: "modifier-key-col-header",
});
// Column header row
const headerSetting = new Setting(detailsEl);
headerSetting.settingEl.addClass("modifier-key-header-row");
headerSetting.infoEl.remove();
[
"SHIFT",
this.isMacOS ? "CMD" : "CTRL",
this.isMacOS ? "OPT" : "ALT",
this.isMacOS ? "CTRL" : "META",
].forEach((key) => {
headerSetting.controlEl.createSpan({
text: key,
cls: "modifier-key-col-header",
});
});
modifierKeys.rules.forEach((rule) => {
const setting = new Setting(detailsEl).setName(
tooltipMessages[modifierSetType]?.[rule.result],
);
setting.addToggle((toggle) =>
toggle
.setValue(rule.shift)
.setTooltip("SHIFT")
.onChange((value) => {
rule.shift = value;
this.update();
}),
);
setting.addToggle((toggle) => {
const isLinkClick = modifierSetType === "LinkClickAction";
toggle
.setValue(isLinkClick ? true : rule.ctrl_cmd)
.setTooltip(this.isMacOS ? "CMD" : "CTRL")
.onChange((value) => {
rule.ctrl_cmd = value;
this.update();
});
if (isLinkClick || this.isMacOS) {
// CMD is always required for link-click actions (Excalidraw detects
// the click via CMD being held). On macOS, CMD is also reserved for
// non-link-click actions.
toggle.setDisabled(true);
setStyle(toggle.toggleEl, { opacity: "0.5" });
}
});
Object.entries(modifierKeys.rules).forEach(([_, rule]) => {
const setting = new Setting(detailsEl).setName(
tooltipMessages[modifierSetType]?.[rule.result],
);
setting.addToggle((toggle) =>
toggle
.setValue(rule.shift)
.setTooltip("SHIFT")
.onChange((value) => {
rule.shift = value;
this.update();
}),
);
setting.addToggle((toggle) => {
const isLinkClick = modifierSetType === "LinkClickAction";
toggle
.setValue(isLinkClick ? true : rule.ctrl_cmd)
.setTooltip(this.isMacOS ? "CMD" : "CTRL")
.onChange((value) => {
rule.ctrl_cmd = value;
this.update();
});
if (isLinkClick || this.isMacOS) {
// CMD is always required for link-click actions (Excalidraw detects
// the click via CMD being held). On macOS, CMD is also reserved for
// non-link-click actions.
toggle.setDisabled(true);
setStyle(toggle.toggleEl, { opacity: "0.5" });
}
});
setting.addToggle((toggle) =>
toggle
.setValue(rule.alt_opt)
.setTooltip(this.isMacOS ? "OPT" : "ALT")
.onChange((value) => {
rule.alt_opt = value;
this.update();
}),
);
setting.addToggle((toggle) =>
toggle
.setValue(rule.meta_ctrl)
.setTooltip(this.isMacOS ? "CTRL" : "META")
.onChange((value) => {
rule.meta_ctrl = value;
this.update();
}),
);
});
setting.addToggle((toggle) =>
toggle
.setValue(rule.alt_opt)
.setTooltip(this.isMacOS ? "OPT" : "ALT")
.onChange((value) => {
rule.alt_opt = value;
this.update();
}),
);
setting.addToggle((toggle) =>
toggle
.setValue(rule.meta_ctrl)
.setTooltip(this.isMacOS ? "CTRL" : "META")
.onChange((value) => {
rule.meta_ctrl = value;
this.update();
}),
);
});
}
}