slop-editor 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +5 -3
  2. package/package.json +1 -1
  3. package/slop.js +34 -30
package/README.md CHANGED
@@ -78,15 +78,17 @@ In the page:
78
78
 
79
79
  Code can render texts itself with `Slop.html('intro.p1')` (HTML), `Slop.paragraphs('intro.p1')` (HTML, one string per paragraph) or `Slop.words('intro.p1')` (plain text). Put `data-slop-ref="intro.p1"` on the element that shows them (a button or link the page builds) and the editor can still find, highlight and scroll to it. Unlike `data-slop`, nothing fills it.
80
80
 
81
- **Lists.** Where the page shows a group as a list of paragraphs, say so, and the editor can add and remove texts there:
81
+ **Lists.** Where the page shows a group's paragraphs (`p1`, `p2`, …) in one element, say so, and the editor can add and remove paragraphs there:
82
82
 
83
83
  ```html
84
- <div data-slop-list="intro"> <!-- a copy of the first child for each text in "intro", in order -->
84
+ <div data-slop-list="intro"> <!-- a copy of the first paragraph for each p<n> in "intro", in order -->
85
+ <h2 data-slop="intro.title"></h2> <!-- the group's other texts and other children stay where they are -->
85
86
  <p data-slop="intro.p1"></p>
87
+ <p data-slop="intro.p2"></p>
86
88
  </div>
87
89
  ```
88
90
 
89
- The list's children are its texts and nothing else. A page that draws them itself (React and the like) puts `data-slop-list-ref="intro"` on the element around them instead, draws one per key of the group, in order, and redraws after an edit.
91
+ All of the group's paragraphs have to be in that element. A new one goes after the paragraph before it. A page that draws them itself (React and the like) puts `data-slop-list-ref="intro"` on the element around them instead, draws one per paragraph key, in order, and redraws after an edit.
90
92
 
91
93
  **Words the page doesn't show as words** (alt text for an image that doesn't load, a tooltip, a label for screen readers, a placeholder) go in attributes: `<img data-slop-attr="alt:hero.photo">`, or several at once, `data-slop-attr="alt:hero.photo title:hero.tip"`. They're filled as plain text. The editor gives them a tab of their own, **Hidden**, since readers' colors can't show them.
92
94
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slop-editor",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A slop editor: track who wrote every word on a page (the AI, you, or nobody), then read the slop and make it yours.",
5
5
  "homepage": "https://baphaelman.github.io/slop-editor/",
6
6
  "repository": {
package/slop.js CHANGED
@@ -377,29 +377,36 @@ const Slop = (() => {
377
377
  });
378
378
  if (copies.length) spills.set(el, copies); else spills.delete(el);
379
379
  }
380
- // a list: an element with data-slop-list="group" shows each text in that group, in order, in a copy of its first
381
- // child. (With data-slop-list-ref, the page draws them itself.) Either way, the editor can add and remove texts there
382
- const templates = new WeakMap(); // list → the element its texts are copies of
380
+ // a list: an element with data-slop-list="group" shows the group's paragraphs (p1, p2, …), in order, each in a copy
381
+ // of the first. The group's other texts (a title, a caption) and the element's other children stay where they
382
+ // are. (With data-slop-list-ref, the page draws them itself.) Either way, the editor can add and remove paragraphs
383
+ const isParagraph = (key) => /^p\d+$/.test(key);
384
+ const templates = new WeakMap(); // list → the element its paragraphs are copies of
383
385
  function fillList(box) {
384
386
  const at = box.dataset.slopList;
387
+ const ours = (x) => x.dataset.slop?.startsWith(`${at}.`) && isParagraph(x.dataset.slop.slice(at.length + 1));
385
388
  if (!templates.has(box)) {
386
- if (!box.firstElementChild) return;
387
- const t = box.firstElementChild.cloneNode(false);
389
+ const first = [...box.children].find(ours);
390
+ if (!first) return;
391
+ const t = first.cloneNode(false);
388
392
  ['id', 'data-slop'].forEach((a) => t.removeAttribute(a));
389
393
  templates.set(box, t);
390
394
  }
391
- const paths = textKeys(nodeAt(text, at)).map((k) => `${at}.${k}`);
392
- const have = new Map([...box.children].filter((x) => x.dataset.slop).map((x) => [x.dataset.slop, x]));
395
+ const paths = textKeys(nodeAt(text, at)).filter(isParagraph).map((k) => `${at}.${k}`);
396
+ const have = new Map([...box.children].filter(ours).map((x) => [x.dataset.slop, x]));
393
397
  have.forEach((x, p) => { if (!paths.includes(p)) { spill(x, []); x.remove(); } });
394
- let last = null;
395
- paths.forEach((p) => {
396
- let x = have.get(p);
397
- if (!x) {
398
- x = templates.get(box).cloneNode(false);
399
- x.dataset.slop = p;
400
- }
401
- if (last ? last.nextElementSibling !== x : box.firstElementChild !== x) (last ? last.after(x) : box.prepend(x));
402
- last = spills.get(x)?.at(-1) ?? x;
398
+ // a new one goes after the paragraph before it (and the paragraphs that one spilled into), or else before the
399
+ // one after it
400
+ const shown = (p) => [...box.children].find((x) => x.dataset.slop === p);
401
+ paths.forEach((p, i) => {
402
+ if (have.has(p)) return;
403
+ const x = templates.get(box).cloneNode(false);
404
+ x.dataset.slop = p;
405
+ const before = paths.slice(0, i).reverse().map(shown).find(Boolean);
406
+ const after = paths.slice(i + 1).map(shown).find(Boolean);
407
+ if (before) (spills.get(before)?.at(-1) ?? before).after(x);
408
+ else if (after) after.before(x);
409
+ else box.append(x);
403
410
  });
404
411
  }
405
412
  const fill = (root = document) => {
@@ -645,8 +652,8 @@ const Slop = (() => {
645
652
  // keys[i], for people. In a list, a paragraph is named by where it is: "Paragraph 3", even when its key is p7
646
653
  function keyName(keys, i) {
647
654
  const group = keys.slice(0, i).join('.');
648
- if (!/^p\d+$/.test(keys[i]) || !listed().has(group)) return human(keys[i]);
649
- return `Paragraph ${textKeys(nodeAt(text, group)).indexOf(keys[i]) + 1}`;
655
+ if (!isParagraph(keys[i]) || !listed().has(group)) return human(keys[i]);
656
+ return `Paragraph ${textKeys(nodeAt(text, group)).filter(isParagraph).indexOf(keys[i]) + 1}`;
650
657
  }
651
658
  // where a text is, for people: "Introduction · Paragraph 2"
652
659
  const where = (path) => {
@@ -919,7 +926,7 @@ const Slop = (() => {
919
926
  render();
920
927
  });
921
928
  const group = keys.slice(0, -1).join('.');
922
- const listItem = !all && group && listed().has(group);
929
+ const listItem = !all && group && listed().has(group) && isParagraph(keys.at(-1));
923
930
  if (listItem) {
924
931
  wrap.insert = plusRow(path, group);
925
932
  wrap.append(wrap.insert);
@@ -1009,13 +1016,10 @@ const Slop = (() => {
1009
1016
 
1010
1017
  /* lists: where the page shows a group as a list, texts can be added and taken out */
1011
1018
  const inList = (key) => [...listed()].some((g) => g === key || g.startsWith(`${key}.`));
1012
- // a key for a new text in a group, like `like` (p3 → p<next>), and never one the group had since it was opened
1013
- function freshKey(group, like) {
1014
- const prefix = /^(.*?)\d+$/.exec(like)?.[1] || 'p';
1015
- const taken = new Set([text, saved, JSON.parse(original)].flatMap((root) => Object.keys(nodeAt(root, group) ?? {})));
1016
- let n = 1 + Math.max(0, ...[...taken].map((k) => (k.startsWith(prefix) && /^\d+$/.test(k.slice(prefix.length)) ? +k.slice(prefix.length) : 0)));
1017
- while (taken.has(`${prefix}${n}`)) n++;
1018
- return `${prefix}${n}`;
1019
+ // a key for a new paragraph in a group: the p<n> after any the group had in the file, at the last save or now
1020
+ function freshKey(group) {
1021
+ const taken = [text, saved, JSON.parse(original)].flatMap((root) => Object.keys(nodeAt(root, group) ?? {})).filter(isParagraph);
1022
+ return `p${1 + Math.max(0, ...taken.map((k) => +k.slice(1)))}`;
1019
1023
  }
1020
1024
  // a text put in a group after the key `after` (or first), the rest in the order they were
1021
1025
  function insert(group, after, key, value) {
@@ -1026,7 +1030,7 @@ const Slop = (() => {
1026
1030
  set(group, next);
1027
1031
  }
1028
1032
  function addText(group, after) {
1029
- const key = freshKey(group, after ?? textKeys(nodeAt(saved, group))[0] ?? '');
1033
+ const key = freshKey(group);
1030
1034
  withUndo('Added a paragraph', () => insert(group, after, key, ''));
1031
1035
  show('all', `${group}.${key}`);
1032
1036
  }
@@ -1084,8 +1088,8 @@ const Slop = (() => {
1084
1088
  // in the All tab, each list's removed texts until the next save, each with Restore, and a way to start an empty one
1085
1089
  function listRows(key, box) {
1086
1090
  [...listed()].filter((g) => g === key || g.startsWith(`${key}.`)).forEach((group) => {
1087
- const now = textKeys(nodeAt(text, group));
1088
- const was = textKeys(nodeAt(saved, group));
1091
+ const now = textKeys(nodeAt(text, group)).filter(isParagraph);
1092
+ const was = textKeys(nodeAt(saved, group)).filter(isParagraph);
1089
1093
  const fieldOf = (k) => [...box.querySelectorAll(':scope > .sp-field')].find((f) => f.dataset.path === `${group}.${k}`);
1090
1094
  was.forEach((k, i) => {
1091
1095
  if (now.includes(k)) return;
@@ -1108,7 +1112,7 @@ const Slop = (() => {
1108
1112
  const start = el('button', 'sp-cat-add sp-start', '+ Paragraph');
1109
1113
  start.type = 'button';
1110
1114
  start.title = `Start "${where(group)}" again`;
1111
- start.addEventListener('click', () => addText(group, null));
1115
+ start.addEventListener('click', () => addText(group, Object.keys(nodeAt(text, group) ?? {}).at(-1) ?? null)); // at the group's end
1112
1116
  box.append(start);
1113
1117
  });
1114
1118
  }