2022-01-08 02:46:01 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import { on, preventDefault } from "../lib/events";
|
|
|
|
import { registerShortcut } from "../lib/shortcuts";
|
|
|
|
import { placeCaretAfterContent } from "../domlib/place-caret";
|
|
|
|
import { saveSelection, restoreSelection } from "../domlib/location";
|
2022-01-16 06:05:35 +01:00
|
|
|
import { isApplePlatform } from "../lib/platform";
|
|
|
|
import { bridgeCommand } from "../lib/bridgecommand";
|
2022-01-08 02:46:01 +01:00
|
|
|
import type { SelectionLocation } from "../domlib/location";
|
|
|
|
|
|
|
|
const locationEvents: (() => void)[] = [];
|
|
|
|
|
|
|
|
function flushLocation(): void {
|
|
|
|
let removeEvent: (() => void) | undefined;
|
|
|
|
|
|
|
|
while ((removeEvent = locationEvents.pop())) {
|
|
|
|
removeEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
function safePlaceCaretAfterContent(editable: HTMLElement): void {
|
|
|
|
/**
|
|
|
|
* Workaround: If you try to invoke an IME after calling
|
|
|
|
* `placeCaretAfterContent` on a cE element, the IME will immediately
|
|
|
|
* end and the input character will be duplicated
|
|
|
|
*/
|
|
|
|
placeCaretAfterContent(editable);
|
|
|
|
restoreSelection(editable, saveSelection(editable)!);
|
|
|
|
}
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
function onFocus(location: SelectionLocation | null): () => void {
|
|
|
|
return function (this: HTMLElement): void {
|
|
|
|
if (!location) {
|
|
|
|
safePlaceCaretAfterContent(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
restoreSelection(this, location);
|
|
|
|
} catch {
|
|
|
|
safePlaceCaretAfterContent(this);
|
|
|
|
}
|
|
|
|
};
|
2022-01-08 02:46:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function onBlur(this: HTMLElement): void {
|
2022-01-11 23:39:41 +01:00
|
|
|
prepareFocusHandling(this, saveSelection(this));
|
2022-01-08 02:46:01 +01:00
|
|
|
}
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
function prepareFocusHandling(
|
|
|
|
editable: HTMLElement,
|
|
|
|
latestLocation: SelectionLocation | null = null,
|
|
|
|
): void {
|
|
|
|
const removeOnFocus = on(editable, "focus", onFocus(latestLocation), {
|
|
|
|
once: true,
|
|
|
|
});
|
2022-01-08 02:46:01 +01:00
|
|
|
|
|
|
|
locationEvents.push(
|
|
|
|
removeOnFocus,
|
|
|
|
on(editable, "pointerdown", removeOnFocus, { once: true }),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
export function initialFocusHandling(editable: HTMLElement): void {
|
|
|
|
prepareFocusHandling(editable);
|
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
/* Must execute before DOMMirror */
|
|
|
|
export function saveLocation(editable: HTMLElement): { destroy(): void } {
|
|
|
|
const removeOnBlur = on(editable, "blur", onBlur);
|
|
|
|
|
|
|
|
return {
|
|
|
|
destroy() {
|
|
|
|
removeOnBlur();
|
|
|
|
flushLocation();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-16 06:05:35 +01:00
|
|
|
if (isApplePlatform()) {
|
|
|
|
registerShortcut(() => bridgeCommand("paste"), "Control+Shift+V");
|
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
export function preventBuiltinContentEditableShortcuts(editable: HTMLElement): void {
|
|
|
|
for (const keyCombination of ["Control+B", "Control+U", "Control+I", "Control+R"]) {
|
|
|
|
registerShortcut(preventDefault, keyCombination, editable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** API */
|
|
|
|
|
|
|
|
export interface ContentEditableAPI {
|
|
|
|
flushLocation(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentEditableApi: ContentEditableAPI = {
|
|
|
|
flushLocation,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default contentEditableApi;
|