slop-editor 0.3.1 → 0.5.0
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.
- package/README.md +38 -9
- package/cli.mjs +233 -11
- package/jsx.mjs +248 -0
- package/markup.mjs +6 -6
- package/package.json +10 -3
- package/slop.css +48 -2
- package/slop.js +550 -30
- package/vite.mjs +89 -0
package/vite.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// slop-editor/vite: the slop editor in a Vite app. The words live in one JSON file (src/slopText.json). This puts them in
|
|
2
|
+
// the page as its text block, with slop.js and slop.css, which render them, and under `vite` (not a build) it answers
|
|
3
|
+
// the editor: its Edit text button appears on the page and saves back into the file. A built site has no editor.
|
|
4
|
+
//
|
|
5
|
+
// import slopEditor from 'slop-editor/vite';
|
|
6
|
+
// export default defineConfig({ plugins: [react(), slopEditor()] }); // or slopEditor({ text: 'content/words.json' })
|
|
7
|
+
//
|
|
8
|
+
// The file's `version` goes up with every save, so a page that loaded an older version can't save over what changed
|
|
9
|
+
// since. Edit the file directly (an agent, say) and the open page reloads. MIT license.
|
|
10
|
+
import { readFile, writeFile, rename } from 'node:fs/promises';
|
|
11
|
+
import { existsSync } from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
|
|
15
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
|
|
17
|
+
// where a project keeps its words unless it says: the file that's there, else src/slopText.json (or slopText.json in a
|
|
18
|
+
// project with no src). `slop-editor install` makes the same choice
|
|
19
|
+
export const textFileIn = (root) => ['src/slopText.json', 'slopText.json'].find((f) => existsSync(path.join(root, f)))
|
|
20
|
+
?? (existsSync(path.join(root, 'src')) ? 'src/slopText.json' : 'slopText.json');
|
|
21
|
+
|
|
22
|
+
export default function slopEditor({ text } = {}) {
|
|
23
|
+
let file;
|
|
24
|
+
let lastSaved = null; // the version this server wrote last, so its own saves don't reload the page
|
|
25
|
+
const read = async () => {
|
|
26
|
+
try {
|
|
27
|
+
return await readFile(file, 'utf8');
|
|
28
|
+
} catch {
|
|
29
|
+
throw new Error(`slop-editor/vite: ${path.relative(process.cwd(), file)} not found. Run \`npx slop-editor install --auto <a .jsx or .tsx file>\` to make it, or write {"version": 1}`);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
name: 'slop-editor',
|
|
34
|
+
configResolved(config) {
|
|
35
|
+
file = path.resolve(config.root, text ?? textFileIn(config.root));
|
|
36
|
+
},
|
|
37
|
+
async transformIndexHtml() {
|
|
38
|
+
const [words, js, css] = await Promise.all([read(), readFile(path.join(here, 'slop.js'), 'utf8'), readFile(path.join(here, 'slop.css'), 'utf8')]);
|
|
39
|
+
// compact, and safe inside a script element
|
|
40
|
+
const block = JSON.stringify(JSON.parse(words)).replace(/</g, '\\u003c');
|
|
41
|
+
return [
|
|
42
|
+
{ tag: 'style', children: css, injectTo: 'head' },
|
|
43
|
+
{ tag: 'script', attrs: { type: 'application/json', id: 'slop-text' }, children: block, injectTo: 'body-prepend' },
|
|
44
|
+
{ tag: 'script', children: js.replace(/<\/script/gi, '<\\/script'), injectTo: 'body' },
|
|
45
|
+
];
|
|
46
|
+
},
|
|
47
|
+
configureServer(server) {
|
|
48
|
+
server.watcher.add(file);
|
|
49
|
+
server.watcher.on('change', async (changed) => {
|
|
50
|
+
if (path.resolve(changed) !== file) return;
|
|
51
|
+
try {
|
|
52
|
+
if (JSON.parse(await readFile(file, 'utf8')).version === lastSaved) return;
|
|
53
|
+
} catch {
|
|
54
|
+
return; // halfway through being written
|
|
55
|
+
}
|
|
56
|
+
server.ws.send({ type: 'full-reload' });
|
|
57
|
+
});
|
|
58
|
+
// the editor's handshake: a GET answers "slop-editor", and a POST saves the whole text block
|
|
59
|
+
server.middlewares.use('/__slop', (req, res) => {
|
|
60
|
+
if (req.method !== 'POST') {
|
|
61
|
+
res.end('slop-editor');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
let body = '';
|
|
65
|
+
req.on('data', (chunk) => { body += chunk; });
|
|
66
|
+
req.on('end', async () => {
|
|
67
|
+
try {
|
|
68
|
+
const sent = JSON.parse(body);
|
|
69
|
+
if (!sent || typeof sent !== 'object' || Array.isArray(sent) || typeof sent.version !== 'number') throw new Error('expected an object with a numeric "version"');
|
|
70
|
+
const current = JSON.parse(await read());
|
|
71
|
+
if (sent.version !== current.version) {
|
|
72
|
+
res.statusCode = 409;
|
|
73
|
+
res.end('the file changed since this page loaded it. Reload the page (copy any unsaved edits first)');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const saved = { ...sent, version: current.version + 1 };
|
|
77
|
+
lastSaved = saved.version;
|
|
78
|
+
await writeFile(`${file}.saving`, JSON.stringify(saved, null, 2) + '\n');
|
|
79
|
+
await rename(`${file}.saving`, file);
|
|
80
|
+
res.end(String(saved.version));
|
|
81
|
+
} catch (e) {
|
|
82
|
+
res.statusCode = 400;
|
|
83
|
+
res.end(e.message);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|