import { FolkShape } from "./folk-shape"; import { css, html } from "./tags"; const styles = css` :host { background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); min-width: 350px; min-height: 400px; } .header { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; background: linear-gradient(135deg, #7c3aed, #8b5cf6); color: white; border-radius: 8px 8px 0 0; font-size: 12px; font-weight: 600; cursor: move; } .header-title { display: flex; align-items: center; gap: 6px; } .note-title { background: transparent; border: none; color: white; font-size: 12px; font-weight: 600; outline: none; width: 150px; } .note-title::placeholder { color: rgba(255, 255, 255, 0.7); } .header-actions { display: flex; gap: 4px; } .header-actions button { background: transparent; border: none; color: white; cursor: pointer; padding: 2px 6px; border-radius: 4px; font-size: 14px; } .header-actions button:hover { background: rgba(255, 255, 255, 0.2); } .content { display: flex; flex-direction: column; height: calc(100% - 36px); } .toolbar { display: flex; align-items: center; gap: 4px; padding: 8px 12px; border-bottom: 1px solid #e2e8f0; flex-wrap: wrap; } .toolbar-btn { padding: 4px 8px; border: none; border-radius: 4px; background: #f1f5f9; cursor: pointer; font-size: 13px; transition: all 0.2s; } .toolbar-btn:hover { background: #e2e8f0; } .toolbar-btn.active { background: #7c3aed; color: white; } .toolbar-divider { width: 1px; height: 20px; background: #e2e8f0; margin: 0 4px; } .mode-toggle { margin-left: auto; display: flex; gap: 2px; background: #f1f5f9; border-radius: 6px; padding: 2px; } .mode-btn { padding: 4px 10px; border: none; border-radius: 4px; background: transparent; cursor: pointer; font-size: 11px; font-weight: 500; color: #64748b; transition: all 0.2s; } .mode-btn.active { background: white; color: #1e293b; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); } .editor-container { flex: 1; display: flex; overflow: hidden; } .editor { flex: 1; padding: 12px 16px; border: none; outline: none; resize: none; font-family: "Monaco", "Consolas", "Courier New", monospace; font-size: 13px; line-height: 1.6; background: #fafafa; } .preview { flex: 1; padding: 12px 16px; overflow-y: auto; font-size: 14px; line-height: 1.7; display: none; } .preview.visible { display: block; } .preview h1 { font-size: 1.5em; margin: 0 0 0.5em; border-bottom: 1px solid #e2e8f0; padding-bottom: 0.3em; } .preview h2 { font-size: 1.3em; margin: 1em 0 0.5em; } .preview h3 { font-size: 1.1em; margin: 1em 0 0.5em; } .preview p { margin: 0.5em 0; } .preview code { background: #f1f5f9; padding: 2px 6px; border-radius: 4px; font-family: "Monaco", "Consolas", monospace; font-size: 0.9em; } .preview pre { background: #1e293b; color: #e2e8f0; padding: 12px 16px; border-radius: 6px; overflow-x: auto; } .preview pre code { background: none; padding: 0; } .preview blockquote { border-left: 4px solid #7c3aed; margin: 0.5em 0; padding: 0.5em 1em; background: #faf5ff; color: #6b21a8; } .preview ul, .preview ol { margin: 0.5em 0; padding-left: 1.5em; } .preview li { margin: 0.25em 0; } .preview a { color: #7c3aed; text-decoration: none; } .preview a:hover { text-decoration: underline; } .preview hr { border: none; border-top: 1px solid #e2e8f0; margin: 1em 0; } .footer { display: flex; align-items: center; justify-content: space-between; padding: 6px 12px; border-top: 1px solid #e2e8f0; font-size: 11px; color: #64748b; } .word-count { display: flex; gap: 12px; } .save-status { display: flex; align-items: center; gap: 4px; } .save-status.saved { color: #10b981; } .save-status.unsaved { color: #f59e0b; } `; declare global { interface HTMLElementTagNameMap { "folk-obs-note": FolkObsNote; } } export class FolkObsNote extends FolkShape { static override tagName = "folk-obs-note"; static { const sheet = new CSSStyleSheet(); const parentRules = Array.from(FolkShape.styles.cssRules) .map((r) => r.cssText) .join("\n"); const childRules = Array.from(styles.cssRules) .map((r) => r.cssText) .join("\n"); sheet.replaceSync(`${parentRules}\n${childRules}`); this.styles = sheet; } #content = ""; #title = "Untitled"; #mode: "edit" | "preview" | "split" = "edit"; #isDirty = false; #lastSaved: Date | null = null; #editor: HTMLTextAreaElement | null = null; #preview: HTMLElement | null = null; #titleInput: HTMLInputElement | null = null; #wordCountEl: HTMLElement | null = null; #saveStatusEl: HTMLElement | null = null; get content() { return this.#content; } set content(value: string) { this.#content = value; if (this.#editor) this.#editor.value = value; this.#updatePreview(); this.#updateWordCount(); } get title() { return this.#title; } set title(value: string) { this.#title = value; if (this.#titleInput) this.#titleInput.value = value; } override createRenderRoot() { const root = super.createRenderRoot(); const wrapper = document.createElement("div"); wrapper.innerHTML = html`
$2");
// Headers
html = html.replace(/^### (.+)$/gm, "$1");
// Links
html = html.replace(
/\[([^\]]+)\]\(([^)]+)\)/g,
'$1'
);
// Blockquotes
html = html.replace(/^> (.+)$/gm, "$1"); // Lists html = html.replace(/^- (.+)$/gm, "
"); html = `
${html}
`; html = html.replace(/<\/p>/g, ""); return html; } #updateWordCount() { if (!this.#wordCountEl) return; const words = this.#content .trim() .split(/\s+/) .filter((w) => w.length > 0).length; const chars = this.#content.length; this.#wordCountEl.innerHTML = ` ${words} words ${chars} characters `; } #updateSaveStatus() { if (!this.#saveStatusEl) return; if (this.#isDirty) { this.#saveStatusEl.className = "save-status unsaved"; this.#saveStatusEl.innerHTML = "\u2022Unsaved"; } else { this.#saveStatusEl.className = "save-status saved"; this.#saveStatusEl.innerHTML = "\u2713Saved"; } } #save() { this.#isDirty = false; this.#lastSaved = new Date(); this.#updateSaveStatus(); this.dispatchEvent( new CustomEvent("save", { detail: { title: this.#title, content: this.#content }, }) ); } #escapeHtml(text: string): string { const div = document.createElement("div"); div.textContent = text; return div.innerHTML; } override toJSON() { return { ...super.toJSON(), type: "folk-obs-note", title: this.title, content: this.content, }; } }