Remove checking for class names for instance checks

This commit is contained in:
Henrik Giesel 2021-01-29 17:41:27 +01:00
parent 982372beae
commit 32ee863577

View file

@ -91,7 +91,7 @@ function onKeyUp(evt: KeyboardEvent): void {
if ( if (
nodeIsElement(anchor) && nodeIsElement(anchor) &&
anchor.tagName === "DIV" && anchor.tagName === "DIV" &&
!anchor.classList.contains("field") && !(anchor instanceof EditingContainer) &&
anchor.childElementCount === 1 && anchor.childElementCount === 1 &&
anchor.children[0].tagName === "BR" anchor.children[0].tagName === "BR"
) { ) {
@ -256,7 +256,7 @@ function onFocus(evt: FocusEvent): void {
} }
elem.focusEditingArea(); elem.focusEditingArea();
currentField = elem; currentField = elem;
pycmd(`focus:${currentFieldOrdinal()}`); pycmd(`focus:${currentField.ord}`);
enableButtons(); enableButtons();
// do this twice so that there's no flicker on newer versions // do this twice so that there's no flicker on newer versions
caretToEnd(); caretToEnd();
@ -291,7 +291,7 @@ function focusIfField(x: number, y: number): boolean {
const elements = document.elementsFromPoint(x, y); const elements = document.elementsFromPoint(x, y);
for (let i = 0; i < elements.length; i++) { for (let i = 0; i < elements.length; i++) {
let elem = elements[i] as EditingContainer; let elem = elements[i] as EditingContainer;
if (elem.classList.contains("field")) { if (elem instanceof EditingContainer) {
elem.focusEditingArea(); elem.focusEditingArea();
// the focus event may not fire if the window is not active, so make sure // the focus event may not fire if the window is not active, so make sure
// the current field is set // the current field is set
@ -354,14 +354,10 @@ function saveField(type: "blur" | "key"): void {
} }
pycmd( pycmd(
`${type}:${currentFieldOrdinal()}:${currentNoteId}:${currentField.fieldHTML}` `${type}:${currentField.ord}:${currentNoteId}:${currentField.fieldHTML}`
); );
} }
function currentFieldOrdinal(): string {
return currentField.id.substring(1);
}
function wrappedExceptForWhitespace(text: string, front: string, back: string): string { function wrappedExceptForWhitespace(text: string, front: string, back: string): string {
const match = text.match(/^(\s*)([^]*?)(\s*)$/); const match = text.match(/^(\s*)([^]*?)(\s*)$/);
return match[1] + front + match[2] + back + match[3]; return match[1] + front + match[2] + back + match[3];
@ -385,10 +381,10 @@ function enableButtons(): void {
// disable the buttons if a field is not currently focused // disable the buttons if a field is not currently focused
function maybeDisableButtons(): void { function maybeDisableButtons(): void {
if (!document.activeElement || document.activeElement.className !== "field") { if (document.activeElement instanceof EditingContainer) {
disableButtons();
} else {
enableButtons(); enableButtons();
} else {
disableButtons();
} }
} }
@ -434,6 +430,16 @@ class EditingArea extends HTMLElement {
this.setAttribute("contenteditable", ""); this.setAttribute("contenteditable", "");
} }
static get observedAttributes(): string[] {
return ['ord'];
}
attributeChangedCallback(name: string, _oldValue: string, newValue: string): void {
switch (name) {
case "ord": this.id = `editor-field-${newValue}`;
}
}
set fieldHTML(content: string) { set fieldHTML(content: string) {
this.innerHTML = content; this.innerHTML = content;
@ -473,6 +479,21 @@ class EditingContainer extends HTMLDivElement {
this.shadowRoot.appendChild(this.editingArea); this.shadowRoot.appendChild(this.editingArea);
} }
static get observedAttributes(): string[] {
return ['ord'];
}
attributeChangedCallback(name: string, _oldValue: string, newValue: string): void {
switch (name) {
case "ord":
this.id = `f${newValue}`;
}
}
get ord(): number {
return Number(this.getAttribute("ord"));
}
connectedCallback(): void { connectedCallback(): void {
this.addEventListener("keydown", onKey); this.addEventListener("keydown", onKey);
this.addEventListener("keyup", onKeyUp); this.addEventListener("keyup", onKeyUp);
@ -493,8 +514,6 @@ class EditingContainer extends HTMLDivElement {
}`, }`,
0 0
); );
this.editingArea.id = `editor-field-${this.id.match(/\d+$/)[0]}`;
} }
disconnectedCallback(): void { disconnectedCallback(): void {
@ -575,10 +594,19 @@ class EditorField extends HTMLDivElement {
this.appendChild(this.editingContainer); this.appendChild(this.editingContainer);
} }
connectedCallback(): void { static get observedAttributes(): string[] {
const index = Array.prototype.indexOf.call(this.parentNode.children, this); return ['ord'];
this.labelContainer.id = `name${index}`; }
this.editingContainer.id = `f${index}`;
attributeChangedCallback(name: string, _oldValue: string, newValue: string): void {
switch (name) {
case "ord":
this.editingContainer.setAttribute("ord", newValue);
}
}
set ord(n: number) {
this.setAttribute("ord", String(n));
} }
initialize(label: string, color: string, content: string): void { initialize(label: string, color: string, content: string): void {
@ -597,9 +625,9 @@ function adjustFieldAmount(amount: number): void {
const fieldsContainer = document.getElementById("fields"); const fieldsContainer = document.getElementById("fields");
while (fieldsContainer.childElementCount < amount) { while (fieldsContainer.childElementCount < amount) {
fieldsContainer.appendChild( const newField = document.createElement("div", { is: "editor-field" }) as EditorField;
document.createElement("div", { is: "editor-field" }) newField.ord = fieldsContainer.childElementCount;
); fieldsContainer.appendChild(newField);
} }
while (fieldsContainer.childElementCount > amount) { while (fieldsContainer.childElementCount > amount) {