feat(editor): add lossless bounded CSV table editing

This commit is contained in:
Anya Tokugawa committed 2026-11-08 20:58:19 +09:00
1 parent bd80ff6a0c
commit 2d6ceac0d7
19 files changed
+520 -40

No files matched your search

+28 -10
View File
@@ -6,8 +6,12 @@ window.PagesEditor = {
this.mount = document.getElementById('editor-mount')
if (!this.editor || !this.preview || !this.mount) return
this.baseRevision = this.mount.dataset.baseRevision || ''
this.baseBody = document.getElementById('editor-base')?.value || ''
this.initialText = this.editor.value
// HTML textarea parsing and CodeMirror's default input both normalize
// CRLF. CSV receives an escaped JS string and uses LF-only splitting.
this.csvInitialSource = this.mount.dataset.csv === 'true' ? window.PagesCSVSource : undefined
delete window.PagesCSVSource
this.baseBody = this.csvInitialSource ?? (document.getElementById('editor-base')?.value || '')
this.initialText = this.csvInitialSource ?? this.editor.value
this.owner = this.mount.dataset.owner || ''
this.serial = 0
this.paused = false
@@ -65,17 +69,21 @@ window.PagesEditor = {
exists:['指定言語には既に本文があります。編集として開いてください','This language already exists. Open it for editing'], history:['過去のリビジョンを復元','Restore a previous revision'],
signin:['再ログイン(別タブ)','Sign in again (new tab)'], insert:['挿入','Insert'], invalid:['言語タグを確認してください','Check the language tag'], identity:['別アカウントに切り替わっています。元のアカウントで開き直してください','Account changed. Reopen using the original account'],
incompatible:['ファイル形式が変更されています。下書きを退避して開き直してください','File type changed. Download your draft and reopen'], older:['さらに古い履歴','Older revisions'],
csvlimit:['CSVは256 KiB以内で保存してください。下書きは保持しています','CSV must be at most 256 KiB. Your draft is retained'],
children:['子ページ一覧','Children'], table:['表','Table'], figure:['画像とキャプション','Image + caption'], note:['補足','Note'], code:['コードブロック','Code block'], quote:['引用','Quote'],
csv:['CSVの表を埋め込む','Embed CSV table'],
protected:['Markdown で表せない HTML は {=html} ブロックに保持します。ブロック内も編集でき、保存・プレビュー時に HTML として扱います。','HTML not representable in Markdown is retained in editable {=html} blocks and rendered as HTML on save / preview.'],
}
return text[key]?.[this.ja ? 0 : 1] || key
},
value(id) { return document.getElementById(id)?.value || '' },
format() {return this.value('edit-type')},
text() {return window.PagesWikiCodeMirror?.state.doc.toString() ?? this.editor.value},
text() {return window.PagesWikiCodeMirror?.state.doc.toString() ?? this.csvInitialSource ?? this.editor.value},
status(key) {
const status = document.getElementById('editor-save-status')
if (status) {status.textContent = this.t(key); status.dataset.state = key}
const csvStatus=this.preview?.querySelector('[data-csv-save-status]')
if (csvStatus) csvStatus.textContent=this.t(key)
},
keyPrefix() {return 'pageswiki.draft.v1:'+JSON.stringify([this.mount.dataset.domain,this.owner,this.value('object-uuid'),this.value('page-language')||'und'])+':'},
expireDrafts() {
@@ -180,6 +188,7 @@ window.PagesEditor = {
}
},
async canonical(text = this.text(), format = this.format()) {
if (this.mount.dataset.csv === 'true' && (new TextEncoder().encode(text).length > 256*1024 || text.includes('\0'))) throw {kind:'csvlimit'}
if (text === this.initialText && format === this.initialFormat) return this.baseBody
if (format !== 'markdown') return text
const data = await this.post('/_/api/v1/render/markdown',{markdown:text,authoring:true})
@@ -192,6 +201,7 @@ window.PagesEditor = {
const controller = new AbortController()
this.previewController = controller
const text = this.text()
if (this.mount.dataset.csv === 'true' && window.PagesCSVEditor) {window.PagesCSVEditor.render(this,text); return}
if (this.format() === 'code' || this.format() === 'plain') {this.preview.classList.add('code-preview'); this.preview.textContent=text; return}
try {
const data = await this.post('/_/api/v1/render/markdown',{
@@ -270,7 +280,9 @@ window.PagesEditor = {
this.dirty=false; this.paused=false; this.clearDraft(); this.status('saved'); return
}
if (serial !== this.serial) {this.status('remote'); this.persist(); return}
const merged = PagesEditorCore.merge(this.baseBody,local,remote.body)
// Text ranges do not describe CSV record boundaries. Review competing
// table edits rather than silently combining rows/columns incorrectly.
const merged = this.mount.dataset.csv === 'true' ? null : PagesEditorCore.merge(this.baseBody,local,remote.body)
const apply = result => {
this.baseRevision=remote.revision_uuid; this.baseBody=remote.body
document.getElementById('page-filename').value=remote.filename||''
@@ -301,12 +313,13 @@ window.PagesEditor = {
const oldFormat = this.format()
document.getElementById('edit-type').value=format
const status=document.getElementById('editor-format-status')
if (status) status.textContent=format
if (status) status.textContent=this.mount.dataset.csv === 'true' ? 'CSV' : format
this.editor.value=text
if (this.mount.dataset.csv === 'true') this.csvInitialSource=text
if (oldFormat !== format && window.PagesWikiCodeMirror) {
window.PagesWikiCodeMirror.destroy()
window.PagesWikiCodeMirror = window.PagesWikiEditor.create(this.mount,text,format,{
keymap:this.mount.dataset.keymap,syntax:this.mount.dataset.syntax==='true',autocomplete:this.mount.dataset.autocomplete==='true',
keymap:this.mount.dataset.keymap,syntax:this.mount.dataset.syntax==='true',autocomplete:this.mount.dataset.autocomplete==='true',preserveLineEndings:this.mount.dataset.csv==='true',
})
} else if (window.PagesWikiCodeMirror) {
const cm=window.PagesWikiCodeMirror
@@ -368,19 +381,24 @@ window.PagesEditor = {
const {dialog,content,button}=this.dialog('tools')
this.paragraph(content,this.t('privacy'))
const select=document.createElement('select'); select.setAttribute('aria-label',this.t('insert'))
this.paragraph(content,this.t('protected'))
const csvFile=this.mount.dataset.csv==='true'
if (!csvFile) this.paragraph(content,this.t('protected'))
const parts=[['children','{{children}}'],['table','<table>\n<thead><tr><th>Column</th></tr></thead>\n<tbody><tr><td>Value</td></tr></tbody>\n</table>'],['figure','<figure><img src="/_/obj/sha256:HASH" alt="Description"><figcaption>Caption</figcaption></figure>'],['note','<aside class="note"><p>Note</p></aside>'],['code','<pre><code class="language-text">code</code></pre>'],['quote','<blockquote><p>Quote</p></blockquote>']]
parts.push(['csv','{{csv:/data/table.csv range=A1:H200 header=true}}'])
parts.forEach(([label],i) => {const option=document.createElement('option'); option.value=i; option.textContent=this.t(label); select.append(option)})
content.append(select)
select.hidden=csvFile
const login=document.createElement('a'); login.href='/_/auth/login?return='+encodeURIComponent(location.pathname+location.search); login.target='_blank'; login.rel='noopener'; login.textContent=this.t('signin'); content.append(login)
button('insert',() => {
const insert=button('insert',() => {
if (csvFile) return
const part=parts[Number(select.value)]
const source=this.format()==='markdown' && part[0]!=='children' ? '```{=html}\n'+part[1]+'\n```' : part[1]
const source=this.format()==='markdown' && !['children','csv'].includes(part[0]) ? '```{=html}\n'+part[1]+'\n```' : part[1]
const text='\n\n'+source+'\n\n'
const cm=window.PagesWikiCodeMirror
if (cm) cm.dispatch(cm.state.replaceSelection(text)); else this.setText(this.text()+text)
dialog.close(); cm?.focus()
})
insert.hidden=csvFile
button('export',() => this.download())
button('discard',() => {
this.clearDraft()
@@ -429,7 +447,7 @@ window.PagesEditor = {
const serial=this.serial
const old=await this.state({revision:select.value})
if (serial!==this.serial || this.saving) return
if (!old.editable) throw {kind:'incompatible'}
if (!old.editable || this.mount.dataset.csv === 'true' && (old.content_type||'').split(';')[0] !== this.value('page-content-type').split(';')[0]) throw {kind:'incompatible'}
this.setText(this.format()==='markdown'?old.markdown:old.body)
this.persist(); dialog.close()
})