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

+58
View File
@@ -0,0 +1,58 @@
(function(root) {
'use strict';
const limits={bytes:256*1024,rows:200,columns:32,cellBytes:16*1024};
const bytes=s=>new TextEncoder().encode(s).length;
function parse(source) {
if(typeof source!=='string'||bytes(source)>limits.bytes||source.includes('\0'))throw Error('size');
const bom=source.startsWith('\ufeff')?'\ufeff':'',rows=[];
let i=bom.length,lineEnding='',cells=[];
while(i<source.length||cells.length){
const start=i;let value='';
if(source[i]==='"'){
i++;let closed=false;
while(i<source.length){const c=source[i++];if(c==='"'){if(source[i]==='"'){value+='"';i++;}else{closed=true;break;}}else value+=c;}
if(!closed||i<source.length&&![',','\r','\n'].includes(source[i]))throw Error('syntax');
value=value.replace(/\r\n/g,'\n');
}else{
while(i<source.length&&![',','\r','\n'].includes(source[i])){if(source[i]==='"')throw Error('syntax');value+=source[i++];}
}
if(bytes(value)>limits.cellBytes||cells.length>=limits.columns)throw Error('size');
cells.push({value,raw:source.slice(start,i),original:value});
if(source[i]===','){i++;continue;}
let end='';
if(source[i]==='\r'){if(source[i+1]!=='\n')throw Error('syntax');end='\r\n';i+=2;}
else if(source[i]==='\n'){end='\n';i++;}
if(!lineEnding&&end)lineEnding=end;
rows.push({cells,end});cells=[];if(rows.length>limits.rows)throw Error('size');
}
return {source,bom,lineEnding:lineEnding||'\r\n',rows};
}
function encode(value){return /[",\r\n]/.test(value)||/^\s/.test(value)?'"'+value.replace(/"/g,'""')+'"':value;}
function serialize(model){
const source=model.bom+model.rows.map(row=>{const record=row.cells.map(c=>c.value===c.original?c.raw:encode(c.value)).join(',');return record+(row.end||(!record?model.lineEnding:''));}).join('');
parse(source);return source;
}
function edit(source,action,row=0,column=0,value=''){
const m=parse(source),blank=()=>({value:'',raw:'',original:''});
if(action==='cell'){
if(!m.rows[row]?.cells[column]||typeof value!=='string')throw Error('cell');
m.rows[row].cells[column].value=value;
}else if(action==='add-row'){
const last=m.rows.at(-1),cols=Math.max(1,...m.rows.map(r=>r.cells.length));
if(last&&!last.end)last.end=m.lineEnding;
// A newline is necessary to distinguish a blank final row from no row.
m.rows.push({cells:Array.from({length:cols},blank),end:m.lineEnding});
}else if(action==='add-column'){
if(!m.rows.length)m.rows.push({cells:[blank()],end:m.lineEnding});
const cols=Math.max(...m.rows.map(r=>r.cells.length));
for(const r of m.rows){while(r.cells.length<cols)r.cells.push(blank());r.cells.push(blank());}
}else if(action==='delete-row'){
if(!m.rows[row])throw Error('row');m.rows.splice(row,1);
}else if(action==='delete-column'){
if(!m.rows.some(r=>r.cells[column]))throw Error('column');
for(const r of m.rows){r.cells.splice(column,1);if(!r.cells.length)r.cells.push(blank());}
}else throw Error('action');
return serialize(m);
}
root.PagesCSV={parse,edit,serialize,limits};
})(typeof window==='undefined'?globalThis:window);
+31
View File
@@ -0,0 +1,31 @@
const test=require('node:test'),assert=require('node:assert/strict');
require('./csv-core.js');const csv=globalThis.PagesCSV;
test('CSV parsing preserves untouched bytes and logical field values',()=>{
for(const source of ['', '\ufeff', 'a', 'a,', '\n', '""', 'a\r\nb\n', '\ufeff"name",description\r\n日本語,"a,b\r\nnext"\r\n', '"a""b",=1+1\n'])assert.equal(csv.serialize(csv.parse(source)),source);
const s='\ufeff"name",description\r\n日本語,"a,b\r\nnext"\r\n';
assert.equal(csv.parse(s).rows[1].cells[1].value,'a,b\nnext');
assert.equal(csv.edit(s,'cell',1,0,'新しい,名前'),'\ufeff"name",description\r\n"新しい,名前","a,b\r\nnext"\r\n');
assert.equal(csv.edit(s,'cell',1,1,'a,b\nnext'),s);
assert.equal(csv.edit('a','cell',0,0,''),'\r\n');
});
test('Row and column operations preserve fields; empty and ragged data are explicit',()=>{
assert.equal(csv.edit('a,b\nc\n','add-column'),'a,b,\nc,,\n');
assert.equal(csv.edit('a,b\nc\n','delete-column',0,1),'a\nc\n');
assert.equal(csv.edit('a,b\r\n"c",d','delete-row',0),'"c",d');
assert.equal(csv.edit('a,b','add-row'),'a,b\r\n,\r\n');
assert.equal(csv.parse(csv.edit('','add-row')).rows.length,1);
assert.equal(csv.edit('a\n','delete-row',0),'');
});
test('Malformed and excessive data fail without truncation',()=>{
for(const source of ['"unfinished','a"b','"a"x','a\rb','a\0b','x'.repeat(256*1024+1),'x'.repeat(16385), 'a,'.repeat(32), 'a\n'.repeat(201)])assert.throws(()=>csv.parse(source));
assert.throws(()=>csv.edit('a\n'.repeat(200),'add-row'));
assert.throws(()=>csv.edit('a','cell',0,0,'x'.repeat(16385)));
assert.throws(()=>csv.edit('a','cell',2,0,'bad'));
});
test('Quotes, HTML, formulas and newlines remain inert CSV data',()=>{
for(const value of ['"','a,b','<img src=x onerror=alert(1)>','{{children}}','=SUM(A1:A9)',' leading','日本語\n次の行','a\r\nb']){
const source=csv.edit('old,unchanged\r\n','cell',0,0,value);
assert.equal(csv.parse(source).rows[0].cells[0].value,value.replace(/\r\n/g,'\n'));
assert.ok(source.endsWith(',unchanged\r\n'));
}
});
+81
View File
@@ -0,0 +1,81 @@
(() => {
'use strict';
const views=new WeakMap(),pageSize=25;
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
class TableEditor {
constructor(editor){this.editor=editor;this.page=0;this.selected=null;this.last=null;this.undo=[];this.redo=[];}
t(en,ja){return this.editor.ja?ja:en;}
render(text){
if(this.last===text)return;
this.last=text;this.selected=null;
const pane=this.editor.preview;pane.classList.remove('code-preview');pane.classList.add('csv-edit-pane');pane.replaceChildren();
try{this.model=PagesCSV.parse(text);}catch{
const error=el('p',this.t('Table unavailable: check CSV syntax or limits (200 rows, 32 columns, 16 KiB per cell). Edit the source; no data was truncated.','表を表示できません。CSVの構文・上限(200行、32列、セル16 KiB)を確認してください。ソースで編集できます。データは切り捨てていません。'));
error.role='status';pane.append(error);return;
}
const toolbar=el('div');toolbar.className='csv-edit-toolbar';
const button=(icon,en,ja,fn)=>{const b=el('button',icon);b.type='button';b.title=b.ariaLabel=this.t(en,ja);b.onclick=fn;toolbar.append(b);return b;};
const save=button('','Save','保存',async()=>{save.disabled=true;try{await this.editor.saveNow();}finally{save.disabled=false;}});
save.innerHTML='<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 4h12l2 2v14H5zM8 4v6h8V4M8 20v-6h8v6"/></svg>';
button('+↧','Add row','行を追加',()=>this.change('add-row'));
button('+→','Add column','列を追加',()=>this.change('add-column'));
this.deleteRow=button('−↧','Delete selected row','選択行を削除',()=>this.change('delete-row'));
this.deleteColumn=button('−→','Delete selected column','選択列を削除',()=>this.change('delete-column'));
this.deleteRow.disabled=this.deleteColumn.disabled=true;
const undo=button('↶','Undo table edit','表の編集を元に戻す',()=>this.history(false));undo.disabled=!this.undo.length;
const redo=button('↷','Redo table edit','表の編集をやり直す',()=>this.history(true));redo.disabled=!this.redo.length;
this.undoButton=undo;this.redoButton=redo;
this.page=Math.max(0,Math.min(this.page,Math.ceil(this.model.rows.length/pageSize)-1));
const previous=button('‹','Previous rows','前の行',()=>{this.page--;this.last=null;this.render(this.editor.text());});previous.disabled=this.page===0;
const first=this.page*pageSize,end=Math.min(first+pageSize,this.model.rows.length);
this.position=el('output',this.model.rows.length?`${first+1}–${end} / ${this.model.rows.length}`:'0');toolbar.append(this.position);
const next=button('›','Next rows','次の行',()=>{this.page++;this.last=null;this.render(this.editor.text());});next.disabled=end>=this.model.rows.length;
this.message=el('span');this.message.role='status';toolbar.append(this.message);
const saved=el('span',document.getElementById('editor-save-status')?.textContent||'');saved.dataset.csvSaveStatus='';saved.role='status';toolbar.append(saved);pane.append(toolbar);
const scroll=el('div');scroll.className='csv-edit-scroll';const table=el('table');table.setAttribute('aria-label',this.t('CSV cells','CSVのセル'));
const head=el('thead'),hr=el('tr');hr.append(el('th','#'));
const width=Math.max(0,...this.model.rows.map(r=>r.cells.length));
for(let c=0;c<width;c++){const th=el('th',String(c+1));th.scope='col';hr.append(th);}head.append(hr);table.append(head);
const body=el('tbody');
for(let r=first;r<end;r++){
const tr=el('tr'),number=el('th',String(r+1));number.scope='row';tr.append(number);
for(let c=0;c<width;c++){
const td=el('td'),cell=this.model.rows[r].cells[c];
if(cell){const input=el('textarea');input.value=cell.value;input.rows=cell.value.includes('\n')?3:1;input.spellcheck=false;input.dataset.row=r;input.dataset.column=c;input.ariaLabel=this.t(`Row ${r+1}, column ${c+1}`,`${r+1}行 ${c+1}列`);
input.addEventListener('focus',()=>{this.selected=[r,c];this.deleteRow.disabled=this.deleteColumn.disabled=false;});
input.addEventListener('input',()=>this.change('cell',r,c,input.value,input));td.append(input);
}
tr.append(td);
}
body.append(tr);
}
table.append(body);scroll.append(table);pane.append(scroll);
}
change(action,row=this.selected?.[0],column=this.selected?.[1],value,input){
const source=this.editor.text();
if(source!==this.last){this.last=null;this.render(source);return;}
try{
const next=PagesCSV.edit(source,action,row,column,value);if(next===source)return;
this.undo.push({before:source,after:next});if(this.undo.length>20)this.undo.shift();this.redo=[];
if(action==='cell'){
// Keep the active textarea and its caret; delayed preview is a no-op.
this.model=PagesCSV.parse(next);this.last=next;this.editor.setText(next);
this.undoButton.disabled=false;this.redoButton.disabled=true;this.message.textContent='';
}else{
if(action==='add-row')this.page=Math.floor((PagesCSV.parse(next).rows.length-1)/pageSize);
this.last=null;this.editor.setText(next);this.render(next);
}
}catch{
if(input)input.value=this.model.rows[row].cells[column].value;
this.message.textContent=this.t('Edit exceeds CSV limits. Source retained.','CSVの上限を超えます。元の内容を保持しました。');
}
}
history(forward){
const from=forward?this.redo:this.undo,to=forward?this.undo:this.redo;if(!from.length)return;
const item=from.at(-1),current=this.editor.text();
if(current!==(forward?item.before:item.after)){this.message.textContent=this.t('Source changed. Use source editor undo.','ソースが変更されています。ソース側の取り消しを使ってください。');return;}
from.pop();to.push(item);const text=forward?item.after:item.before;this.last=null;this.editor.setText(text);this.render(text);
}
}
window.PagesCSVEditor={render(editor,text){let view=views.get(editor);if(!view){view=new TableEditor(editor);views.set(editor,view);}view.render(text);}};
})();
+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()
})