hide1 commited on
Commit
044ac85
·
verified ·
1 Parent(s): 9305a6d

Upload index.html

Browse files
Files changed (1) hide show
  1. index.html +236 -21
index.html CHANGED
@@ -57,6 +57,12 @@ input[type=color]{width:39px;height:32px;border:1px solid var(--line);border-rad
57
  #hex{color:var(--sub);font:13px Consolas,monospace}
58
  @media(max-width:800px){.workspace{grid-template-columns:minmax(0,1fr)}.results{position:static}.preset-grid{grid-template-columns:repeat(3,minmax(0,1fr))}main{padding-top:15px}.brand{gap:7px}.tools{gap:4px}}
59
  @media(max-height:650px){.results{position:static}}
 
 
 
 
 
 
60
  </style>
61
  </head>
62
  <body><main>
@@ -67,11 +73,10 @@ input[type=color]{width:39px;height:32px;border:1px solid var(--line);border-rad
67
  <section aria-label="文字の設定">
68
  <fieldset><legend>文字と配置</legend>
69
  <div class="fields">
70
- <label>親文字<input id="base" type="text" value="紅蓮"></label>
71
- <label>ルビ<input id="reading" type="text" value="グレン" placeholder="ひらがな・カタカナ"></label>
72
- <label>ルビの配置<select id="align"><option value="spread">親文字の幅に均等配置</option><option value="center">中央にまとめる</option><option value="groups">グループごと</option></select></label>
73
  <label><span class="range-label">ルビの大きさ <output id="sizeValue" for="size">20%</output></span><input id="size" type="range" min="10" max="50" value="20"></label>
74
- <p id="groupHint" class="hint full" hidden>同じ位置を「|」で区切ります。例:親文字「紅|蓮」、ルビ「グ|レン」。読みのない組は空欄にできます。</p>
75
  </div></fieldset>
76
  <fieldset><legend>書体と装飾</legend>
77
  <div class="fields">
@@ -106,6 +111,9 @@ input[type=color]{width:39px;height:32px;border:1px solid var(--line);border-rad
106
  <textarea id="prompt" lang="en" aria-labelledby="promptTitle" readonly placeholder="親文字を入力してください"></textarea>
107
  <div class="actions"><button class="primary" id="copy" type="button">プロンプトをコピー</button><span role="status" id="notice"></span></div>
108
  </section></section></div></main>
 
 
 
109
  <script>
110
  "use strict";
111
  const el = id => document.getElementById(id);
@@ -144,15 +152,176 @@ const design = {...defaults};
144
  const flags = ["bold","italic","underline","strike","includeRuby","linked"];
145
  const designFields = ["style","effect","outline",...flags];
146
  const symbolNames = {"★":"a five-pointed star","♡":"an outline heart","♪":"a musical note","✦":"a four-pointed sparkle","→":"a right arrow","🔥":"a flame","🌸":"a cherry blossom","✨":"sparkles","🎵":"musical notes","❤️":"a red heart"};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  function settings() {
148
- const base = el("base").value.trim(), reading = el("reading").value.trim();
149
- const align = el("align").value, grouped = align === "groups";
150
- const bases = grouped ? base.split(/[||]/) : [base];
151
- const readings = grouped && reading ? reading.split(/[||]/) : [reading];
152
- const groups = bases.map((text,i) => ({text, reading:readings[i] || ""}));
153
- const error = grouped && (bases.some(t=>!t.trim()) || (reading && readings.length !== bases.length))
154
- ? "親文字とルビの区切り数を揃え、親文字は各組に入力してください。" : "";
155
- return {...design, styleSpec:styles[design.style], base:bases.join(""), reading:readings.join(""), groups, align, error,
156
  ratio:Number(el("size").value)/100, rubyInk:design.linked ? design.ink : design.ruby, ornament:el("ornament").value.trim()};
157
  }
158
  function finishText(s) {
@@ -180,17 +349,28 @@ function decorations(s) {
180
  function rubyText(s,reading) {
181
  const spread=s.align==="spread" && characters(reading).length>1;
182
  return "Use small "+colorText(s.rubyInk)+" furigana directly above the main lettering, about "+Math.round(s.ratio*100)+"% of its height. "+
183
- (spread ? "Space the glyphs evenly from the left edge to the right edge of the title, keeping their natural proportions." : "Center the reading over its corresponding text with natural character spacing.");
184
  }
185
  function ornamentText(s) {
186
  const desc = symbolNames[s.ornament] || "a decorative mark shaped like “"+s.ornament+"”";
187
  return "Place "+desc+" to the right of the title as a separate ornament.";
188
  }
 
 
 
 
 
 
 
 
 
 
 
189
  function plainPrompt(s) {
190
  let text = "Write “"+s.base+"”";
191
  if (s.reading) {
192
- if (s.align==="groups" && s.groups.length>1) {
193
- const pairs=s.groups.filter(g=>g.reading).map(g=>"“"+g.reading+"” above “"+g.text+"”");
194
  text+=" with small furigana: "+pairs.join(", ");
195
  } else {
196
  const spread=s.align==="spread" && characters(s.reading).length>1;
@@ -207,8 +387,8 @@ function makePrompt(s) {
207
  const lines=["Render the horizontal title “"+s.base+"” in "+colorText(s.ink)+face+"."];
208
  if (d) lines.push(d);
209
  if (s.reading) {
210
- if (s.align==="groups") {
211
- for (const g of s.groups) if(g.reading) lines.push("Place “"+g.reading+"” directly above “"+g.text+"”.");
212
  } else lines.push("The reading is “"+s.reading+"”.");
213
  lines.push(rubyText(s,s.reading));
214
  if (d) lines.push(s.includeRuby ? "Repeat the title treatment on the reading at its smaller scale." : "Keep the reading plain and upright.");
@@ -226,6 +406,25 @@ function measure(text,size,s,decorated) {
226
  }
227
  // All geometry is in lettering units, shared by the preview and Layout exporter.
228
  function composition(s) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  const size=160, items=[];
230
  let cursor=0;
231
  for (const g of s.groups) {
@@ -392,16 +591,32 @@ for (const [hex,label] of palette){
392
  }
393
  for(const symbol of Object.keys(symbolNames)){
394
  const b=document.createElement("button"); b.type="button"; b.textContent=symbol; b.setAttribute("aria-label",symbol+"を挿入");
395
- b.onclick=()=>{const input=el(el("symbolTarget").value); input.setRangeText(symbol,input.selectionStart,input.selectionEnd,"end"); input.focus(); update();};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
  el("symbols").append(b);
397
  }
398
  function update(){
399
  const s=settings(), format=el("format").value, useLayout=format==="layout";
400
  const w=useLayout ? el("width").valueAsNumber : canvas.clientWidth, h=useLayout ? el("height").valueAsNumber : canvas.clientHeight;
401
  const validSize=n=>Number.isSafeInteger(n) && n>0 && n%16===0;
402
- const error=(s.base && s.error) || (useLayout && (!validSize(w)||!validSize(h)) ? "生成幅と高さは16の倍数で入力してください。" : "");
403
- const scene=s.base && !error ? composition(s) : null;
404
- el("dimensions").hidden=!useLayout; el("groupHint").hidden=s.align!=="groups";
405
  el("error").textContent=error; el("error").hidden=!error; el("sizeValue").value=el("size").value+"%";
406
  let prompt="";
407
  if (scene) {
 
57
  #hex{color:var(--sub);font:13px Consolas,monospace}
58
  @media(max-width:800px){.workspace{grid-template-columns:minmax(0,1fr)}.results{position:static}.preset-grid{grid-template-columns:repeat(3,minmax(0,1fr))}main{padding-top:15px}.brand{gap:7px}.tools{gap:4px}}
59
  @media(max-height:650px){.results{position:static}}
60
+ #editor{padding:18px 16px;min-height:118px;margin:6px 0;white-space:pre-wrap;overflow-wrap:anywhere;background:var(--surface);border:1px solid var(--line);border-radius:6px;font:500 23px/2 Meiryo,sans-serif}
61
+ #editor ruby{white-space:nowrap;cursor:pointer;ruby-align:space-between} #editor.center ruby{ruby-align:center}
62
+ #editor rt{font-size:var(--ruby-size, .5em);line-height:1;user-select:none}
63
+ #rubyBox{position:fixed;z-index:5;width:min(310px,calc(100vw - 16px));padding:12px;background:var(--surface);border:1px solid var(--line);border-radius:8px;box-shadow:0 4px 18px #0003}
64
+ #rubyBox{left:clamp(8px,var(--anchor-x),max(8px,100% - 318px));overflow:auto}
65
+ #sampleRuby{font-size:22px;padding:8px 0;line-height:1.4} #rubyTarget{overflow-wrap:anywhere;max-height:55px;overflow:auto} #rubyBox .actions{display:flex;gap:6px;flex-wrap:wrap}
66
  </style>
67
  </head>
68
  <body><main>
 
73
  <section aria-label="文字の設定">
74
  <fieldset><legend>文字と配置</legend>
75
  <div class="fields">
76
+ <div class="full"><label id="bodyLabel">本文</label><div id="editor" role="textbox" aria-multiline="true" aria-labelledby="bodyLabel" aria-describedby="editorHelp" contenteditable="true" spellcheck="false">こんにちは、<ruby contenteditable="false">今日<rt>きょう</rt></ruby>はいい<ruby contenteditable="false">天気<rt>てんき</rt></ruby>ですね。</div>
77
+ <p id="editorHelp" class="hint">本文を入力し、読みを付けたい文字をドラッグしてください。付けたルビはクリックして編集できます。</p></div>
78
+ <label>ルビの配置<select id="align"><option value="spread">親文字の幅に均等配置</option><option value="center">中央にまとめる</option></select></label>
79
  <label><span class="range-label">ルビの大きさ <output id="sizeValue" for="size">20%</output></span><input id="size" type="range" min="10" max="50" value="20"></label>
 
80
  </div></fieldset>
81
  <fieldset><legend>書体と装飾</legend>
82
  <div class="fields">
 
111
  <textarea id="prompt" lang="en" aria-labelledby="promptTitle" readonly placeholder="親文字を入力してください"></textarea>
112
  <div class="actions"><button class="primary" id="copy" type="button">プロンプトをコピー</button><span role="status" id="notice"></span></div>
113
  </section></section></div></main>
114
+ <div id="rubyBox" role="dialog" aria-labelledby="rubyTarget" hidden>
115
+ <label for="reading" id="rubyTarget"></label><div id="sampleRuby" aria-hidden="true"><ruby><span></span><rt></rt></ruby></div><input id="reading" type="text" placeholder="例:きょう" autocomplete="off">
116
+ <div class="actions"><button type="button" id="applyRuby" class="primary">適用</button><button type="button" id="removeRuby">ルビを外す</button><button type="button" id="cancelRuby">閉じる</button></div></div>
117
  <script>
118
  "use strict";
119
  const el = id => document.getElementById(id);
 
152
  const flags = ["bold","italic","underline","strike","includeRuby","linked"];
153
  const designFields = ["style","effect","outline",...flags];
154
  const symbolNames = {"★":"a five-pointed star","♡":"an outline heart","♪":"a musical note","✦":"a four-pointed sparkle","→":"a right arrow","🔥":"a flame","🌸":"a cherry blossom","✨":"sparkles","🎵":"musical notes","❤️":"a red heart"};
155
+ const editor = el("editor"), rubyBox = el("rubyBox"), readingInput = el("reading");
156
+ let editing = null, lastCaret = null;
157
+ function readGroups(root) {
158
+ const groups = [];
159
+ function append(text, reading = "") {
160
+ if (!text) return;
161
+ const last = groups.at(-1);
162
+ if (!reading && last && !last.reading) last.text += text;
163
+ else groups.push({text, reading});
164
+ }
165
+ function visit(node) {
166
+ if (node.nodeType === Node.TEXT_NODE) return append(node.data);
167
+ if (node.nodeName === "RT") return;
168
+ if (node.nodeName === "RUBY") {
169
+ const base = Array.from(node.childNodes).filter(n => n.nodeName !== "RT").map(n => n.textContent).join("");
170
+ return append(base, node.querySelector("rt")?.textContent || "");
171
+ }
172
+ if (node.nodeName === "BR") {
173
+ if (node.parentNode.childNodes.length > 1 || node.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) append("\n");
174
+ return;
175
+ }
176
+ if (["DIV","P"].includes(node.nodeName) && node.previousSibling) append("\n");
177
+ node.childNodes.forEach(visit);
178
+ }
179
+ root.childNodes.forEach(visit);
180
+ return groups;
181
+ }
182
+ function editorRange() {
183
+ const sel = window.getSelection();
184
+ if (!sel.rangeCount) return null;
185
+ const range = sel.getRangeAt(0);
186
+ return editor.contains(range.commonAncestorContainer) ? range.cloneRange() : null;
187
+ }
188
+ function selectRange(range) {
189
+ editor.focus({preventScroll:true});
190
+ const sel = window.getSelection();
191
+ sel.removeAllRanges(); sel.addRange(range);
192
+ }
193
+ function closeEditor() {
194
+ editing = null;
195
+ rubyBox.hidden = true;
196
+ }
197
+ function positionBox() {
198
+ if (!editing) return;
199
+ const rect = editing.range.getBoundingClientRect();
200
+ const below = innerHeight-rect.bottom >= rect.top;
201
+ const inset = Math.max(8, (below ? rect.bottom : innerHeight-rect.top)+5);
202
+ const style = rubyBox.style;
203
+ style.setProperty("--anchor-x",rect.left+"px");
204
+ style.top = below ? inset+"px" : "auto";
205
+ style.bottom = below ? "auto" : inset+"px";
206
+ style.maxHeight = Math.max(0,innerHeight-inset-8)+"px";
207
+ }
208
+ function openEditor(range, ruby = null) {
209
+ const text = ruby ? Array.from(ruby.childNodes).filter(n=>n.nodeName!=="RT").map(n=>n.textContent).join("") : range.toString();
210
+ const crossesNodes = range.startContainer !== range.endContainer || range.startContainer.nodeType!==Node.TEXT_NODE;
211
+ if (!text.trim() || /[\r\n]/.test(text) || (!ruby && crossesNodes && range.cloneContents().querySelector("ruby,br,div,p"))) return closeEditor();
212
+ editing = {range, text, ruby};
213
+ positionBox();
214
+ el("rubyTarget").textContent = "「"+text+"」の読み";
215
+ readingInput.value = ruby?.querySelector("rt")?.textContent || "";
216
+ el("sampleRuby").querySelector("span").textContent=text;
217
+ el("sampleRuby").querySelector("rt").textContent=readingInput.value;
218
+ el("removeRuby").hidden = !ruby;
219
+ el("applyRuby").disabled = !readingInput.value.trim();
220
+ rubyBox.hidden = false;
221
+ }
222
+ function pickRange(event) {
223
+ const range = editorRange();
224
+ if (range) lastCaret = range;
225
+ const ruby = event.target.closest?.("ruby");
226
+ if (ruby) {
227
+ const target = document.createRange();
228
+ target.selectNode(ruby); openEditor(target, ruby);
229
+ } else if (range && !range.collapsed) openEditor(range);
230
+ else closeEditor();
231
+ }
232
+ editor.addEventListener("pointerup", pickRange);
233
+ editor.addEventListener("click", e => { if (e.detail===0) pickRange(e); });
234
+ editor.addEventListener("keyup", e => { if(e.shiftKey && e.key.startsWith("Arrow")) pickRange(e); });
235
+ function enclosingRuby(node) {
236
+ return (node.nodeType===Node.TEXT_NODE ? node.parentElement : node)?.closest("ruby");
237
+ }
238
+ editor.addEventListener("keydown", e => {
239
+ if (e.isComposing || e.keyCode===229) return;
240
+ const range=editorRange();
241
+ if (!range) return;
242
+ if (e.key==="F2" && !range.collapsed) {
243
+ e.preventDefault(); openEditor(range);
244
+ if (editing) readingInput.focus();
245
+ return;
246
+ }
247
+ if (!range.collapsed || !["Backspace","Delete"].includes(e.key)) return;
248
+ const node=range.startContainer, offset=range.startOffset, backward=e.key==="Backspace";
249
+ let ruby=enclosingRuby(node);
250
+ if (!ruby) {
251
+ let neighbor;
252
+ if (node.nodeType===Node.ELEMENT_NODE) neighbor=node.childNodes[offset-(backward?1:0)];
253
+ else if (offset===(backward?0:node.length)) neighbor=backward?node.previousSibling:node.nextSibling;
254
+ if (neighbor?.nodeName==="RUBY") ruby=neighbor;
255
+ }
256
+ if (ruby) {
257
+ e.preventDefault(); closeEditor(); range.selectNode(ruby); selectRange(range);
258
+ document.execCommand("delete");
259
+ }
260
+ });
261
+ editor.addEventListener("beforeinput", e => {
262
+ if (!e.inputType.startsWith("insert")) return;
263
+ const range=editorRange();
264
+ if (!range?.collapsed) return;
265
+ const ruby=enclosingRuby(range.startContainer);
266
+ if (ruby) {
267
+ if (range.startOffset===0) range.setStartBefore(ruby);
268
+ else range.setStartAfter(ruby);
269
+ range.collapse(true); selectRange(range);
270
+ }
271
+ });
272
+ editor.addEventListener("focusout", () => {
273
+ const range = editorRange();
274
+ if (range) lastCaret = range;
275
+ });
276
+ function applyReading(remove = false) {
277
+ if (!editing || (!remove && !readingInput.value.trim())) return;
278
+ const {range,text} = editing;
279
+ const ruby = document.createElement("ruby"), rt = document.createElement("rt");
280
+ ruby.contentEditable = "false";
281
+ rt.textContent = readingInput.value.trim(); ruby.append(text,rt);
282
+ closeEditor(); selectRange(range);
283
+ document.execCommand(remove ? "insertText" : "insertHTML", false, remove ? text : ruby.outerHTML);
284
+ }
285
+ el("applyRuby").onclick = () => applyReading();
286
+ el("removeRuby").onclick = () => applyReading(true);
287
+ function cancelReading() {
288
+ if (!editing) return;
289
+ const range = editing.range;
290
+ closeEditor(); selectRange(range);
291
+ }
292
+ el("cancelRuby").onclick = cancelReading;
293
+ readingInput.oninput = () => { el("sampleRuby").querySelector("rt").textContent=readingInput.value; el("applyRuby").disabled = !readingInput.value.trim(); };
294
+ document.addEventListener("keydown", e => {
295
+ if (!editing || e.isComposing || e.keyCode===229) return;
296
+ if (e.key==="Escape") { e.preventDefault(); cancelReading(); }
297
+ if (e.key==="Enter" && e.target===readingInput) { e.preventDefault(); applyReading(); }
298
+ });
299
+ document.addEventListener("pointerdown", e => {
300
+ if (!rubyBox.contains(e.target) && !editor.contains(e.target) && !e.target.closest("#symbols,#symbolTarget")) closeEditor();
301
+ });
302
+ window.addEventListener("scroll", e => {
303
+ if (e.target===document || e.target.contains?.(editor)) positionBox();
304
+ },true);
305
+ window.addEventListener("resize",positionBox);
306
+ editor.oninput = closeEditor;
307
+ editor.oncopy = e => {
308
+ const range = editorRange();
309
+ if (!range || range.collapsed) return;
310
+ e.preventDefault();
311
+ e.clipboardData.setData("text/plain",readGroups(range.cloneContents()).map(g=>g.text).join(""));
312
+ };
313
+ function insertPlain(text) {
314
+ if (!text.includes("\n")) return document.execCommand("insertText",false,text);
315
+ const span=document.createElement("span");
316
+ span.textContent=text;
317
+ document.execCommand("insertHTML",false,span.innerHTML.replaceAll("\n","<br>"));
318
+ }
319
+ editor.onpaste = e => {
320
+ e.preventDefault(); insertPlain(e.clipboardData.getData("text/plain").replace(/\r\n?/g,"\n"));
321
+ };
322
  function settings() {
323
+ const groups=readGroups(editor), base=groups.map(g=>g.text).join(""), reading=groups.map(g=>g.reading).join("");
324
+ return {...design, styleSpec:styles[design.style], base, reading, groups, align:el("align").value,
 
 
 
 
 
 
325
  ratio:Number(el("size").value)/100, rubyInk:design.linked ? design.ink : design.ruby, ornament:el("ornament").value.trim()};
326
  }
327
  function finishText(s) {
 
349
  function rubyText(s,reading) {
350
  const spread=s.align==="spread" && characters(reading).length>1;
351
  return "Use small "+colorText(s.rubyInk)+" furigana directly above the main lettering, about "+Math.round(s.ratio*100)+"% of its height. "+
352
+ (spread ? "Space the glyphs evenly from the left edge to the right edge of each annotated word, keeping their natural proportions." : "Center the reading over its corresponding text with natural character spacing.");
353
  }
354
  function ornamentText(s) {
355
  const desc = symbolNames[s.ornament] || "a decorative mark shaped like “"+s.ornament+"”";
356
  return "Place "+desc+" to the right of the title as a separate ornament.";
357
  }
358
+ function readingPairs(s) {
359
+ let offset=0;
360
+ return s.groups.flatMap(g=>{
361
+ const before=s.base.slice(0,offset);
362
+ offset+=g.text.length;
363
+ if(!g.reading) return [];
364
+ const count=s.base.split(g.text).length-1;
365
+ const where=count>1 ? "occurrence "+before.split(g.text).length+" of " : "";
366
+ return ["“"+g.reading+"” above "+where+"“"+g.text+"”"];
367
+ });
368
+ }
369
  function plainPrompt(s) {
370
  let text = "Write “"+s.base+"”";
371
  if (s.reading) {
372
+ if (s.groups.length>1) {
373
+ const pairs=readingPairs(s);
374
  text+=" with small furigana: "+pairs.join(", ");
375
  } else {
376
  const spread=s.align==="spread" && characters(s.reading).length>1;
 
387
  const lines=["Render the horizontal title “"+s.base+"” in "+colorText(s.ink)+face+"."];
388
  if (d) lines.push(d);
389
  if (s.reading) {
390
+ if (s.groups.length>1) {
391
+ lines.push("Place "+readingPairs(s).join(", ")+".");
392
  } else lines.push("The reading is “"+s.reading+"”.");
393
  lines.push(rubyText(s,s.reading));
394
  if (d) lines.push(s.includeRuby ? "Repeat the title treatment on the reading at its smaller scale." : "Keep the reading plain and upright.");
 
406
  }
407
  // All geometry is in lettering units, shared by the preview and Layout exporter.
408
  function composition(s) {
409
+ if (!s.groups.some(g=>g.text.includes("\n"))) return composeLine(s);
410
+ const rows=[[]], items=[];
411
+ for(const group of s.groups){
412
+ group.text.split("\n").forEach((text,i)=>{
413
+ if(i) rows.push([]);
414
+ if(text) rows.at(-1).push({...group,text});
415
+ });
416
+ }
417
+ rows.forEach((groups,row)=>{
418
+ if(!groups.length && !(row===rows.length-1 && s.ornament)) return;
419
+ const line=composeLine({...s,groups,ornament:row===rows.length-1?s.ornament:""});
420
+ const dy=row*160*(1.4+s.ratio);
421
+ for(const item of line.items){
422
+ item.baseline+=dy; item.box[1]+=dy; item.box[3]+=dy; items.push(item);
423
+ }
424
+ });
425
+ return {items,box:items.reduce((b,i)=>[Math.min(b[0],i.box[0]),Math.min(b[1],i.box[1]),Math.max(b[2],i.box[2]),Math.max(b[3],i.box[3])],[Infinity,Infinity,-Infinity,-Infinity])};
426
+ }
427
+ function composeLine(s) {
428
  const size=160, items=[];
429
  let cursor=0;
430
  for (const g of s.groups) {
 
591
  }
592
  for(const symbol of Object.keys(symbolNames)){
593
  const b=document.createElement("button"); b.type="button"; b.textContent=symbol; b.setAttribute("aria-label",symbol+"を挿入");
594
+ b.onmousedown=e=>e.preventDefault();
595
+ b.onclick=()=>{
596
+ const target=el("symbolTarget").value;
597
+ if(target==="base"){
598
+ closeEditor();
599
+ const range=lastCaret || document.createRange();
600
+ if(!lastCaret){range.selectNodeContents(editor);range.collapse(false);}
601
+ selectRange(range); insertPlain(symbol);
602
+ } else {
603
+ if(target==="reading" && !editing){
604
+ editor.focus(); el("notice").textContent="先に本文の文字を選択してください。"; return;
605
+ }
606
+ const input=el(target);
607
+ input.setRangeText(symbol,input.selectionStart,input.selectionEnd,"end"); input.focus();
608
+ input.dispatchEvent(new Event("input",{bubbles:true}));
609
+ }
610
+ };
611
  el("symbols").append(b);
612
  }
613
  function update(){
614
  const s=settings(), format=el("format").value, useLayout=format==="layout";
615
  const w=useLayout ? el("width").valueAsNumber : canvas.clientWidth, h=useLayout ? el("height").valueAsNumber : canvas.clientHeight;
616
  const validSize=n=>Number.isSafeInteger(n) && n>0 && n%16===0;
617
+ const error=(useLayout && (!validSize(w)||!validSize(h)) ? "生成幅と高さは16の倍数で入力してください。" : "");
618
+ const scene=s.base.trim() && !error ? composition(s) : null;
619
+ el("dimensions").hidden=!useLayout; editor.classList.toggle("center",s.align==="center"); editor.style.setProperty("--ruby-size", Math.max(.35,s.ratio)+"em");
620
  el("error").textContent=error; el("error").hidden=!error; el("sizeValue").value=el("size").value+"%";
621
  let prompt="";
622
  if (scene) {