@nonoun/native-playground 0.2.3 → 0.2.4

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/dist/editors.d.ts CHANGED
@@ -1,17 +1,4 @@
1
- import { EditorView } from '@codemirror/view';
2
1
  export type TabName = 'html' | 'css' | 'js';
3
- export interface EditorInstance {
4
- view: EditorView;
5
- getCode(): string;
6
- setCode(code: string): void;
7
- destroy(): void;
8
- }
9
- export interface EditorOptions {
10
- parent: HTMLElement;
11
- initialCode: string;
12
- language: TabName;
13
- readonly?: boolean;
14
- onChange?: (code: string) => void;
15
- }
16
- export declare function createEditor(options: EditorOptions): EditorInstance;
2
+ export declare const TAB_LABELS: Record<TabName, string>;
3
+ export declare function getLanguageExtension(language: TabName): import("@codemirror/language").LanguageSupport;
17
4
  //# sourceMappingURL=editors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"editors.d.ts","sourceRoot":"","sources":["../src/editors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAM9C,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC;AAE5C,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,IAAI,MAAM,CAAC;IAClB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,WAAW,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC;AAcD,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,cAAc,CAgDnE"}
1
+ {"version":3,"file":"editors.d.ts","sourceRoot":"","sources":["../src/editors.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC;AAE5C,eAAO,MAAM,UAAU,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,CAA0C,CAAC;AAE1F,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,OAAO,kDAMrD"}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export { NPlayground } from './playground-element.ts';
2
2
  export { createPlaygroundStore } from './playground-store.ts';
3
3
  export type { PlaygroundStore, ConsoleEntry } from './playground-store.ts';
4
- export { createEditor } from './editors.ts';
5
- export type { EditorInstance, EditorOptions, TabName } from './editors.ts';
4
+ export { getLanguageExtension, TAB_LABELS } from './editors.ts';
5
+ export type { TabName } from './editors.ts';
6
6
  export { buildSrcdoc } from './iframe/template.ts';
7
7
  export type { SrcdocOptions } from './iframe/template.ts';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG3E,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG3E,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG3E,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAChE,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
@@ -166,9 +166,14 @@
166
166
  display: none;
167
167
  }
168
168
 
169
+ /* Native-codemirror — fill panel, strip border-radius */
170
+ :where(native-playground) :where(native-codemirror) {
171
+ height: 100%;
172
+ border-radius: 0;
173
+ }
174
+
169
175
  /* CodeMirror overrides */
170
176
  :where(native-playground) :where(.cm-editor) {
171
- height: 100%;
172
177
  font-size: 0.8125rem;
173
178
  /* Force dark color-scheme so contenteditable text inherits light-on-dark
174
179
  colors even when the parent page uses color-scheme: light dark */
@@ -1,2 +1,2 @@
1
- import { i as e, n as t, r as n, t as r } from "./playground-element-DgFyaxxQ.js";
2
- export { r as NPlayground, t as buildSrcdoc, n as createEditor, e as createPlaygroundStore };
1
+ import { a as e, i as t, n, r, t as i } from "./playground-element-CrsNEOwq.js";
2
+ export { i as NPlayground, r as TAB_LABELS, n as buildSrcdoc, e as createPlaygroundStore, t as getLanguageExtension };
@@ -1,11 +1,9 @@
1
1
  import { CopyController as e, NativeElement as t, PresentController as n, ResizeController as r, signal as i } from "@nonoun/native-ui";
2
- import { EditorState as a } from "@codemirror/state";
3
- import { EditorView as o } from "@codemirror/view";
4
- import { NBaseExtensions as s, NSyntaxHighlighting as c, NTheme as l } from "@nonoun/native-codemirror";
5
- import { html as u } from "@codemirror/lang-html";
6
- import { css as d } from "@codemirror/lang-css";
7
- import { javascript as f } from "@codemirror/lang-javascript";
8
- function p(e, t, n) {
2
+ import "@nonoun/native-codemirror/register";
3
+ import { html as a } from "@codemirror/lang-html";
4
+ import { css as o } from "@codemirror/lang-css";
5
+ import { javascript as s } from "@codemirror/lang-javascript";
6
+ function c(e, t, n) {
9
7
  return {
10
8
  html: i(e),
11
9
  css: i(t),
@@ -24,57 +22,26 @@ function p(e, t, n) {
24
22
  previewTheme: i("")
25
23
  };
26
24
  }
27
- function m(e) {
25
+ const l = {
26
+ html: "HTML",
27
+ css: "CSS",
28
+ js: "JS"
29
+ };
30
+ function u(e) {
28
31
  switch (e) {
29
- case "html": return u();
30
- case "css": return d();
31
- case "js": return f();
32
+ case "html": return a();
33
+ case "css": return o();
34
+ case "js": return s();
32
35
  }
33
36
  }
34
- function h(e) {
35
- let { parent: t, initialCode: n, language: r, readonly: i = !1, onChange: u } = e, d = [
36
- l,
37
- c,
38
- s,
39
- m(r),
40
- a.readOnly.of(i)
41
- ];
42
- u && d.push(o.updateListener.of((e) => {
43
- e.docChanged && u(e.state.doc.toString());
44
- }));
45
- let f = new o({
46
- state: a.create({
47
- doc: n,
48
- extensions: d
49
- }),
50
- parent: t,
51
- root: document
52
- });
53
- return {
54
- view: f,
55
- getCode() {
56
- return f.state.doc.toString();
57
- },
58
- setCode(e) {
59
- f.dispatch({ changes: {
60
- from: 0,
61
- to: f.state.doc.length,
62
- insert: e
63
- } });
64
- },
65
- destroy() {
66
- f.destroy();
67
- }
68
- };
69
- }
70
37
  /**
71
38
  * Escape `<\/script>` sequences in user-provided JS to prevent
72
39
  * breaking out of the inline script tag.
73
40
  */
74
- function g(e) {
41
+ function d(e) {
75
42
  return e.replace(/<\/script>/gi, "<\\/script>");
76
43
  }
77
- function _(e) {
44
+ function f(e) {
78
45
  let { html: t, css: n, js: r, frameworkCss: i, cssUrl: a, registerUrl: o, themeOverrides: s } = e;
79
46
  return `<!DOCTYPE html>
80
47
  <html>
@@ -124,19 +91,15 @@ function _(e) {
124
91
  window.parent.postMessage({ type: 'playground:ready' }, '*');
125
92
  })();
126
93
  <\/script>
127
- <script type="module">${g(r)}<\/script>
94
+ <script type="module">${d(r)}<\/script>
128
95
  </body>
129
96
  </html>`;
130
97
  }
131
- var v = [
98
+ var p = [
132
99
  "html",
133
100
  "css",
134
101
  "js"
135
- ], y = {
136
- html: "HTML",
137
- css: "CSS",
138
- js: "JS"
139
- }, b = class extends t {
102
+ ], m = class extends t {
140
103
  static observedAttributes = [
141
104
  "orientation",
142
105
  "auto-run",
@@ -179,7 +142,11 @@ var v = [
179
142
  }
180
143
  /** Programmatically set code in one or more editors. */
181
144
  setCode(e) {
182
- for (let t of v) e[t] !== void 0 && (this.#e[t].value = e[t], this.#t.get(t)?.setCode(e[t]));
145
+ for (let t of p) if (e[t] !== void 0) {
146
+ this.#e[t].value = e[t];
147
+ let n = this.#t.get(t);
148
+ n && (n.value = e[t]);
149
+ }
183
150
  this.#e.autoRun.value && this.#v();
184
151
  }
185
152
  attributeChangedCallback(e, t, n) {
@@ -211,7 +178,7 @@ var v = [
211
178
  }
212
179
  }
213
180
  setup() {
214
- if (super.setup(), this.#e = p("", "", ""), this.#S(), this.#i = new e(this, { value: () => this.#e[this.#e.activeTab.value].value }), this.#p(), this.#o = new n(this), this.#f) {
181
+ if (super.setup(), this.#e = c("", "", ""), this.#S(), this.#i = new e(this, { value: () => this.#e[this.#e.activeTab.value].value }), this.#p(), this.#o = new n(this), this.#f) {
215
182
  let e = this.#e.orientation.value === "vertical";
216
183
  this.#a = new r(this.#f, {
217
184
  handleSelector: ".pg-resize-handle",
@@ -227,7 +194,7 @@ var v = [
227
194
  }
228
195
  }), this.addEffect(() => {
229
196
  let e = this.#e.activeTab.value;
230
- for (let t = 0; t < v.length; t++) this.#u[t].hidden = v[t] !== e;
197
+ for (let t = 0; t < p.length; t++) this.#u[t].hidden = p[t] !== e;
231
198
  this.#l && this.#l.setAttribute("value", e);
232
199
  }), this.addEffect(() => {
233
200
  let e = this.#e.consoleOpen.value;
@@ -242,9 +209,7 @@ var v = [
242
209
  });
243
210
  }
244
211
  teardown() {
245
- window.removeEventListener("message", this.#k), clearTimeout(this.#r);
246
- for (let e of this.#t.values()) e.destroy();
247
- this.#t.clear(), this.#i.destroy(), this.#a?.destroy(), this.#a = null, this.#o?.destroy(), this.#o = null, this.#n = null, this.#d = null, this.#f = null, this.#l = null, this.#u = [], super.teardown();
212
+ window.removeEventListener("message", this.#k), clearTimeout(this.#r), this.#t.clear(), this.#i.destroy(), this.#a?.destroy(), this.#a = null, this.#o?.destroy(), this.#o = null, this.#n = null, this.#d = null, this.#f = null, this.#l = null, this.#u = [], super.teardown();
248
213
  }
249
214
  #p() {
250
215
  let e = document.createElement("n-card-header"), t = document.createElement("n-toolbar");
@@ -263,25 +228,27 @@ var v = [
263
228
  s.className = "pg-split";
264
229
  let c = document.createElement("div");
265
230
  c.className = "pg-editor";
266
- let l = document.createElement("n-card-header"), u = document.createElement("n-tabs");
267
- u.setAttribute("value", this.#e.activeTab.value), u.setAttribute("size", "sm");
268
- for (let e of v) {
231
+ let u = document.createElement("n-card-header"), d = document.createElement("n-tabs");
232
+ d.setAttribute("value", this.#e.activeTab.value), d.setAttribute("size", "sm");
233
+ for (let e of p) {
269
234
  let t = document.createElement("n-tab");
270
- t.setAttribute("value", e), t.textContent = y[e], u.appendChild(t);
235
+ t.setAttribute("value", e), t.textContent = l[e], d.appendChild(t);
271
236
  }
272
- u.addEventListener("native:change", this.#O), this.#l = u, l.appendChild(u), c.appendChild(l);
273
- for (let e = 0; e < v.length; e++) {
237
+ d.addEventListener("native:change", this.#O), this.#l = d, u.appendChild(d), c.appendChild(u);
238
+ for (let e = 0; e < p.length; e++) {
274
239
  let t = document.createElement("div");
275
- t.className = "pg-code-panel", t.setAttribute("role", "tabpanel"), t.hidden = v[e] !== this.#e.activeTab.value, c.appendChild(t), this.#u.push(t);
240
+ t.className = "pg-code-panel", t.setAttribute("role", "tabpanel"), t.hidden = p[e] !== this.#e.activeTab.value;
241
+ let n = document.createElement("native-codemirror");
242
+ t.appendChild(n), this.#t.set(p[e], n), c.appendChild(t), this.#u.push(t);
276
243
  }
277
- let d = document.createElement("div");
278
- d.className = "pg-console", d.hidden = !this.#e.consoleOpen.value, c.appendChild(d), this.#d = d;
279
244
  let f = document.createElement("div");
280
- f.className = "pg-resize-handle", c.appendChild(f), s.appendChild(c), this.#f = c;
281
- let p = document.createElement("div");
282
- p.className = "pg-preview";
283
- let m = document.createElement("iframe");
284
- m.setAttribute("sandbox", "allow-scripts"), m.setAttribute("title", "Preview"), p.appendChild(m), this.#n = m, s.appendChild(p), this.append(e, s);
245
+ f.className = "pg-console", f.hidden = !this.#e.consoleOpen.value, c.appendChild(f), this.#d = f;
246
+ let m = document.createElement("div");
247
+ m.className = "pg-resize-handle", c.appendChild(m), s.appendChild(c), this.#f = c;
248
+ let h = document.createElement("div");
249
+ h.className = "pg-preview";
250
+ let g = document.createElement("iframe");
251
+ g.setAttribute("sandbox", "allow-scripts"), g.setAttribute("title", "Preview"), h.appendChild(g), this.#n = g, s.appendChild(h), this.append(e, s);
285
252
  }
286
253
  #m(e, t, n) {
287
254
  let r = document.createElement("n-button");
@@ -308,23 +275,20 @@ var v = [
308
275
  }
309
276
  #g() {
310
277
  let e = this.#e.readonly.value;
311
- for (let t = 0; t < v.length; t++) {
312
- let n = v[t], r = this.#u[t], i = this.#e[n], a = h({
313
- parent: r,
314
- initialCode: i.value,
315
- language: n,
316
- readonly: e,
317
- onChange: (e) => {
318
- i.value = e, this.dispatchEvent(new CustomEvent("playground:change", {
319
- bubbles: !0,
320
- detail: {
321
- tab: n,
322
- code: e
323
- }
324
- }));
325
- }
278
+ for (let t of p) {
279
+ let n = this.#t.get(t);
280
+ if (!n) continue;
281
+ let r = this.#e[t];
282
+ n.value = r.value, n.extensions = [u(t)], e && (n.readOnly = !0), n.addEventListener("native:input", (e) => {
283
+ let n = e.detail.value;
284
+ r.value = n, this.dispatchEvent(new CustomEvent("playground:change", {
285
+ bubbles: !0,
286
+ detail: {
287
+ tab: t,
288
+ code: n
289
+ }
290
+ }));
326
291
  });
327
- this.#t.set(n, a);
328
292
  }
329
293
  }
330
294
  /**
@@ -357,7 +321,7 @@ var v = [
357
321
  let e = this.#n;
358
322
  if (!e) return;
359
323
  let t = this.getAttribute("css-url") ?? "", n = this.getAttribute("register-url") ?? "", r = this.#e.html.value, i = this.#e.css.value, a = this.#e.js.value;
360
- this.#e.consoleEntries.value = [], e.srcdoc = _({
324
+ this.#e.consoleEntries.value = [], e.srcdoc = f({
361
325
  html: r,
362
326
  css: i,
363
327
  js: a,
@@ -375,7 +339,9 @@ var v = [
375
339
  }
376
340
  #b() {
377
341
  let { initialHtml: e, initialCss: t, initialJs: n } = this.#e;
378
- this.#e.html.value = e, this.#e.css.value = t, this.#e.js.value = n, this.#t.get("html")?.setCode(e), this.#t.get("css")?.setCode(t), this.#t.get("js")?.setCode(n), this.dispatchEvent(new CustomEvent("playground:reset", {
342
+ this.#e.html.value = e, this.#e.css.value = t, this.#e.js.value = n;
343
+ let r = this.#t.get("html"), i = this.#t.get("css"), a = this.#t.get("js");
344
+ r && (r.value = e), i && (i.value = t), a && (a.value = n), this.dispatchEvent(new CustomEvent("playground:reset", {
379
345
  bubbles: !0,
380
346
  detail: {
381
347
  html: e,
@@ -447,4 +413,4 @@ var v = [
447
413
  }
448
414
  };
449
415
  };
450
- export { p as i, _ as n, h as r, b as t };
416
+ export { c as a, u as i, f as n, l as r, m as t };
@@ -1,4 +1,5 @@
1
1
  import { NativeElement } from '@nonoun/native-ui';
2
+ import '@nonoun/native-codemirror/register';
2
3
  import type { TabName } from './editors.ts';
3
4
  /**
4
5
  * Interactive code playground with live preview.
@@ -1 +1 @@
1
- {"version":3,"file":"playground-element.d.ts","sourceRoot":"","sources":["../src/playground-element.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAuD,MAAM,mBAAmB,CAAC;AAIvG,OAAO,KAAK,EAAkB,OAAO,EAAE,MAAM,cAAc,CAAC;AAM5D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,WAAY,SAAQ,aAAa;;IAC5C,MAAM,CAAC,kBAAkB,WA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,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAqCpF,KAAK,IAAI,IAAI;IA6Fb,QAAQ,IAAI,IAAI;CA2ZjB"}
package/dist/register.js CHANGED
@@ -1,4 +1,4 @@
1
- import { t as e } from "./playground-element-DgFyaxxQ.js";
1
+ import { t as e } from "./playground-element-CrsNEOwq.js";
2
2
  import { define as t } from "@nonoun/native-ui";
3
3
  /**
4
4
  * Register native-playground
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nonoun/native-playground",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Embeddable live code sandbox for @nonoun/native-ui",
5
5
  "license": "MIT",
6
6
  "type": "module",