import { getPhrase } from "../../Language";
import { escapeHTML } from "../../StringUtil";
import { dispatchToCkeditor, listenToCkeditor } from "./Event";
-import * as EventHandler from "../../Event/Handler";
-type Payload = {
+export type Payload = {
html: string;
timestamp: number;
};
export function deleteDraft(identifier: string): void {
try {
window.localStorage.removeItem(getLocalStorageKey(identifier));
-
- EventHandler.fire("com.woltlab.wcf.ckeditor5", "deleteDraft", {
- identifier,
- });
} catch {
// We cannot do anything meaningful if this fails.
}
}
-function saveDraft(identifier: string, html: string): void {
+function saveDraft(element: HTMLElement, identifier: string, html: string): void {
if (html === "") {
deleteDraft(identifier);
try {
window.localStorage.setItem(getLocalStorageKey(identifier), JSON.stringify(payload));
- EventHandler.fire("com.woltlab.wcf.ckeditor5", "saveDraft", {
- identifier,
- payload,
- });
+ dispatchToCkeditor(element).autosave(payload);
} catch (e) {
console.warn("Unable to write to the local storage.", e);
}
configuration.autosave = {
save(editor) {
- saveDraft(identifier, editor.data.get());
+ saveDraft(element, identifier, editor.data.get());
return Promise.resolve();
},
import type { UploadMediaEventPayload } from "./Media";
import type { InsertQuoteEventPayload } from "./Quote";
import type { CKEditor5 } from "@woltlab/editor";
+import { Payload } from "WoltLabSuite/Core/Component/Ckeditor/Autosave";
const enum EventNames {
Bbcode = "ckeditor5:bbcode",
SubmitOnEnter = "ckeditor5:submit-on-enter",
UploadAttachment = "ckeditor5:upload-attachment",
UploadMedia = "ckeditor5:upload-media",
+ Autosave = "ckeditor5:autosave",
}
type BbcodeEventPayload = {
bbcode: string;
this.#element.dispatchEvent(new CustomEvent<void>(EventNames.DiscardRecoveredData));
}
+ autosave(payload: Payload): void {
+ this.#element.dispatchEvent(
+ new CustomEvent<Payload>(EventNames.Autosave, {
+ detail: payload,
+ }),
+ );
+ }
+
insertAttachment(payload: InsertAttachmentPayload): void {
this.#element.dispatchEvent(
new CustomEvent<InsertAttachmentPayload>(EventNames.InsertAttachment, {
return this;
}
+
+ autosave(callback: (payload: Payload) => void): this {
+ this.#element.addEventListener(EventNames.Autosave, (event: CustomEvent<Payload>) => {
+ callback(event.detail);
+ });
+
+ return this;
+ }
}
export function dispatchToCkeditor(element: HTMLElement): EventDispatcher {
* @since 6.0
* @woltlabExcludeBundle tiny
*/
-define(["require", "exports", "tslib", "../../Core", "../../Language", "../../StringUtil", "./Event", "../../Event/Handler"], function (require, exports, tslib_1, Core_1, Language_1, StringUtil_1, Event_1, EventHandler) {
+define(["require", "exports", "../../Core", "../../Language", "../../StringUtil", "./Event"], function (require, exports, Core_1, Language_1, StringUtil_1, Event_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeAutosave = exports.setupRestoreDraft = exports.deleteDraft = void 0;
- EventHandler = tslib_1.__importStar(EventHandler);
function getLocalStorageKey(identifier) {
return `${(0, Core_1.getStoragePrefix)()}ckeditor5-${identifier}`;
}
function deleteDraft(identifier) {
try {
window.localStorage.removeItem(getLocalStorageKey(identifier));
- EventHandler.fire("com.woltlab.wcf.ckeditor5", "deleteDraft", {
- identifier,
- });
}
catch {
// We cannot do anything meaningful if this fails.
}
}
exports.deleteDraft = deleteDraft;
- function saveDraft(identifier, html) {
+ function saveDraft(element, identifier, html) {
if (html === "") {
deleteDraft(identifier);
return;
};
try {
window.localStorage.setItem(getLocalStorageKey(identifier), JSON.stringify(payload));
- EventHandler.fire("com.woltlab.wcf.ckeditor5", "saveDraft", {
- identifier,
- payload,
- });
+ (0, Event_1.dispatchToCkeditor)(element).autosave(payload);
}
catch (e) {
console.warn("Unable to write to the local storage.", e);
removeExpiredDrafts();
configuration.autosave = {
save(editor) {
- saveDraft(identifier, editor.data.get());
+ saveDraft(element, identifier, editor.data.get());
return Promise.resolve();
},
waitingTime: 15000,
discardRecoveredData() {
this.#element.dispatchEvent(new CustomEvent("ckeditor5:discard-recovered-data" /* EventNames.DiscardRecoveredData */));
}
+ autosave(payload) {
+ this.#element.dispatchEvent(new CustomEvent("ckeditor5:autosave" /* EventNames.Autosave */, {
+ detail: payload,
+ }));
+ }
insertAttachment(payload) {
this.#element.dispatchEvent(new CustomEvent("ckeditor5:insert-attachment" /* EventNames.InsertAttachment */, {
detail: payload,
});
return this;
}
+ autosave(callback) {
+ this.#element.addEventListener("ckeditor5:autosave" /* EventNames.Autosave */, (event) => {
+ callback(event.detail);
+ });
+ return this;
+ }
}
function dispatchToCkeditor(element) {
return new EventDispatcher(element);