feat(editor): support atomic multi-cell CSV paste
This commit is contained in:
1 parent
2d6ceac0d7
commit
09afb438c9
7 files changed
+88
-11
No files matched your search
@@ -50,9 +50,11 @@ IPアドレス管理(IPAM)、DNSゾーン/レコード管理、機器・
|
||||
|
||||
初期CSV macroは実装済みです。`{{csv:/data/items.csv range=A1:H200 header=true}}` を認証済みProjectionで展開し、`rev=` で同じObjectのRevisionへ固定できます。現在のPath閲覧権限を毎回検査し、外部URL、匿名公開、asset指定、共有キャッシュはこの段階に含めません。入力256 KiB・UTF-8、表示200行×32列、走査2,000行・100列、セル16 KiB、Projection当たり4種類のCSVまでに制限します。引用符・改行・BOMに対応し、セルはHTML escapeしてmacroや数式を実行しません。処理不能時は内容を漏らさない警告に置き換えます。Authoringでは元のmacroを保持します。
|
||||
|
||||
既存CSVの編集は `/e/-/{path}` の組み込みエディタで行います。デスクトップはソースと表を並べ、700px以下では表を全幅表示してソースへ切り替えます。セル編集、行・列追加/削除、20操作までの表のUndo/Redoに対応。表は200行・32列・セル16 KiB以内を25行ずつ描画し、先頭行を自動でヘッダ扱いしません。矩形でない行の欠落セルを勝手に保存せず、列追加時だけ明示的に空セルを補います。全体256 KiB・UTF-8以内を対象とし、超過・不正形式の表を部分編集可能にして保存することはありません。表上限を超えても全体上限内ならソースで編集できます。
|
||||
既存CSVの編集は `/e/-/{path}` の組み込みエディタで行います。デスクトップはソースと表を並べ、700px以下では表を全幅表示してソースへ切り替えます。セル編集、行・列追加/削除、20操作までの表のUndo/Redoに対応。表は200行・32列・セル16 KiB以内を25行ずつ描画し、先頭行を自動でヘッダ扱いしません。矩形でない行の欠落セルは表示だけでは補わず、列追加・貼り付けで必要になった部分だけ空セルを補います。全体256 KiB・UTF-8以内を対象とし、超過・不正形式の表を部分編集可能にして保存することはありません。表上限を超えても全体上限内ならソースで編集できます。
|
||||
|
||||
CSV正本をHTMLへ変換せず、未変更fieldの引用符、BOM、混在LF/CRLFを保持します。セル内のHTML、macro、数式は実行しません。表編集も既存の下書き・認証切れ検出・言語別Revision CAS・履歴復元を使用し、他の保存と競合したCSVは文字列の自動マージをせず比較画面へ送ります。原文と編集済み表の両方を別正本として保存しません。DBスキーマ変更はありません。CSV新規作成ウィザード、TSV/文字コード変換、大容量表のwindow編集、複数セル貼付、列型/数式、ODSは残件です。
|
||||
表計算ソフトからの複数セル貼り付けは、フォーカス中のセルを左上として反映します。クリップボードの `text/plain` にタブまたは改行があればタブ区切り表として解析し、引用符内の改行・タブ・二重引用符を保持します。カンマは列区切りにせずセル値として扱い、単一行の通常テキストはセル内への貼り付けです。複数行を一つのセルへ貼る場合は引用符で囲むかソースを編集します。HTML形式のクリップボードは使いません。必要な行・セルの拡張を含めて1操作でUndo/Redoでき、構文・行列・セル・変換後CSV全体の上限を検査してから一括反映します。失敗時は部分反映せず、既存のUndo履歴も変更しません。貼り付け範囲外のセルと改行形式は維持します。
|
||||
|
||||
CSV正本をHTMLへ変換せず、未変更fieldの引用符、BOM、混在LF/CRLFを保持します。セル内のHTML、macro、数式は実行しません。表編集も既存の下書き・認証切れ検出・言語別Revision CAS・履歴復元を使用し、他の保存と競合したCSVは文字列の自動マージをせず比較画面へ送ります。原文と編集済み表の両方を別正本として保存しません。DBスキーマ変更はありません。CSV新規作成ウィザード、TSVファイル/文字コード変換、大容量表のwindow編集、矩形選択/範囲コピー、列型/数式、ODSは残件です。
|
||||
|
||||
CSVをPage本文へ複製せず、認可済みの別Documentをテーブルとして投影します。共有キャッシュを将来追加する際は、Path、Revision(またはasset hash)、列・行上限、表示モードと権限を考慮します。macroは表示時にboundedなProjectionを作るだけで、結果HTMLを正本へ逆流させません。CSV Editorは元Revisionを編集し、表投影とは別の操作・競合・権限として扱います。
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ P0は認可・整合性・復旧の安全条件、P1は日常利用の回復性
|
||||
| SHARE-01 / P2 | 未実装:委譲share URL、期限/失効/監査、説明可能な権限総覧 | AUTH-01、PUB-01。login/adapter/federation資格情報と別目的、単一Page投影、caveat・依存物の失効を検証 | [公開](publication.md)、[Schutzaria](schutzaria.md) |
|
||||
| COLLAB-01 / P2 | 一部実装:Page等の変更購読、最近閲覧/編集、集約通知。コメント/反応/評価は未実装 | OUT-01。現在権限で再投影、private履歴のopt-in/保持。inline commentはRevision変更時の孤立・再接続を扱う | [拡張](extensions.md) |
|
||||
| BLOG-01 / P2 | 一部実装:安全なtDiary import、最近コメント、遠隔ブログ射影 | COLLAB-01、遠隔はFED-01。importは再開/重複抑止/dry-run。同一instance射影を再実装しない | [ブログ・日記](blog-diary.md) |
|
||||
| CSV-01 / P2 | viewer・有界CSV macro・既存CSVの表編集を実装。残件:新規作成UI、大容量/複数セル編集、匿名投影、asset指定、ODS境界 | BOM/改行保持、25行描画、行列・セル・総量上限、Revision CAS、権限、競合確認/履歴復元fixture。表示HTMLをCSV正本へ逆流させない | [表設計](extensions.md) |
|
||||
| CSV-01 / P2 | viewer・有界CSV macro・既存CSVの表編集/複数セル貼り付けを実装。残件:新規作成UI、大容量編集、矩形選択/範囲コピー、匿名投影、asset指定、ODS境界 | BOM/改行保持、25行描画、行列・セル・総量上限、貼り付けの一括検証/Undo、Revision CAS、権限、競合確認/履歴復元fixture。表示HTMLをCSV正本へ逆流させない | [表設計](extensions.md) |
|
||||
| DIAGRAM-01 / P2 | 一部実装:nwdiag/Draw.io provider、slide preview/editor | RENDER-01。安全なSVG・include拒否・renderer版固定、ticket/CAS保存、print。既存Excalidraw/Krokiとは分ける | [Kunstzeal](kunstzeal.md) |
|
||||
| RECIPE-01 / P2 | 未実装:構造化レシピmetadataとPage投影の初期slice | 材料・数量・未知単位を保持、人数変更は派生値、Asset認可、版競合、出典・公開確認 | [料理レシピ](recipe-management.md) |
|
||||
|
||||
|
||||
+17
@@ -44,6 +44,23 @@
|
||||
cell(1,1).focus();button('−→').click();assert(PagesCSV.parse(e.text()).rows[0].cells.length===2,'Column delete failed');
|
||||
button('↶').click();cell(1,1).focus();button('−↧').click();assert(PagesCSV.parse(e.text()).rows.length===28,'Row delete failed');
|
||||
button('↶').click();
|
||||
// Spreadsheet clipboard paste is one atomic undo step, including new rows.
|
||||
e.setText(original);await e.renderPreview();
|
||||
const paste=(r,c,text)=>{const data=new DataTransfer();data.setData('text/plain',text);data.setData('text/html','<img src=x onerror=alert(1)>');const target=cell(r,c);target.focus();const event=new ClipboardEvent('paste',{clipboardData:data,bubbles:true,cancelable:true});target.dispatchEvent(event);return event;};
|
||||
assert(paste(1,0,'first\t"multi\nline"\r\n<img src=x onerror=alert(1)>\t=1+1\r\n').defaultPrevented,'Table paste not intercepted');
|
||||
const pasted=e.text();assert(PagesCSV.parse(pasted).rows.length===3,'Paste did not grow rows');
|
||||
assert(cell(1,1).value==='multi\nline','Quoted multiline clipboard broken');
|
||||
assert(!document.querySelector('.csv-edit-pane img'),'Clipboard HTML executed');
|
||||
assert(document.activeElement===cell(1,0),'Paste focus lost');
|
||||
assert(await e.saveNow(),'Pasted CSV save failed');assert((await e.state()).body===pasted,'Pasted CSV not persisted exactly');e.paused=true;
|
||||
button('↶').click();assert(e.text()===original,'Paste not undone atomically');
|
||||
button('↷').click();assert(e.text()===pasted,'Paste redo changed bytes');
|
||||
assert(paste(1,0,'valid\tdata\n"unfinished').defaultPrevented,'Malformed paste fell through');
|
||||
assert(e.text()===pasted,'Malformed paste partially applied');
|
||||
assert(paste(1,0,'a\n'.repeat(200)).defaultPrevented,'Oversize paste fell through');
|
||||
assert(e.text()===pasted,'Oversize paste partially applied');
|
||||
assert(!paste(1,0,'ordinary, text').defaultPrevented,'Single-cell text paste intercepted');
|
||||
button('↶').click();assert(e.text()===original,'Rejected paste polluted undo');
|
||||
const css=getComputedStyle(document.querySelector('.csv-edit-toolbar'));assert(css.color!==css.backgroundColor,'Toolbar lacks contrast');
|
||||
clearTimeout(e.autosaveTimer);clearInterval(e.stateTimer);e.paused=true;
|
||||
document.body.dataset.vimReady='true';
|
||||
|
||||
+26
-5
@@ -2,7 +2,7 @@
|
||||
'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) {
|
||||
function parseRecords(source,separator) {
|
||||
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=[];
|
||||
@@ -11,14 +11,14 @@
|
||||
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');
|
||||
if(!closed||i<source.length&&![separator,'\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++];}
|
||||
while(i<source.length&&![separator,'\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;}
|
||||
if(source[i]===separator){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++;}
|
||||
@@ -27,6 +27,7 @@
|
||||
}
|
||||
return {source,bom,lineEnding:lineEnding||'\r\n',rows};
|
||||
}
|
||||
function parse(source){return parseRecords(source,',');}
|
||||
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('');
|
||||
@@ -54,5 +55,25 @@
|
||||
}else throw Error('action');
|
||||
return serialize(m);
|
||||
}
|
||||
root.PagesCSV={parse,edit,serialize,limits};
|
||||
function paste(source,row,column,clipboard){
|
||||
// Clipboard tables use tabs, not commas. Parse and validate the entire
|
||||
// operation before returning anything to the editor or its undo history.
|
||||
const m=parse(source),incoming=parseRecords(clipboard,'\t');
|
||||
if(!Number.isInteger(row)||!Number.isInteger(column)||row<0||column<0||row>=limits.rows||column>=limits.columns)throw Error('cell');
|
||||
if(!incoming.rows.length)return source;
|
||||
const width=Math.max(...incoming.rows.map(r=>r.cells.length));
|
||||
if(row+incoming.rows.length>limits.rows||column+width>limits.columns)throw Error('size');
|
||||
const blank=()=>({value:'',raw:'',original:''});
|
||||
while(m.rows.length<row+incoming.rows.length){
|
||||
const last=m.rows.at(-1);if(last&&!last.end)last.end=m.lineEnding;
|
||||
m.rows.push({cells:[blank()],end:m.lineEnding});
|
||||
}
|
||||
for(let r=0;r<incoming.rows.length;r++){
|
||||
const target=m.rows[row+r].cells,fields=incoming.rows[r].cells;
|
||||
while(target.length<column+fields.length)target.push(blank());
|
||||
for(let c=0;c<fields.length;c++)target[column+c].value=fields[c].value;
|
||||
}
|
||||
return serialize(m);
|
||||
}
|
||||
root.PagesCSV={parse,edit,paste,serialize,limits};
|
||||
})(typeof window==='undefined'?globalThis:window);
|
||||
@@ -29,3 +29,34 @@ test('Quotes, HTML, formulas and newlines remain inert CSV data',()=>{
|
||||
assert.ok(source.endsWith(',unchanged\r\n'));
|
||||
}
|
||||
});
|
||||
test('Tab-separated paste preserves untouched CSV bytes and treats commas as data',()=>{
|
||||
const source='\ufeff"keep",old,last\r\n"unchanged",x,y\n';
|
||||
assert.equal(csv.paste(source,0,1,'日本語,値\t"a""b"\r\nnext\tfinal\r\n'),
|
||||
'\ufeff"keep","日本語,値","a""b"\r\n"unchanged",next,final\n');
|
||||
assert.equal(csv.paste(source,0,0,'keep\told\tlast\nunchanged\tx\ty\n'),source);
|
||||
assert.equal(csv.paste('a,b\n',0,0,'"line1\r\nline2"\t"tab\tvalue"\n'),'"line1\nline2",tab\tvalue\n');
|
||||
});
|
||||
test('Paste grows only required rows and cells, preserving ragged data',()=>{
|
||||
assert.equal(csv.paste('a,b\nc\nz\n',0,1,'x\ty\nz'),'a,x,y\nc,z\nz\n');
|
||||
assert.equal(csv.paste('a,b',0,1,'x\ty\nz\tw'),'a,x,y\r\n,z,w\r\n');
|
||||
assert.equal(csv.paste('',0,0,'a\tb\n'),'a,b\r\n');
|
||||
assert.equal(csv.paste('a,b\n',0,0,'\t\n'),',\n');
|
||||
assert.equal(csv.paste('a,b\n',0,0,''),'a,b\n');
|
||||
});
|
||||
test('Paste validates both input and final CSV size without partial writes',()=>{
|
||||
const source='original,untouched\r\n';
|
||||
for(const text of ['a\tb\n"unfinished','a\0b\tc','x'.repeat(16385)+'\tb','a\n'.repeat(201),'a\t'.repeat(32)])assert.throws(()=>csv.paste(source,0,0,text));
|
||||
for(const [row,col] of [[-1,0],[0,-1],[0.5,0],[0,NaN],[200,0],[0,32]])assert.throws(()=>csv.paste(source,row,col,'a\tb'));
|
||||
assert.throws(()=>csv.paste(source,199,0,'a\nb'));
|
||||
assert.throws(()=>csv.paste(source,0,31,'a\tb'));
|
||||
// Valid individual cells can still exceed the total document limit.
|
||||
const big=Array(16).fill('x'.repeat(16000)).join(',');
|
||||
assert.throws(()=>csv.paste(big,0,16,'y'.repeat(16000)));
|
||||
// Quoting comma-heavy clipboard data expands the resulting CSV.
|
||||
assert.throws(()=>csv.paste('',0,0,Array(16).fill(','.repeat(16383)).join('\t')));
|
||||
assert.equal(source,'original,untouched\r\n');
|
||||
});
|
||||
test('Pasted HTML, macros and formulas remain values rather than executable content',()=>{
|
||||
const values=['<img src=x onerror=alert(1)>','{{children}}','=SUM(A1:A9)'];
|
||||
assert.deepEqual(csv.parse(csv.paste('',0,0,values.join('\t'))).rows[0].cells.map(c=>c.value),values);
|
||||
});
|
||||
@@ -44,6 +44,11 @@
|
||||
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);
|
||||
input.addEventListener('paste',event=>{
|
||||
const text=event.clipboardData?.getData('text/plain');
|
||||
if(typeof text!=='string'||!/[\t\r\n]/.test(text))return;
|
||||
event.preventDefault();this.change('paste',r,c,text);
|
||||
});
|
||||
}
|
||||
tr.append(td);
|
||||
}
|
||||
@@ -55,7 +60,7 @@
|
||||
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;
|
||||
const next=action==='paste'?PagesCSV.paste(source,row,column,value):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.
|
||||
@@ -64,10 +69,11 @@
|
||||
}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);
|
||||
if(action==='paste')this.editor.preview.querySelector(`textarea[data-row="${row}"][data-column="${column}"]`)?.focus();
|
||||
}
|
||||
}catch{
|
||||
if(input)input.value=this.model.rows[row].cells[column].value;
|
||||
this.message.textContent=this.t('Edit exceeds CSV limits. Source retained.','CSVの上限を超えます。元の内容を保持しました。');
|
||||
this.message.textContent=action==='paste'?this.t('Paste rejected: invalid tab-separated data or CSV limits exceeded. Source retained.','貼り付けできません。タブ区切りデータの形式またはCSVの上限を確認してください。元の内容を保持しました。'):this.t('Edit exceeds CSV limits. Source retained.','CSVの上限を超えます。元の内容を保持しました。');
|
||||
}
|
||||
}
|
||||
history(forward){
|
||||
|
||||
@@ -274,7 +274,7 @@ Ln 1, Col 1
|
||||
</main>
|
||||
|
||||
<script src="/static/js/editor-core.js?v=priority-ux-1"></script>
|
||||
{{ if .CSVEditor }}<script>window.PagesCSVSource = {{ .Body }};</script><script src="/static/js/csv-core.js?v=1"></script><script src="/static/js/csv-editor.js?v=1"></script>{{ end }}
|
||||
{{ if .CSVEditor }}<script>window.PagesCSVSource = {{ .Body }};</script><script src="/static/js/csv-core.js?v=2"></script><script src="/static/js/csv-editor.js?v=2"></script>{{ end }}
|
||||
<script src="/static/js/editor.js?v=csv-edit-1"></script>
|
||||
<script src="/static/js/path-actions.js"></script>
|
||||
|
||||
|
||||
Reference in new issue
Block a user