2021-04-13 10:57:08 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-08-03 05:52:07 +02:00
|
|
|
import type { EditorField } from "./editor-field";
|
2021-08-03 06:02:29 +02:00
|
|
|
import * as tr from "lib/i18n";
|
2021-08-03 05:52:07 +02:00
|
|
|
|
|
|
|
import { registerShortcut } from "lib/shortcuts";
|
2021-02-28 14:12:48 +01:00
|
|
|
import { bridgeCommand } from "./lib";
|
2021-08-03 06:02:29 +02:00
|
|
|
import { appendInParentheses } from "./helpers";
|
|
|
|
import { getCurrentField, forEditorField, i18n } from ".";
|
2021-03-28 15:45:51 +02:00
|
|
|
import pinIcon from "./pin-angle.svg";
|
2021-02-28 14:12:48 +01:00
|
|
|
|
2021-08-03 06:12:04 +02:00
|
|
|
function toggleStickyCurrentField(): void {
|
2021-08-03 05:52:07 +02:00
|
|
|
const currentField = getCurrentField();
|
|
|
|
|
|
|
|
if (currentField) {
|
|
|
|
const labelContainer = (currentField.parentElement as EditorField)
|
|
|
|
.labelContainer;
|
|
|
|
labelContainer.toggleSticky();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 06:12:04 +02:00
|
|
|
function toggleStickyAll(): void {
|
2021-08-03 05:52:07 +02:00
|
|
|
bridgeCommand("toggleStickyAll", (values: boolean[]) =>
|
|
|
|
forEditorField(values, (field, value) => field.labelContainer.setSticky(value))
|
|
|
|
);
|
2021-03-01 15:20:31 +01:00
|
|
|
}
|
|
|
|
|
2021-08-03 06:12:04 +02:00
|
|
|
export function activateStickyShortcuts(): void {
|
2021-08-03 05:52:07 +02:00
|
|
|
registerShortcut(toggleStickyCurrentField, "F9");
|
|
|
|
registerShortcut(toggleStickyAll, "Shift+F9");
|
2021-03-01 15:20:31 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 14:12:48 +01:00
|
|
|
export class LabelContainer extends HTMLDivElement {
|
|
|
|
sticky: HTMLSpanElement;
|
|
|
|
label: HTMLSpanElement;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2021-03-01 13:06:07 +01:00
|
|
|
this.className = "d-flex justify-content-between";
|
|
|
|
|
2021-08-03 06:02:29 +02:00
|
|
|
i18n.then(() => {
|
|
|
|
this.title = appendInParentheses(tr.editingToggleSticky(), "F9");
|
|
|
|
});
|
|
|
|
|
2021-03-01 13:06:07 +01:00
|
|
|
this.label = document.createElement("span");
|
|
|
|
this.label.className = "fieldname";
|
|
|
|
this.appendChild(this.label);
|
2021-02-28 14:12:48 +01:00
|
|
|
|
|
|
|
this.sticky = document.createElement("span");
|
2021-03-28 15:45:51 +02:00
|
|
|
this.sticky.className = "icon pin-icon me-1";
|
|
|
|
this.sticky.innerHTML = pinIcon;
|
2021-02-28 14:12:48 +01:00
|
|
|
this.sticky.hidden = true;
|
|
|
|
this.appendChild(this.sticky);
|
|
|
|
|
2021-08-03 05:52:07 +02:00
|
|
|
this.setSticky = this.setSticky.bind(this);
|
|
|
|
this.hoverIcon = this.hoverIcon.bind(this);
|
|
|
|
this.removeHoverIcon = this.removeHoverIcon.bind(this);
|
2021-02-28 14:12:48 +01:00
|
|
|
this.toggleSticky = this.toggleSticky.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback(): void {
|
|
|
|
this.sticky.addEventListener("click", this.toggleSticky);
|
2021-08-03 05:52:07 +02:00
|
|
|
this.sticky.addEventListener("mouseenter", this.hoverIcon);
|
|
|
|
this.sticky.addEventListener("mouseleave", this.removeHoverIcon);
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
|
|
|
this.sticky.removeEventListener("click", this.toggleSticky);
|
2021-08-03 05:52:07 +02:00
|
|
|
this.sticky.removeEventListener("mouseenter", this.hoverIcon);
|
|
|
|
this.sticky.removeEventListener("mouseleave", this.removeHoverIcon);
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
initialize(labelName: string): void {
|
|
|
|
this.label.innerText = labelName;
|
|
|
|
}
|
|
|
|
|
2021-08-03 05:52:07 +02:00
|
|
|
activateSticky(initialState: boolean): void {
|
|
|
|
this.setSticky(initialState);
|
|
|
|
this.sticky.hidden = false;
|
|
|
|
}
|
|
|
|
|
2021-02-28 18:46:43 +01:00
|
|
|
setSticky(state: boolean): void {
|
2021-03-08 21:18:53 +01:00
|
|
|
this.sticky.classList.toggle("is-inactive", !state);
|
2021-02-28 18:46:43 +01:00
|
|
|
}
|
|
|
|
|
2021-08-03 05:52:07 +02:00
|
|
|
hoverIcon(): void {
|
|
|
|
this.sticky.classList.add("icon--hover");
|
|
|
|
}
|
|
|
|
|
|
|
|
removeHoverIcon(): void {
|
|
|
|
this.sticky.classList.remove("icon--hover");
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-08-03 05:52:07 +02:00
|
|
|
toggleSticky(): void {
|
|
|
|
bridgeCommand(`toggleSticky:${this.getAttribute("ord")}`, this.setSticky);
|
|
|
|
this.removeHoverIcon();
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
}
|