/** * — Collaborative markdown editor with 3-step wizard flow. * * Step 1: Write — full-width markdown editor with format dropdown * Step 2: Preview — full-width flipbook with download/edit actions * Step 3: Publish — centered publish panel (Share/DIY/Order) * * Supports file drag-and-drop, sample content, PDF preview/download, * and real-time collaborative editing via Automerge CRDT. */ import { pubsDraftSchema, pubsDocId } from '../schemas'; import type { PubsDoc } from '../schemas'; import type { DocumentId } from '../../../shared/local-first/document'; import { TourEngine } from '../../../shared/tour-engine'; interface BookFormat { id: string; name: string; widthMm: number; heightMm: number; description: string; minPages: number; maxPages: number; } interface DraftEntry { docId: DocumentId; draftId: string; title: string; updatedAt: number; } const SAMPLE_CONTENT = `# The Commons ## What Are Commons? Commons are shared resources managed by communities. They can be natural resources like forests, fisheries, and water systems, or digital resources like open-source software, Wikipedia, and creative works. The concept of the commons has deep historical roots. In medieval England, common land was shared among villagers for grazing animals, gathering firewood, and growing food. These weren't unmanaged free-for-alls — they operated under sophisticated rules developed over generations. ## Elinor Ostrom's Design Principles Elinor Ostrom, who won the Nobel Prize in Economics in 2009, identified eight design principles for successful commons governance: 1. Clearly defined boundaries 2. Rules adapted to local conditions 3. Collective-choice arrangements 4. Monitoring by community members 5. Graduated sanctions for rule violators 6. Accessible conflict-resolution mechanisms 7. Recognition of the right to organize 8. Nested enterprises for larger-scale resources > The tragedy of the commons is not inevitable. Communities around the world have managed shared resources sustainably for centuries when given the tools and authority to do so. ## The Digital Commons The internet has created entirely new forms of commons. Open-source software, Creative Commons licensing, and collaborative platforms demonstrate that the commons model scales beyond physical resources. --- These ideas matter because they challenge the assumption that only private ownership or government control can manage resources effectively. The commons represent a third way — community governance of shared wealth.`; const SYNC_DEBOUNCE_MS = 800; export class FolkPubsEditor extends HTMLElement { private _formats: BookFormat[] = []; private _spaceSlug = "personal"; private _selectedFormat = "digest"; private _loading = false; private _error: string | null = null; private _pdfUrl: string | null = null; private _pdfInfo: string | null = null; private _pdfPageCount = 0; // ── Wizard view state ── private _view: "write" | "preview" | "publish" = "write"; // ── Dropdown state ── private _formatDropdownOpen = false; private _draftsDropdownOpen = false; // ── Automerge collaborative state ── private _runtime: any = null; private _activeDocId: DocumentId | null = null; private _activeDraftId: string | null = null; private _drafts: DraftEntry[] = []; private _unsubChange: (() => void) | null = null; private _isRemoteUpdate = false; private _syncTimer: ReturnType | null = null; private _syncConnected = false; private _tour!: TourEngine; private static readonly TOUR_STEPS = [ { target: '.content-area', title: "Editor", message: "Write or paste markdown content here. Drag-and-drop text files also works.", advanceOnClick: false }, { target: '.format-dropdown-btn', title: "Format", message: "Choose a pocket-book format — digest, half-letter, A6, and more.", advanceOnClick: false }, { target: '.btn-generate', title: "Generate PDF", message: "Generate a print-ready PDF and advance to the preview step.", advanceOnClick: false }, { target: '.drafts-dropdown-btn', title: "Drafts", message: "Save multiple drafts with real-time collaborative sync.", advanceOnClick: false }, { target: '.btn-zine-gen', title: "Zine Generator", message: "Generate an AI-illustrated 8-page zine — pick a topic, style, and tone, then edit any section before printing.", advanceOnClick: false }, ]; // ── Close dropdowns on outside click ── private _outsideClickHandler = (e: MouseEvent) => { if (!this.shadowRoot) return; const path = e.composedPath(); if (this._formatDropdownOpen) { const dd = this.shadowRoot.querySelector('.format-dropdown'); if (dd && !path.includes(dd)) { this._formatDropdownOpen = false; this.renderDropdowns(); } } if (this._draftsDropdownOpen) { const dd = this.shadowRoot.querySelector('.drafts-dropdown'); if (dd && !path.includes(dd)) { this._draftsDropdownOpen = false; this.renderDropdowns(); } } }; set formats(val: BookFormat[]) { this._formats = val; if (this.shadowRoot) this.render(); } set spaceSlug(val: string) { this._spaceSlug = val; } async connectedCallback() { if (!this.shadowRoot) this.attachShadow({ mode: "open" }); this._tour = new TourEngine( this.shadowRoot!, FolkPubsEditor.TOUR_STEPS, "rpubs_tour_done", () => this.shadowRoot!.host as HTMLElement, ); this.render(); const space = this.getAttribute("space") || ""; this._spaceSlug = space || this._spaceSlug; // Connect to the local-first runtime await this.initRuntime(); if (space === "demo" && !this._activeDocId) { this.loadDemoContent(); } if (!localStorage.getItem("rpubs_tour_done")) { setTimeout(() => this._tour.start(), 1200); } document.addEventListener("click", this._outsideClickHandler); } private async initRuntime() { const runtime = (window as any).__rspaceOfflineRuntime; if (!runtime?.isInitialized) return; this._runtime = runtime; try { const docs = await runtime.subscribeModule('pubs', 'drafts', pubsDraftSchema); this._syncConnected = runtime.isOnline; this._drafts = []; for (const [docId, doc] of docs) { const parts = (docId as string).split(':'); const draftId = parts[3] || ''; this._drafts.push({ docId: docId as DocumentId, draftId, title: (doc as any).draft?.title || 'Untitled', updatedAt: (doc as any).draft?.updatedAt || (doc as any).meta?.createdAt || 0, }); } this._drafts.sort((a, b) => b.updatedAt - a.updatedAt); if (this._drafts.length > 0) { await this.switchDraft(this._drafts[0].docId, this._drafts[0].draftId); } else { await this.createNewDraft(); } } catch (e) { console.error('[folk-pubs-editor] Runtime init failed:', e); } } private async createNewDraft() { if (!this._runtime) return; const draftId = crypto.randomUUID(); const dataSpace = this._runtime.resolveDocSpace?.('pubs') || this._spaceSlug; const docId = pubsDocId(dataSpace, draftId) as DocumentId; const doc = await this._runtime.subscribe(docId, pubsDraftSchema); this._runtime.change(docId, 'Initialize draft', (d: PubsDoc) => { d.meta.spaceSlug = this._spaceSlug; d.meta.createdAt = Date.now(); d.draft.id = draftId; d.draft.createdAt = Date.now(); d.draft.updatedAt = Date.now(); }); this._drafts.unshift({ docId, draftId, title: 'Untitled', updatedAt: Date.now(), }); await this.switchDraft(docId, draftId); } private async switchDraft(docId: DocumentId, draftId: string) { if (this._unsubChange) { this._unsubChange(); this._unsubChange = null; } this._activeDocId = docId; this._activeDraftId = draftId; // Reset to write view on draft switch this._view = "write"; if (this._pdfUrl) { URL.revokeObjectURL(this._pdfUrl); this._pdfUrl = null; this._pdfInfo = null; } if (!this._runtime) return; const doc = await this._runtime.subscribe(docId, pubsDraftSchema); this.populateFromDoc(doc); this._unsubChange = this._runtime.onChange(docId, (updated: any) => { this._isRemoteUpdate = true; this.applyRemoteUpdate(updated); this.updateDraftListEntry(docId, updated); this._isRemoteUpdate = false; }); this.render(); } private populateFromDoc(doc: any) { if (!this.shadowRoot) return; requestAnimationFrame(() => { if (!this.shadowRoot) return; const textarea = this.shadowRoot.querySelector(".content-area") as HTMLTextAreaElement; const titleInput = this.shadowRoot.querySelector(".title-input") as HTMLInputElement; const authorInput = this.shadowRoot.querySelector(".author-input") as HTMLInputElement; if (textarea && doc.content != null) textarea.value = doc.content; if (titleInput && doc.draft?.title != null) titleInput.value = doc.draft.title; if (authorInput && doc.draft?.author != null) authorInput.value = doc.draft.author; if (doc.draft?.format) { this._selectedFormat = doc.draft.format; } }); } private applyRemoteUpdate(doc: any) { if (!this.shadowRoot) return; const textarea = this.shadowRoot.querySelector(".content-area") as HTMLTextAreaElement; const titleInput = this.shadowRoot.querySelector(".title-input") as HTMLInputElement; const authorInput = this.shadowRoot.querySelector(".author-input") as HTMLInputElement; if (textarea && doc.content != null && textarea.value !== doc.content) { const start = textarea.selectionStart; const end = textarea.selectionEnd; textarea.value = doc.content; textarea.selectionStart = Math.min(start, textarea.value.length); textarea.selectionEnd = Math.min(end, textarea.value.length); } if (titleInput && doc.draft?.title != null && titleInput.value !== doc.draft.title) { titleInput.value = doc.draft.title; } if (authorInput && doc.draft?.author != null && authorInput.value !== doc.draft.author) { authorInput.value = doc.draft.author; } if (doc.draft?.format && doc.draft.format !== this._selectedFormat) { this._selectedFormat = doc.draft.format; } } private updateDraftListEntry(docId: DocumentId, doc: any) { const entry = this._drafts.find((d) => d.docId === docId); if (entry) { entry.title = doc.draft?.title || 'Untitled'; entry.updatedAt = doc.draft?.updatedAt || Date.now(); } } private syncContentToDoc(content: string) { if (!this._runtime || !this._activeDocId || this._isRemoteUpdate) return; if (this._syncTimer) clearTimeout(this._syncTimer); this._syncTimer = setTimeout(() => { this._runtime.change(this._activeDocId!, 'Update content', (d: PubsDoc) => { d.content = content; d.draft.updatedAt = Date.now(); }); }, SYNC_DEBOUNCE_MS); } private syncMetaToDoc(field: 'title' | 'author' | 'format', value: string) { if (!this._runtime || !this._activeDocId || this._isRemoteUpdate) return; this._runtime.change(this._activeDocId!, `Update ${field}`, (d: PubsDoc) => { (d.draft as any)[field] = value; d.draft.updatedAt = Date.now(); }); if (field === 'title') { const entry = this._drafts.find((d) => d.docId === this._activeDocId); if (entry) { entry.title = value || 'Untitled'; entry.updatedAt = Date.now(); } } } private loadDemoContent() { requestAnimationFrame(() => { if (!this.shadowRoot) return; const textarea = this.shadowRoot.querySelector(".content-area") as HTMLTextAreaElement; const titleInput = this.shadowRoot.querySelector(".title-input") as HTMLInputElement; const authorInput = this.shadowRoot.querySelector(".author-input") as HTMLInputElement; if (textarea) textarea.value = SAMPLE_CONTENT; if (titleInput) titleInput.value = "The Commons"; if (authorInput) authorInput.value = "rSpace Community"; }); } disconnectedCallback() { if (this._pdfUrl) URL.revokeObjectURL(this._pdfUrl); if (this._unsubChange) this._unsubChange(); if (this._syncTimer) clearTimeout(this._syncTimer); document.removeEventListener("click", this._outsideClickHandler); } private renderDropdowns() { if (!this.shadowRoot) return; // Format dropdown panel const formatPanel = this.shadowRoot.querySelector('.format-dropdown-panel') as HTMLElement; if (formatPanel) formatPanel.hidden = !this._formatDropdownOpen; // Drafts dropdown panel const draftsPanel = this.shadowRoot.querySelector('.drafts-dropdown-panel') as HTMLElement; if (draftsPanel) draftsPanel.hidden = !this._draftsDropdownOpen; } private getWordCount(): number { if (!this.shadowRoot) return 0; const textarea = this.shadowRoot.querySelector(".content-area") as HTMLTextAreaElement; if (!textarea || !textarea.value.trim()) return 0; return textarea.value.trim().split(/\s+/).length; } private getContent(): string { if (!this.shadowRoot) return ''; const textarea = this.shadowRoot.querySelector(".content-area") as HTMLTextAreaElement; return textarea?.value || ''; } private formatTime(ts: number): string { if (!ts) return ''; const d = new Date(ts); const now = new Date(); if (d.toDateString() === now.toDateString()) { return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); } return d.toLocaleDateString([], { month: 'short', day: 'numeric' }); } private render() { if (!this.shadowRoot) return; const currentFormat = this._formats.find(f => f.id === this._selectedFormat); const formatLabel = currentFormat ? currentFormat.name : this._selectedFormat; this.shadowRoot.innerHTML = ` ${this.getStyles()}
${this._runtime ? `
` : ''} ${this._syncConnected ? 'Synced' : ''} 📰 Zine
${this._pdfInfo ? `${this._pdfInfo}` : ''}
${this._view === 'write' ? this.renderWriteStep() : ''} ${this._view === 'preview' ? this.renderPreviewStep() : ''} ${this._view === 'publish' ? this.renderPublishStep() : ''}
`; this.bindEvents(); this._tour.renderOverlay(); // Populate fields from current doc after render if (this._runtime && this._activeDocId) { const doc = this._runtime.get(this._activeDocId); if (doc) this.populateFromDoc(doc); } } private renderWriteStep(): string { return `
${this.escapeHtml(this._formats.find(f => f.id === this._selectedFormat)?.name || this._selectedFormat)}
${this._error ? `
${this.escapeHtml(this._error)}
` : ''}
`; } private renderPreviewStep(): string { return `
Download PDF
${this._pdfInfo || ''}
`; } private renderPublishStep(): string { return `
`; } startTour() { this._tour.start(); } private bindEvents() { if (!this.shadowRoot) return; // Tour this.shadowRoot.querySelector(".btn-tour-trigger")?.addEventListener("click", () => this.startTour()); // Step navigation this.shadowRoot.querySelectorAll(".step[data-step]").forEach(btn => { btn.addEventListener("click", () => { const step = (btn as HTMLElement).dataset.step as "write" | "preview" | "publish"; if (step === "write") { this._view = "write"; this.render(); } else if (step === "preview" && this._pdfUrl) { this._view = "preview"; this.render(); } else if (step === "publish" && this._pdfUrl) { this._view = "publish"; this.render(); } }); }); // Back navigation buttons this.shadowRoot.querySelector('[data-action="back-to-write"]')?.addEventListener("click", () => { this._view = "write"; this.render(); }); this.shadowRoot.querySelector('[data-action="back-to-preview"]')?.addEventListener("click", () => { this._view = "preview"; this.render(); }); this.shadowRoot.querySelector('.btn-publish-next')?.addEventListener("click", () => { this._view = "publish"; this.render(); }); // Fullscreen toggle this.shadowRoot.querySelector('[data-action="fullscreen-toggle"]')?.addEventListener("click", () => { const flipbook = this.shadowRoot!.querySelector("folk-pubs-flipbook"); if (!flipbook) return; if (document.fullscreenElement) { document.exitFullscreen(); } else { flipbook.requestFullscreen().catch(() => {}); } }); // Format dropdown this.shadowRoot.querySelector('.format-dropdown-btn')?.addEventListener("click", (e) => { e.stopPropagation(); this._formatDropdownOpen = !this._formatDropdownOpen; this._draftsDropdownOpen = false; this.renderDropdowns(); }); this.shadowRoot.querySelectorAll('.format-option').forEach(btn => { btn.addEventListener("click", () => { this._selectedFormat = (btn as HTMLElement).dataset.format!; this._formatDropdownOpen = false; this.syncMetaToDoc('format', this._selectedFormat); // Clear previous PDF on format change if (this._pdfUrl) { URL.revokeObjectURL(this._pdfUrl); this._pdfUrl = null; this._pdfInfo = null; this._view = "write"; } this._error = null; this.render(); }); }); // Drafts dropdown this.shadowRoot.querySelector('.drafts-dropdown-btn')?.addEventListener("click", (e) => { e.stopPropagation(); this._draftsDropdownOpen = !this._draftsDropdownOpen; this._formatDropdownOpen = false; this.renderDropdowns(); }); this.shadowRoot.querySelectorAll('.draft-option:not(.new-draft-option)').forEach(btn => { btn.addEventListener("click", () => { const el = btn as HTMLElement; const docId = el.dataset.docId as DocumentId; const draftId = el.dataset.draftId!; this._draftsDropdownOpen = false; if (docId !== this._activeDocId) { this.switchDraft(docId, draftId); } else { this.renderDropdowns(); } }); }); this.shadowRoot.querySelector('.new-draft-option')?.addEventListener("click", () => { this._draftsDropdownOpen = false; this.createNewDraft(); }); const textarea = this.shadowRoot.querySelector(".content-area") as HTMLTextAreaElement; const titleInput = this.shadowRoot.querySelector(".title-input") as HTMLInputElement; const authorInput = this.shadowRoot.querySelector(".author-input") as HTMLInputElement; const generateBtn = this.shadowRoot.querySelector(".btn-generate") as HTMLButtonElement; const sampleBtn = this.shadowRoot.querySelector(".btn-sample"); const fileInput = this.shadowRoot.querySelector('input[type="file"]') as HTMLInputElement; // Content sync + word count update textarea?.addEventListener("input", () => { this.syncContentToDoc(textarea.value); this.updateWordCount(); }); // Update word count on initial render if (textarea) { requestAnimationFrame(() => this.updateWordCount()); } // Title/Author sync titleInput?.addEventListener("input", () => { this.syncMetaToDoc('title', titleInput.value); }); authorInput?.addEventListener("input", () => { this.syncMetaToDoc('author', authorInput.value); }); // Sample content sampleBtn?.addEventListener("click", () => { if (!textarea) return; textarea.value = SAMPLE_CONTENT; if (titleInput) titleInput.value = ""; if (authorInput) authorInput.value = ""; this.syncContentToDoc(SAMPLE_CONTENT); this.syncMetaToDoc('title', ''); this.syncMetaToDoc('author', ''); this.updateWordCount(); }); // File upload fileInput?.addEventListener("change", () => { const file = fileInput.files?.[0]; if (file && textarea) { const reader = new FileReader(); reader.onload = () => { const text = reader.result as string; textarea.value = text; this.syncContentToDoc(text); this.updateWordCount(); }; reader.readAsText(file); } }); // Drag-and-drop textarea?.addEventListener("dragover", (e) => { e.preventDefault(); textarea.classList.add("dragover"); }); textarea?.addEventListener("dragleave", () => { textarea.classList.remove("dragover"); }); textarea?.addEventListener("drop", (e) => { e.preventDefault(); textarea.classList.remove("dragover"); const file = (e as DragEvent).dataTransfer?.files[0]; if (file && (file.type.startsWith("text/") || file.name.match(/\.(md|txt|markdown)$/))) { const reader = new FileReader(); reader.onload = () => { const text = reader.result as string; textarea.value = text; this.syncContentToDoc(text); this.updateWordCount(); }; reader.readAsText(file); } }); // Generate PDF generateBtn?.addEventListener("click", async () => { const content = textarea?.value?.trim(); if (!content) { this._error = "Please enter some content first."; this.render(); return; } this._loading = true; this._error = null; if (this._pdfUrl) URL.revokeObjectURL(this._pdfUrl); this._pdfUrl = null; this.render(); try { const res = await fetch(`/${this._spaceSlug}/rpubs/api/generate`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ content, title: titleInput?.value?.trim() || undefined, author: authorInput?.value?.trim() || undefined, format: this._selectedFormat, }), }); if (!res.ok) { const err = await res.json(); throw new Error(err.error || "Generation failed"); } const blob = await res.blob(); const pageCount = res.headers.get("X-Page-Count") || "?"; const format = this._formats.find((f) => f.id === this._selectedFormat); this._pdfUrl = URL.createObjectURL(blob); this._pdfPageCount = parseInt(pageCount) || 0; this._pdfInfo = `${pageCount} pages \u00B7 ${format?.name || this._selectedFormat}`; this._loading = false; // Auto-advance to preview this._view = "preview"; this.render(); } catch (e: any) { this._loading = false; this._error = e.message; this.render(); } }); } private updateWordCount() { if (!this.shadowRoot) return; const el = this.shadowRoot.querySelector('.word-count'); if (!el) return; const count = this.getWordCount(); el.textContent = count > 0 ? `${count} words` : ''; } private getStyles(): string { return ``; } private escapeHtml(s: string): string { return s.replace(/&/g, "&").replace(//g, ">"); } } customElements.define("folk-pubs-editor", FolkPubsEditor);