windcraft-editor 0.1.1 → 0.2.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 CHANGED
@@ -1,48 +1,77 @@
1
+ <p align="center">
2
+ <a href="https://github.com/Tolaj/windcraft"><img src="https://raw.githubusercontent.com/Tolaj/windcraft/main/docs/media/logo-card.png" alt="Windcraft" width="320"></a>
3
+ </p>
4
+
1
5
  # windcraft-editor
2
6
 
7
+ A visual editor for **HTML + Tailwind** that you can put in any web page. Select, drag and style like Figma, and every change is real Tailwind code. No backend, no build step.
8
+
9
+ ## Try it in one file
10
+
11
+ Save this as `index.html`, run `npx serve` next to it, and open the address it prints:
12
+
13
+ ```html
14
+ <div id="root"></div>
15
+ <script type="module">
16
+ import { startEditor } from "https://unpkg.com/windcraft-editor/editor/index.js";
17
+ startEditor(document.getElementById("root"), {
18
+ html: '<h1 class="p-10 text-4xl font-bold">Hello</h1>',
19
+ });
20
+ </script>
21
+ ```
22
+
23
+ That is all. Designs are saved in the browser (localStorage).
24
+
25
+ ## Use it from npm
26
+
3
27
  ```bash
4
28
  npm install windcraft-editor
5
29
  ```
6
30
 
7
- The Windcraft visual editor (HTML + Tailwind): canvas, layers, Properties panel, code panel and pages. It has no backend of its own: the page that mounts it gives it a **host** that loads and saves.
31
+ Serve the `node_modules/windcraft-editor` folder as static files (for example `app.use("/windcraft", express.static("node_modules/windcraft-editor"))`) and import from it:
8
32
 
9
33
  ```js
10
- import { mountEditor } from "windcraft-editor";
11
-
12
- await mountEditor(document.getElementById("root"), {
13
- host, // saving, projects, AI and the address: see design/host.js
14
- data, // the design to open: { id, title, htmlCode, ... }
15
- });
34
+ import { startEditor } from "/windcraft/editor/index.js";
35
+ startEditor(document.getElementById("root"), { html: "<h1>Hello</h1>" });
16
36
  ```
17
37
 
18
- | Piece | Where |
38
+ ### `startEditor(root, options)`
39
+
40
+ | Option | What it does |
19
41
  |---|---|
20
- | What the app must provide | `design/host.js` (the `Host` type) |
21
- | A host with no backend (localStorage) | `demo/demo.js` |
22
- | Run the demo | `npm run demo`, then open http://localhost:4000/editor/demo/ |
23
- | Windcraft's own host | `src/public/js/app-host.js` |
42
+ | `html` | The HTML a new design starts with |
43
+ | `title` | The design's name (default `My design`) |
44
+ | `key` | The localStorage key, so two editors on one site do not share designs |
45
+ | `theme` | `{ brand: "#e11d48", gray: "stone", font: "Poppins", radius: "xl" }` |
46
+
47
+ ## Save to your own server
24
48
 
25
- The package carries the Tailwind logic inside it (`core/`) and reaches it by relative paths, so there is nothing else to map. On a plain page map `windcraft-editor` to `<where you serve the package>/editor/index.js` with an import map; a bundler does that itself (an import map, as in `demo/index.html`, or a bundler). `mountEditor` loads Tailwind's play CDN (v3) unless you pass `tailwind: false`. One editor per page; it takes over the page's size, keys and zoom.
49
+ `startEditor` keeps designs in the browser. To load and save somewhere else, use `mountEditor` and give it a **host**: a few functions (`createComponent`, `updateComponent`, ...) that talk to your backend. The list is in [`editor/design/host.js`](design/host.js), and [`local-host.js`](local-host.js) is a complete one to copy.
50
+
51
+ ```js
52
+ import { mountEditor } from "/windcraft/editor/index.js";
26
53
 
27
- The AI panel and "Generate" need `host.generate`; without a working one they show its error.
54
+ await mountEditor(document.getElementById("root"), {
55
+ host, // your load and save functions
56
+ data: { id, title, htmlCode }, // the design to open
57
+ });
58
+ ```
28
59
 
29
- ## Embedding in a container
60
+ ## Put it inside a part of a page
30
61
 
31
- `embedEditor` puts the editor in an element of your page, isolated in an iframe (its own keys, zoom and styles), so several can share a page and it fills whatever size you give it. Leave out the parts you don't need with `ui`; keep the code box and the details form in your own page.
62
+ `startEditor` and `mountEditor` take over the whole page. To place the editor in one box, use `embedEditor`. It runs in an iframe, so several can share a page.
32
63
 
33
64
  ```js
34
- import { embedEditor } from "windcraft-editor";
65
+ import { embedEditor } from "/windcraft/editor/index.js";
35
66
 
36
- const ed = embedEditor(el, {
37
- host, // saving, as above
38
- data: { htmlCode }, // what to open
39
- ui: { panels: false, nav: false, tools: ["move", "hand", "interact", "frame", "shape", "text", "add"] },
40
- onDetails: (fields) => showMyForm(fields), // the first save of a new design needs a name: you ask, then call saveDetails
67
+ const ed = embedEditor(document.getElementById("box"), {
68
+ host,
69
+ data: { htmlCode: "<h1>Hello</h1>" },
70
+ ui: { panels: false, nav: false }, // hide parts you do not need
41
71
  });
42
- ed.onChange(({ html, dirty, status, source }) => {}); // source: "host" (your setHtml), "editor" (a change there), "status" (save state only)
43
- ed.setHtml(html); ed.getHtml(); ed.isDirty(); ed.save();
44
- await ed.saveDetails({ title, category, description, tags, isPublished, useAsReference, project });
45
- ed.destroy();
72
+
73
+ ed.onChange(({ html }) => console.log(html));
74
+ ed.setHtml("<p>New</p>"); // ed.getHtml(), ed.isDirty(), ed.save(), ed.destroy()
46
75
  ```
47
76
 
48
77
  | `ui` | Effect |
@@ -51,4 +80,9 @@ ed.destroy();
51
80
  | `nav: false` | no Back, page list or project |
52
81
  | `tools: [...]` | only these tools (`move hand interact frame shape text add`); `null` keeps all |
53
82
 
54
- The iframe loads `embed.html` (next to `embed.js`; pass `src` if you serve it elsewhere). It needs the same import map as any editor page. Run `npm run demo` and open `/editor/demo/embed.html` for a working example: a code box and the editor following each other.
83
+ ## Notes
84
+
85
+ - The editor loads Tailwind's play CDN (v3) unless you pass `tailwind: false` because your page already has it.
86
+ - One full-page editor per page. It takes over the page's size and keyboard.
87
+ - Source, demo and issues: https://github.com/Tolaj/windcraft
88
+ - MIT licensed.
package/editor/index.js CHANGED
@@ -9,6 +9,7 @@
9
9
  import { setHost } from "./design/host.js";
10
10
  import { config } from "./design/config.js";
11
11
  import { editorHtml } from "./template.js";
12
+ import { localHost, savedDesign } from "./local-host.js";
12
13
 
13
14
  const here = (path) => new URL(path, import.meta.url).href;
14
15
  const TAILWIND = "https://cdn.tailwindcss.com";
@@ -60,3 +61,21 @@ export async function mountEditor(root, { host, data = {}, back = null, tailwind
60
61
  return (await import("./design/editor/api.js")).api;
61
62
  }
62
63
  export { embedEditor } from "./embed.js";
64
+
65
+ /**
66
+ * The quickest way in: the editor with nothing to set up. Designs are saved in this browser (localStorage).
67
+ * @param {HTMLElement} root
68
+ * @param {{ html?: string, title?: string, key?: string, theme?: object }} [options]
69
+ * html: what a new design starts with; title: its name; key: the localStorage key (use different ones for different editors);
70
+ * theme: { brand, gray, font, radius }
71
+ */
72
+ export async function startEditor(root, { html = "", title = "My design", key = "windcraft", theme } = {}) {
73
+ const categories = [{ group: "Designs", items: [{ slug: "design", name: "Design" }] }];
74
+ const saved = savedDesign(key);
75
+ await mountEditor(root, {
76
+ host: localHost(key),
77
+ data: saved
78
+ ? { id: saved._id, title: saved.title, htmlCode: saved.htmlCode, version: saved.version, category: saved.category || "design", description: saved.description, tags: String(saved.tags || "").split(",").filter(Boolean), categories }
79
+ : { title, htmlCode: html, category: "design", description: "", theme, categories },
80
+ });
81
+ }
@@ -0,0 +1,39 @@
1
+ // A host with no backend: designs are kept in this browser's localStorage. startEditor (index.js) uses it, and so does the demo.
2
+
3
+ const read = (key) => { try { return JSON.parse(localStorage.getItem(key) || "{}"); } catch { return {}; } };
4
+ const write = (key, all) => localStorage.setItem(key, JSON.stringify(all));
5
+ const id = () => Math.random().toString(36).slice(2, 10);
6
+
7
+ /** @param {string} key the localStorage key the designs are kept under */
8
+ export function localHost(key = "windcraft") {
9
+ /** @type {import("./design/host.js").Host} */
10
+ const host = {
11
+ async createComponent(fields) {
12
+ const all = read(key), _id = id();
13
+ all[_id] = { ...fields, _id, version: 1 };
14
+ write(key, all);
15
+ return all[_id];
16
+ },
17
+ async updateComponent(docId, fields) {
18
+ const all = read(key);
19
+ if (!all[docId]) throw new Error("That design isn't saved here");
20
+ const next = { ...all[docId], ...fields };
21
+ if (fields.htmlCode !== undefined && fields.htmlCode !== all[docId].htmlCode) next.version = (all[docId].version || 1) + 1;
22
+ all[docId] = next;
23
+ write(key, all);
24
+ return next;
25
+ },
26
+ async deleteComponent(docId) { const all = read(key); delete all[docId]; write(key, all); },
27
+ async createProject(name) { return { id: id(), name }; },
28
+ async saveLayout() {},
29
+ saveLayoutOnExit() {},
30
+ showDesign(docId) { history.replaceState({}, "", docId ? `?design=${docId}` : location.pathname); },
31
+ openDesign(docId) { location.href = `?design=${docId}`; },
32
+ };
33
+ return host;
34
+ }
35
+
36
+ /** The design the address (?design=...) points at, if it is saved here. */
37
+ export function savedDesign(key = "windcraft") {
38
+ return read(key)[new URLSearchParams(location.search).get("design") || ""] || null;
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "windcraft-editor",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Windcraft's visual HTML + Tailwind editor. Mount it in a page and give it a host that loads and saves designs.",
5
5
  "keywords": [
6
6
  "tailwind",