slop-editor 0.5.0 → 0.5.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 (4) hide show
  1. package/README.md +6 -5
  2. package/package.json +1 -1
  3. package/slop.css +12 -6
  4. package/slop.js +58 -15
package/README.md CHANGED
@@ -157,11 +157,12 @@ import Slop, { SlopText, SlopMeter, slopAttrs, slopList, useSlop } from 'slop-ed
157
157
 
158
158
  `slop-editor edit page.html` serves the page's folder on 127.0.0.1 and opens the page with an **Edit text** button. Published, the page shows no sign of the editor. `Slop.start({edit: 'local'})` gives every visitor an editor that saves in their browser instead (the demo page uses it). When the page's author publishes a new version, a visitor's saved edits carry over: the author's changes win, text by text, and the visitor's stay everywhere else. They're kept under the page's path; on a host that gives each version an address of its own, name the page instead: `Slop.start({edit: 'local', key: 'my-page'})`.
159
159
 
160
- - **All** shows every text, grouped by section. **Slop left** shows only the texts that aren't all yours yet; work through it until it's empty. **Repeats** shows the repeated text. **Hidden** (on pages that have some) shows alt text, tooltips and labels.
160
+ - **All** shows every text, grouped by section. An open section's name stays at the top while you scroll its texts, so you can close it from anywhere in it. **Slop left** shows only the texts that aren't all yours yet; work through it until it's empty. **Repeats** shows the repeated text. **Hidden** (on pages that have some) shows alt text, tooltips and labels.
161
161
  - Each text box shows its words in their authors' colors, as readers see them with the Slop-o-meter on.
162
- - Each text, section and the whole page has a pie of what it's made of, by its name, and a **slop** switch at the right. Off approves the AI's words; on takes that back. Your typed words are never touched. Every switch can be undone from the status line.
163
- - At the top: how much of the page is slop, and every category with its share, the biggest first (as in the meters' legends). Click a category's dot for its color picker, laid out like the browser's (a rectangle of shades; the eyedropper, the color and a hue bar; RGB, HEX or HSL numbers). Its eyedropper, or dragging the dot onto any color on the page, takes a color from the page: the point is a ring, and a magnifier beside it (not over it, so you can see where you are) shows the page around the point three times bigger, rimmed in the color, joined to the ring by a line (Escape drops it). **Revert colors** puts every category back to its default. Rename a category by editing its name. **+ Category** adds one; × deletes it (its words become AI-generated).
164
- - Focus a text and a row of categories appears under it: **Mark all as** (or **Mark selection as**, with words selected) gives the text, or just the selection, to a category.
162
+ - Each text and section has a pie of what it's made of, by its name, and a **slop** switch at the right. Off approves the AI's words; on takes that back. Your typed words are never touched. Every switch can be undone from the status line.
163
+ - At the top: every category with its share of the page, the biggest first (as in the meters' legends). Click a category's dot for its color picker, laid out like the browser's (a rectangle of shades; the eyedropper, the color and a hue bar; RGB, HEX or HSL numbers). Its eyedropper, or dragging the dot onto any color on the page, takes a color from the page: the point is a ring, and a magnifier beside it (not over it, so you can see where you are) shows the page around the point three times bigger, rimmed in the color, joined to the ring by a line (Escape drops it). **Revert colors** puts every category back to its default. Rename a category by editing its name. **+ Category** adds one; × deletes it (its words become AI-generated).
164
+ - Focus a text and a row of categories appears under it: **Mark all as** (or **Mark selection as**, as soon as you start selecting) gives the text, or just the selection, to a category.
165
+ - Words copied (or cut) from a text keep their authors when pasted into a text: AI words stay AI, approved stay approved. Words pasted from anywhere else are yours, as if typed.
165
166
  - In a list, a **+** under the text you're on adds an empty paragraph after it; pointing at the + draws a line on the page where it would go. The trash can before its slop switch outlines the text on the page in dashed red; a text with words takes a second click (it asks **Remove?**), and stays red until then. A removed text stays in **All** as a row with **Restore** until the next save. New texts get new keys (`p7` between `p2` and `p3` is still called Paragraph 3 in the editor), so a key never moves to other words.
166
167
  - Click any text on the page to jump to it in the editor. Focus a text in the editor to find it on the page.
167
168
  - Drag the header to move the editor anywhere, and drag any edge or corner to resize it. It stays where you leave it; double-click the header to put it back. On a phone it's a sheet along the bottom, and dragging changes its height.
@@ -189,7 +190,7 @@ It's the page's choice, made in the editor (**Readers see**) and saved in the bl
189
190
  - **Pie chart**: a pie of who wrote what, and a legend: each category in its color, with its share of the page's text.
190
191
  - **Colors**: the Slop-o-meter, the pie and legend plus a switch that colors every word by who wrote it.
191
192
 
192
- It shows where the page puts `<div data-slop-meter></div>`. While the editor is open, you see the colors whatever readers get.
193
+ It shows where the page puts `<div data-slop-meter></div>`. While the editor is open, you see the colors whatever readers get; its **Colors** switch (or the page's Slop-o-meter) turns them off to compare.
193
194
 
194
195
  A meter can cover one part of the page: `<div data-slop-meter="games.othello"></div>` measures only the texts under `games.othello`, and its switch colors only its section, the closest `[data-slop-section]` or else its parent. A section's switch starts the way the page's is and is its own once flipped. Only the page's is remembered.
195
196
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slop-editor",
3
- "version": "0.5.0",
3
+ "version": "0.5.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.css CHANGED
@@ -74,7 +74,16 @@ html.slop-dark {
74
74
  .sp-status[data-kind="dirty"] { color: var(--_ink-2); }
75
75
  .sp-status[data-kind="error"] { color: #d0453a; }
76
76
  .sp-link { padding: 0; border: 0; background: none; color: var(--_accent) !important; font: 600 0.78rem var(--_font); text-decoration: underline; text-underline-offset: 2px; cursor: pointer; }
77
- .sp-save { flex: none; margin-left: auto; height: 30px; padding: 0 13px; border-radius: 8px; border: 1px solid var(--_accent); background: var(--_accent); color: var(--_bg) !important; font: 600 0.82rem var(--_font); font-variant-numeric: tabular-nums; cursor: pointer; }
77
+ .sp-colors {
78
+ flex: none; display: inline-flex; align-items: center; gap: 6px; height: 30px; margin-left: auto; padding: 0 10px 0 7px; border-radius: 8px;
79
+ border: 1px solid var(--_rule); background: none; font: 600 0.78rem var(--_font); color: var(--_ink-3) !important; cursor: pointer;
80
+ }
81
+ .sp-colors:hover { border-color: var(--_ink-3); }
82
+ .sp-colors .sp-switch { background: var(--_rule); }
83
+ .sp-colors[aria-checked="true"] { color: var(--_ink) !important; }
84
+ .sp-colors[aria-checked="true"] .sp-switch { background: var(--slop-c-ai, var(--_accent)); }
85
+ .sp-colors[aria-checked="true"] .sp-switch::after { transform: translateX(10px); }
86
+ .sp-save { flex: none; height: 30px; padding: 0 13px; border-radius: 8px; border: 1px solid var(--_accent); background: var(--_accent); color: var(--_bg) !important; font: 600 0.82rem var(--_font); font-variant-numeric: tabular-nums; cursor: pointer; }
78
87
  .sp-save:hover:not(:disabled) { filter: brightness(1.1); }
79
88
  .sp-save:disabled { border-color: var(--_rule); background: none; color: var(--_ink-3) !important; cursor: default; }
80
89
  .sp-revert { flex: none; padding: 0 2px; border: 0; background: none; font: 600 0.72rem var(--_font); color: var(--_ink-3) !important; text-decoration: underline; text-decoration-color: var(--_rule); text-underline-offset: 2px; cursor: pointer; }
@@ -93,11 +102,6 @@ html.slop-dark {
93
102
 
94
103
  /* the whole page: how much is slop, the page's switch, and the categories */
95
104
  .sp-cover { display: grid; gap: 10px; padding: 0 16px 14px; }
96
- .sp-cover-row { display: flex; align-items: center; gap: 10px; }
97
- .sp-big { font-size: 0.9rem; color: var(--_ink-2); }
98
- .sp-big b { font: 700 1.55rem/1 var(--_font); color: var(--slop-c-ai, var(--_accent)); font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
99
- .sp-words { margin-right: auto !important; font-size: 0.78rem; color: var(--_ink-3); font-variant-numeric: tabular-nums; }
100
- .sp-cover-row .sp-pie { width: 22px; height: 22px; }
101
105
  .sp-cats { display: flex; flex-wrap: wrap; gap: 6px; }
102
106
  .sp-cat { display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 9px 0 4px; border: 1px solid var(--_rule); border-radius: 99px; font-size: 0.78rem; }
103
107
  .sp-cat { cursor: pointer; }
@@ -164,6 +168,8 @@ html.slop-dark {
164
168
  /* sections */
165
169
  .sp-group { border-bottom: 1px solid var(--_rule); }
166
170
  .sp-group summary { display: flex; align-items: center; gap: 8px; padding: 10px 4px; cursor: pointer; list-style: none; }
171
+ /* an open section's name stays at the top while its texts scroll, so it can be closed from anywhere in it */
172
+ .sp-group[open] > summary { position: sticky; top: -8px; z-index: 3; margin: 0 -12px; padding: 10px 16px; background: var(--_bg); }
167
173
  .sp-group summary::-webkit-details-marker { display: none; }
168
174
  .sp-group summary::before { content: ""; flex: none; width: 6px; height: 6px; margin: 0 3px 0 1px; border: solid var(--_ink-3); border-width: 0 1.5px 1.5px 0; transform: rotate(-45deg); transition: transform .15s; }
169
175
  .sp-group[open] summary::before { transform: rotate(45deg) translate(-2px, 0); }
package/slop.js CHANGED
@@ -101,6 +101,8 @@ const Slop = (() => {
101
101
  const reslop = (value) => reattribute(value, 'ok', 'ai');
102
102
  // characters start to end (of the plain text) marked as one author's
103
103
  const assign = (value, start, end, to) => fromChars(plain(value), authorsByChar(value).map((a, i) => (i >= start && i < end ? to : a)));
104
+ // characters from `start` on given these authors, one each (what was pasted keeps who wrote it), the text unchanged
105
+ const overlay = (value, start, authors) => fromChars(plain(value), authorsByChar(value).map((a, i) => authors[i - start] ?? a));
104
106
 
105
107
  const LINK = /\[([^\]]+)\]\(([^)\s]+)\)/y;
106
108
  const VAR = /\{(\w+)\}/y;
@@ -310,7 +312,7 @@ const Slop = (() => {
310
312
  return out;
311
313
  }
312
314
 
313
- const core = { BUILTIN, plain, applyEdit, reslop, unslop, assign, reattribute, runs, readable, render, makeup, share, percent, repeats, rebase, listsIn, wordsOf, paragraphsOf, vars };
315
+ const core = { BUILTIN, plain, applyEdit, reslop, unslop, assign, overlay, reattribute, runs, readable, render, makeup, share, percent, repeats, rebase, listsIn, wordsOf, paragraphsOf, vars };
314
316
  if (typeof document === 'undefined') return core; // Node (the CLI and the tests) only needs the format
315
317
 
316
318
  /* ---------- the block ---------- */
@@ -626,13 +628,15 @@ const Slop = (() => {
626
628
  const store = storage(() => localStorage);
627
629
  const session = storage(() => sessionStorage); // this tab only: the unsaved draft
628
630
  // what readers see is the page's choice, "nothing" unless it says otherwise. While the editor is open, its
629
- // author sees the colors whatever readers get
631
+ // author sees the colors whatever readers get, until they switch them off to compare (editColors)
630
632
  const SHOW = ['nothing', 'pie', 'colors'];
631
633
  const shows = () => (SHOW.includes(text._show) ? text._show : 'nothing');
632
634
  const meters = [];
633
635
  const built = new WeakMap(); // element → its meter
634
636
  let meterOn = false;
635
637
  let editing = false;
638
+ let editColors = true;
639
+ let editSwitch = null; // the editor's own Slop-o-meter switch, in its header
636
640
  function setMeter(on) {
637
641
  meterOn = on;
638
642
  paintShow();
@@ -643,7 +647,8 @@ const Slop = (() => {
643
647
  const sectionOf = (m) => (m.at && m.el.parentElement ? m.el.closest('[data-slop-section]') ?? m.el.parentElement : document.documentElement);
644
648
  function paintShow() {
645
649
  const mode = shows();
646
- document.documentElement.classList.toggle('slop-on', editing || (mode === 'colors' && meterOn));
650
+ document.documentElement.classList.toggle('slop-on', editing ? editColors : mode === 'colors' && meterOn);
651
+ editSwitch?.setAttribute('aria-checked', String(editColors));
647
652
  meters.forEach((m) => {
648
653
  m.el.dataset.show = mode;
649
654
  const section = sectionOf(m);
@@ -655,7 +660,7 @@ const Slop = (() => {
655
660
  section.classList.toggle('slop-on', own === true);
656
661
  section.classList.toggle('slop-off', own === false);
657
662
  }
658
- m.btn.setAttribute('aria-pressed', String(m.own ?? meterOn));
663
+ m.btn.setAttribute('aria-pressed', String(editing ? editColors : m.own ?? meterOn));
659
664
  });
660
665
  }
661
666
  function paintMeters() {
@@ -683,6 +688,7 @@ const Slop = (() => {
683
688
  + '<span class="slop-meter-key"><span class="slop-pie" role="img"></span><span class="slop-legend"></span></span>';
684
689
  const m = { el, at: el.dataset.slopMeter || '', own: null, section: null, btn: el.querySelector('button'), pie: el.querySelector('.slop-pie'), legend: el.querySelector('.slop-legend') };
685
690
  m.btn.addEventListener('click', () => {
691
+ if (editing) { editColors = !editColors; paintShow(); return; } // the editor's switch, while it's open
686
692
  const on = !(m.own ?? meterOn);
687
693
  if (m.at) { m.own = on; paintShow(); return; } // only the page-wide choice is remembered
688
694
  setMeter(on);
@@ -760,11 +766,11 @@ const Slop = (() => {
760
766
  <header class="sp-head" title="Drag to move, or drag an edge to resize. Double-click to put it back">
761
767
  <span class="sp-grip" aria-hidden="true"></span>
762
768
  <div><h2>Edit text</h2><p class="sp-status" role="status" aria-live="polite"></p></div>
769
+ <button type="button" class="sp-colors" role="switch" title="Slop-o-meter: color the page's words by who wrote them. Only for you, while you edit"><span class="sp-switch" aria-hidden="true"></span>Colors</button>
763
770
  <button type="button" class="sp-save" title="Save (⌘S)" disabled>Saved</button>
764
771
  <button type="button" class="sp-close" aria-label="Close the editor">×</button>
765
772
  </header>
766
773
  <div class="sp-cover">
767
- <div class="sp-cover-row"><p class="sp-big"><b></b> slop</p><p class="sp-words"></p></div>
768
774
  <div class="sp-cats"></div>
769
775
  <div class="sp-show"><span>Readers see</span><div class="sp-seg" role="radiogroup" aria-label="What readers see">
770
776
  <button type="button" role="radio" data-show="nothing" title="No sign of the slop editor on the published page">Nothing</button>
@@ -786,8 +792,9 @@ const Slop = (() => {
786
792
  const $ = (sel) => aside.querySelector(sel);
787
793
  const body = $('.sp-body');
788
794
  const statusEl = $('.sp-status');
789
- const pageControl = control(() => entries().map(([p]) => p), 'the whole page');
790
- $('.sp-cover-row').append(pageControl);
795
+ aside.classList.add('slop-on'); // the text boxes keep their colors when the page's are off
796
+ editSwitch = $('.sp-colors');
797
+ editSwitch.addEventListener('click', () => { editColors = !editColors; paintShow(); });
791
798
 
792
799
  function status(msg, kind = '', action) {
793
800
  statusEl.textContent = msg;
@@ -1383,6 +1390,32 @@ const Slop = (() => {
1383
1390
  const assignLabel = (input) => {
1384
1391
  if (assignEl.firstChild) assignEl.firstChild.textContent = input.selectionStart === input.selectionEnd ? 'Mark all as' : 'Mark selection as';
1385
1392
  };
1393
+ // while a selection is being dragged out too, so the row doesn't change under the pointer once it's let go
1394
+ document.addEventListener('selectionchange', () => {
1395
+ const a = document.activeElement;
1396
+ if (a?.tagName === 'TEXTAREA' && assignEl.parentNode?.contains(a)) assignLabel(a);
1397
+ });
1398
+
1399
+ /* copying a text's words keeps who wrote them: pasted into a text (here, or in another page's editor), they keep
1400
+ their authors. Words from anywhere else are mine, as typed */
1401
+ const CLIP = 'application/x-slop-editor';
1402
+ let clip = null; // {words, authors}, the last copied here: for a browser that keeps only plain text
1403
+ function copyOut(e, input, path) {
1404
+ const [s, t] = [input.selectionStart, input.selectionEnd];
1405
+ if (s === t || !e.clipboardData) return;
1406
+ clip = { words: input.value.slice(s, t), authors: authorsByChar(get(path)).slice(s, t) };
1407
+ e.preventDefault();
1408
+ e.clipboardData.setData('text/plain', clip.words);
1409
+ e.clipboardData.setData(CLIP, JSON.stringify(clip));
1410
+ if (e.type === 'cut') document.execCommand('delete'); // as an edit, so ⌘Z brings it back
1411
+ }
1412
+ function pastedIn(e) {
1413
+ const words = (e.clipboardData?.getData('text/plain') ?? '').replace(/\r\n?/g, '\n');
1414
+ let c = clip;
1415
+ try { c = JSON.parse(e.clipboardData.getData(CLIP)) ?? clip; } catch { /* plain text only */ }
1416
+ if (!c || c.words !== words || !Array.isArray(c.authors) || c.authors.length !== words.length) return null;
1417
+ return { words, authors: c.authors.map((a) => (typeof a === 'string' && a ? a : 'me')) };
1418
+ }
1386
1419
 
1387
1420
  /* the views */
1388
1421
  const setOf = (path) => sets.find((s) => s.paths.includes(path));
@@ -1462,10 +1495,15 @@ const Slop = (() => {
1462
1495
  input.addEventListener('beforeinput', () => { input.at = [input.selectionStart, input.selectionEnd]; });
1463
1496
  input.addEventListener('input', (e) => {
1464
1497
  const restoring = /^history(Undo|Redo)$/.test(e.inputType);
1465
- edit(paths(), input, restoring, restoring || e.isComposing || /Composition/.test(e.inputType) ? undefined : input.at);
1498
+ const pasted = e.inputType === 'insertFromPaste' ? input.pasted : null;
1499
+ edit(paths(), input, restoring, restoring || e.isComposing || /Composition/.test(e.inputType) ? undefined : input.at, pasted);
1466
1500
  input.at = undefined;
1501
+ input.pasted = null;
1467
1502
  });
1468
- ['select', 'keyup', 'pointerup'].forEach((t) => input.addEventListener(t, () => assignLabel(input)));
1503
+ input.addEventListener('copy', (e) => copyOut(e, input, paths()[0]));
1504
+ input.addEventListener('cut', (e) => copyOut(e, input, paths()[0]));
1505
+ input.addEventListener('paste', (e) => { input.pasted = pastedIn(e); });
1506
+ ['select', 'selectionchange', 'keyup', 'pointerup'].forEach((t) => input.addEventListener(t, () => assignLabel(input)));
1469
1507
  input.addEventListener('focus', () => { focus(path, all, input, wrap); showAssign(wrap, input, paths()); });
1470
1508
  input.addEventListener('blur', () => {
1471
1509
  mark(false);
@@ -1495,7 +1533,11 @@ const Slop = (() => {
1495
1533
  det.querySelector('.sp-gname').after(ctl.pie);
1496
1534
  det.querySelector('summary').append(ctl);
1497
1535
  det.addEventListener('toggle', () => {
1498
- if (det.open) { state.open.add(key); det.querySelectorAll('textarea').forEach(grow); } else state.open.delete(key);
1536
+ if (det.open) { state.open.add(key); det.querySelectorAll('textarea').forEach(grow); return; }
1537
+ state.open.delete(key);
1538
+ // closed from its name stuck at the top: back to where the section starts
1539
+ const above = det.getBoundingClientRect().top - body.getBoundingClientRect().top;
1540
+ if (above < 0) body.scrollTop += above;
1499
1541
  });
1500
1542
  paths.forEach((p) => det.append(field(p)));
1501
1543
  if (state.tab === 'all') listRows(key, det);
@@ -1625,7 +1667,7 @@ const Slop = (() => {
1625
1667
 
1626
1668
  function render() {
1627
1669
  FLAGS.forEach((name) => flag(name)); // the elements pointed at may be gone
1628
- controls = [controls.find((c) => c.el === pageControl)];
1670
+ controls = [];
1629
1671
  sets = repeats(entries());
1630
1672
  groupKeys().forEach((k) => { if (!state.known.has(k)) { state.known.add(k); state.open.add(k); } });
1631
1673
  renderCats();
@@ -1679,8 +1721,6 @@ const Slop = (() => {
1679
1721
  const cats = categories();
1680
1722
  const all = entries();
1681
1723
  const page = makeup(all.map(([, v]) => v));
1682
- $('.sp-big b').textContent = percent(page.letters ? (page.counts.ai ?? 0) / page.letters : 0);
1683
- $('.sp-words').textContent = count(page.allWords, 'word');
1684
1724
  aside.querySelectorAll('.sp-seg button').forEach((b) => b.setAttribute('aria-checked', String(b.dataset.show === shows())));
1685
1725
  const revert = $('.sp-cat-revert');
1686
1726
  if (revert) revert.hidden = !Object.values(text._categories ?? {}).some((c) => c.color);
@@ -1815,12 +1855,15 @@ const Slop = (() => {
1815
1855
  past.set(p, list.slice(-200));
1816
1856
  }
1817
1857
  const recall = (p, words) => past.get(p)?.findLast((v) => plain(v) === words);
1818
- function edit(paths, input, restoring, at) {
1858
+ // `pasted`: words copied from a text, with their authors, now at `at`
1859
+ function edit(paths, input, restoring, at, pasted) {
1819
1860
  undo = null;
1820
1861
  const f = state.focus;
1862
+ const keep = pasted && at && input.value.startsWith(pasted.words, at[0]);
1821
1863
  [...paths, ...(f?.linked ?? [])].forEach((p) => {
1822
1864
  remember(p);
1823
- set(p, applyEdit(get(p), input.value, restoring ? recall(p, input.value) : undefined, at));
1865
+ const next = applyEdit(get(p), input.value, restoring ? recall(p, input.value) : undefined, at);
1866
+ set(p, keep ? overlay(next, at[0], pasted.authors) : next);
1824
1867
  remember(p);
1825
1868
  });
1826
1869
  grow(input);