(["draft", this.draftKey])?.text
: undefined;
}
if ((changedProperties.has("weavy") || changedProperties.has("draftKey")) && this.weavy) {
if (this.draftKey) {
await this.draftQuery.trackQuery(getDraftOptions(this.weavy, this.draftKey));
} else {
this.draftQuery.untrackQuery();
this.draftText = undefined;
}
}
if (!this.draftQuery.result.isPending) {
this.draftText = this.draftQuery.result.data?.text;
}
if (changedProperties.has("draftText") && !this.draftText && this.fileContent) {
// Set initial text when draft is removed
this.text = this.fileContent;
}
}
/**
* Persist draft to storage if draft mode is enabled.
*
* Saves editor text, embeds, poll options and ongoing file uploads to localStorage.
*
* @internal
*/
protected saveDraft() {
if (!this.weavy || !this.draft || !this.draftKey) return;
if (this.hasChanged() && !this.draftQuery.result.isPending) {
const draft: EditorDraftType = {
text: this.text,
updatedAt: new Date().toUTCString(),
};
saveDraft(this.weavy, this.draftKey, draft);
this.draftText = undefined;
} else if (this.fileContent !== undefined && this.draftText === this.fileContent) {
removeDraft(this.weavy, this.draftKey);
}
}
/**
* Submit the editor content by dispatching a `submit` event.
*
* Validates that there is content to submit and that no uploads/embeds are still processing.
*
* @internal
*/
protected submit() {
const text = this.text?.trim() ?? "";
if (!text) {
return;
}
const saveEvent: FileEditorSaveEventType = new (CustomEvent as NamedEvent)("save", {
detail: { text },
bubbles: true,
composed: true,
});
this.dispatchEvent(saveEvent);
}
/**
* Reset the editor state after a successful submit.
*
* Clears editor content, removes pending uploads from the query cache and deletes the stored draft.
*
* @internal
*/
protected resetEditor() {
if (this.weavy && this.draftKey) {
removeDraft(this.weavy, this.draftKey);
}
this.clearEditor();
}
/**
* Clear the editor UI and internal state (text, embeds, attachments, metadata).
*
* @internal
*/
protected clearEditor() {
this.text = "";
this.metadata = {};
}
/**
* Checks if the editor has a dirty internal state (text, embeds, attachments, metadata).
*
* @internal
*/
protected hasChanged() {
return this.fileContent !== undefined && this.text !== this.fileContent;
}
/**
* Throttled drafting saver to minimize writes.
*
* @internal
*/
private throttledDrafting = throttle(
() => {
this.saveDraft();
},
500,
{ leading: true, trailing: true },
);
/**
* Prevents trailing writes from recreating a draft after Publish or Discard removes the editor.
*/
override disconnectedCallback() {
this.throttledDrafting.cancel();
super.disconnectedCallback();
}
/**
* CodeMirror editor
* @internal
*/
@state()
protected get editor() {
return this.editorRef.value?.editor;
}
protected renderEditor(): (TemplateResult | typeof nothing)[] | TemplateResult | typeof nothing {
return html` this.requestUpdate("editor")}
@change=${(e: EditorChangeEventType) => {
this.text = e.detail.text;
}}
@select=${() => this.requestUpdate("editor")}
@submit=${(e: CustomEvent) => {
e.preventDefault();
this.submit();
}}
>`;
}
protected renderLiveView() {
return this.markdownIsLoading
? html` `
: unsafeHTML(this.liveRender?.toString());
}
protected renderTopSlot(): (TemplateResult | typeof nothing)[] | TemplateResult | typeof nothing {
return nothing;
}
protected renderMiddleSlot(): (TemplateResult | typeof nothing)[] | TemplateResult | typeof nothing {
const cssParts = {
"wy-light": this.mode === "light",
"wy-code": this.appearance === "code",
"wy-document": this.appearance === "document" || this.appearance === "text",
"wy-content-text": this.appearance === "text",
"wy-content-text-edit": this.appearance === "text",
};
const anyParts = Array.from(Object.values(cssParts)).some(onlyValues);
return anyParts
? html`${this.showLive ? this.renderLiveView() : this.renderEditor()}
`
: this.renderEditor();
}
/**
* Render the bottom slot which aggregates lists (polls, meetings, file uploads, attachments, embeds).
*
* @internal
*/
protected renderBottomSlot(): (TemplateResult | typeof nothing)[] | TemplateResult | typeof nothing {
const isMarkdown = this.file?.media_type === "text/markdown";
const inputDisabled = this.showLive;
return [
isMarkdown
? html`
`
: nothing,
this.renderLists(),
];
}
/**
* Render the lists section (poll options, meetings, file uploads, attachments, embeds).
*
* @internal
*/
protected renderLists(): TemplateResult | typeof nothing {
return nothing;
}
protected override render() {
return html`
${this.renderTopSlot()} ${this.renderMiddleSlot()} ${this.renderBottomSlot()}
`;
}
override updated(changedProperties: PropertyValueMap) {
super.updated(changedProperties);
const isMarkdown = this.file?.media_type === "text/markdown";
if (this.editorRef.value) {
this.#resolveEditor?.(this.editorRef.value);
}
if (
(changedProperties.has("text") || changedProperties.has("file") || changedProperties.has("showLive")) &&
isMarkdown &&
this.showLive
) {
if (!markdownIsLoading) {
if (markdown) {
this.liveRender = this.text !== undefined ? markdown.parse(this.text, { bytes: false }) : undefined;
} else {
markdownIsLoading = true;
void import("@logue/markdown-wasm").then(async (markdownModule) => {
await markdownModule.ready();
markdown = markdownModule;
markdownIsLoading = false;
this.liveRender = this.text !== undefined ? markdown.parse(this.text, { bytes: false }) : undefined;
});
this.requestUpdate("markdownIsLoading");
}
}
}
if (changedProperties.has("text")) {
this.throttledDrafting();
}
}
}