anki/ts/editor/destroyable.ts
Henrik Giesel c9bd39c6a2
Several editor fixes (#1478)
* Declare toolbar api via modifiable property

* Fix addon buttons

* Assign editing areas in a synchronous way

* Restore the Tab shortcut that moves the caret to end

- moveCaretToEnd is called twice now: The moveCaretToEnd calls in
  *TextInput causes the caret to be moved to the end when switching back
  from the window and back.
- To fix this, I will need the code for saving and restoring locations
  from #1377

* Restore selections in the PlainTextInput

* Improve type safety of destroyable

- clearable-array was renamed to destroyable
2021-11-05 11:29:02 +10:00

36 lines
1.1 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export interface Destroyable {
destroy(): void;
}
export function clearableArray<T>(): (T & Destroyable)[] {
const list: (T & Destroyable)[] = [];
return new Proxy(list, {
get: function (target: (T & Destroyable)[], prop: string | symbol) {
if (!isNaN(Number(prop)) && !target[prop]) {
const item = {} as T & Destroyable;
const destroy = (): void => {
const index = list.indexOf(item);
list.splice(index, 1);
};
target[prop] = new Proxy(item, {
get: function (target: T & Destroyable, prop: string | symbol) {
if (prop === "destroy") {
return destroy;
}
return target[prop];
},
});
}
return target[prop];
},
});
}