melonykit 1.18.2 → 1.20.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/dist/index.js CHANGED
@@ -20299,7 +20299,7 @@ function _6(e = {}) {
20299
20299
  let { codeLanguages: t, defaultCodeLanguage: n, addKeymap: r = !0, base: { parser: i } = n6, completeHTMLTags: a = !0, pasteURLAsLink: o = !0, htmlTagLanguage: s = g6 } = e;
20300
20300
  if (!(i instanceof O0)) throw RangeError("Base parser provided to `markdown` should be a Markdown parser");
20301
20301
  let c = e.extensions ? [e.extensions] : [], l = [s.support, e6], u;
20302
- o && l.push(S6), n instanceof oh ? (l.push(n.support), u = n.language) : n && (u = n);
20302
+ o && l.push(C6), n instanceof oh ? (l.push(n.support), u = n.language) : n && (u = n);
20303
20303
  let d = t || u ? i6(t, u) : void 0;
20304
20304
  c.push(iv({
20305
20305
  codeParser: d,
@@ -20329,7 +20329,14 @@ function b6() {
20329
20329
  let e = L3(new w1(yo.create({ extensions: g6 }), 0, !0));
20330
20330
  return y6 = e ? e.options : [];
20331
20331
  }
20332
- var x6 = /code|horizontalrule|html|link|comment|processing|escape|entity|image|mark|url/i, S6 = /*@__PURE__*/ J.domEventHandlers({ paste: (e, t) => {
20332
+ var x6 = /link|processing|image|url/i;
20333
+ function S6(e, t) {
20334
+ let { from: n, to: r } = e;
20335
+ if (r <= t.from || n >= t.to || n < t.from && r > t.to) return !1;
20336
+ let i = e.node, a = i.firstChild, o = i.lastChild;
20337
+ return a && o && a.from == n && o.to == r && /Mark/.test(a.name) && o.name == a.name && (n = a.to, r = o.from), (t.from < n || t.to > r) && (n < t.from || r > t.to);
20338
+ }
20339
+ var C6 = /*@__PURE__*/ J.domEventHandlers({ paste: (e, t) => {
20333
20340
  let { main: n } = t.state.selection;
20334
20341
  if (n.empty) return !1;
20335
20342
  let r = e.clipboardData?.getData("text/plain");
@@ -20339,10 +20346,10 @@ var x6 = /code|horizontalrule|html|link|comment|processing|escape|entity|image|m
20339
20346
  from: n.from,
20340
20347
  to: n.to,
20341
20348
  enter: (e) => {
20342
- (e.from > n.from || x6.test(e.name)) && (a = !0);
20349
+ (x6.test(e.name) || S6(e, n)) && (a = !0);
20343
20350
  },
20344
20351
  leave: (e) => {
20345
- e.to < n.to && (a = !0);
20352
+ (x6.test(e.name) || S6(e, n)) && (a = !0);
20346
20353
  }
20347
20354
  }), !a && (t.dispatch({
20348
20355
  changes: [{
@@ -20355,7 +20362,7 @@ var x6 = /code|horizontalrule|html|link|comment|processing|escape|entity|image|m
20355
20362
  userEvent: "input.paste",
20356
20363
  scrollIntoView: !0
20357
20364
  }), !0);
20358
- } }), C6 = (e) => J.theme({
20365
+ } }), w6 = (e) => J.theme({
20359
20366
  "&": {
20360
20367
  backgroundColor: "transparent",
20361
20368
  color: A.colors.deepOcean,
@@ -20382,7 +20389,7 @@ var x6 = /code|horizontalrule|html|link|comment|processing|escape|entity|image|m
20382
20389
  fontVariantNumeric: "tabular-nums",
20383
20390
  opacity: .5
20384
20391
  }
20385
- }), w6 = [
20392
+ }), T6 = [
20386
20393
  lh.of(" "),
20387
20394
  Vp(),
20388
20395
  hg(),
@@ -20394,8 +20401,8 @@ var x6 = /code|horizontalrule|html|link|comment|processing|escape|entity|image|m
20394
20401
  ...S1,
20395
20402
  ...Pg
20396
20403
  ])
20397
- ], T6 = 1e3;
20398
- function E6({ width: i, language: a = "markdown", validateSyntax: o = !0, readOnly: s = !1, showLineNumbers: l = !0, value: d = "", onChange: f }) {
20404
+ ], E6 = 1e3;
20405
+ function D6({ width: i, language: a = "markdown", validateSyntax: o = !0, readOnly: s = !1, showLineNumbers: l = !0, value: d = "", onChange: f }) {
20399
20406
  let p = u(null), m = u(null), h = u(f), g = u(o), _ = u(null), v = e(!1), y = e(!0);
20400
20407
  c(() => {
20401
20408
  h.current = f;
@@ -20408,14 +20415,14 @@ function E6({ width: i, language: a = "markdown", validateSyntax: o = !0, readOn
20408
20415
  return m.current = new J({
20409
20416
  doc: d ?? "",
20410
20417
  extensions: [
20411
- w6,
20418
+ T6,
20412
20419
  l ? pm() : [],
20413
20420
  yo.readOnly.of(s),
20414
20421
  J.editable.of(!s),
20415
20422
  J.contentAttributes.of({ tabindex: s ? "-1" : "0" }),
20416
20423
  J.lineWrapping,
20417
- D6(a),
20418
- C6(l),
20424
+ O6(a),
20425
+ w6(l),
20419
20426
  e
20420
20427
  ],
20421
20428
  parent: p.current
@@ -20444,12 +20451,12 @@ function E6({ width: i, language: a = "markdown", validateSyntax: o = !0, readOn
20444
20451
  S(), g.current && (_.current = setTimeout(() => {
20445
20452
  let e = m.current?.state;
20446
20453
  if (!e || !g.current) return;
20447
- let t = O6(e);
20454
+ let t = k6(e);
20448
20455
  y.value = t, h.current?.({
20449
20456
  value: e.doc.toString(),
20450
20457
  isValid: t
20451
20458
  });
20452
- }, T6));
20459
+ }, E6));
20453
20460
  }
20454
20461
  function S() {
20455
20462
  clearTimeout(_.current), _.current = null;
@@ -20472,12 +20479,12 @@ function E6({ width: i, language: a = "markdown", validateSyntax: o = !0, readOn
20472
20479
  })]
20473
20480
  })] }) : null] });
20474
20481
  }
20475
- function D6(e) {
20482
+ function O6(e) {
20476
20483
  if (e === "markdown") return _6();
20477
20484
  if (e === "html") return W3();
20478
20485
  throw Error(`Unsupported editor language "${e}".`);
20479
20486
  }
20480
- function O6(e) {
20487
+ function k6(e) {
20481
20488
  if (!e.doc.toString().trim()) return !1;
20482
20489
  let t = Xm(e, e.doc.length, 100);
20483
20490
  if (!t) return !1;
@@ -20486,4 +20493,4 @@ function O6(e) {
20486
20493
  e.type.isError && (n = !1);
20487
20494
  } }), n;
20488
20495
  }
20489
- export { Pr as Accordion, Dr as Actions, Ar as AppWrapper, Ur as Bar, Gr as Box, wr as Button, oi as Checkbox, bi as Counter, ei as Dropdown, E6 as Editor, Yr as Grid, Mr as Headline, Tt as Icon, qr as Layout, ii as OptionGroup, xr as PopupBox, _i as SectionToggleBox, ni as Slider, Zr as Space, pi as Switch, Si as TabBox, wi as TabContainer, ki as Table, hi as Tag, vr as Text, Qr as TextField, A as Theme, Rr as Toolbar };
20496
+ export { Pr as Accordion, Dr as Actions, Ar as AppWrapper, Ur as Bar, Gr as Box, wr as Button, oi as Checkbox, bi as Counter, ei as Dropdown, D6 as Editor, Yr as Grid, Mr as Headline, Tt as Icon, qr as Layout, ii as OptionGroup, xr as PopupBox, _i as SectionToggleBox, ni as Slider, Zr as Space, pi as Switch, Si as TabBox, wi as TabContainer, ki as Table, hi as Tag, vr as Text, Qr as TextField, A as Theme, Rr as Toolbar };
@@ -1,27 +1,28 @@
1
1
  import { EventEmitter } from 'node:events';
2
2
 
3
- const emitter = new EventEmitter();
4
- const handlers = new Map();
5
- const clients = new Set();
3
+ const _emitter = new EventEmitter();
4
+ const _handlers = new Map();
5
+ const _clients = new Set();
6
+ const _connectedApps = new WeakSet();
6
7
 
7
- emitter.setMaxListeners(50);
8
+ _emitter.setMaxListeners(50);
8
9
 
9
10
  export function handle(invocationName, handler) {
10
- if (handlers.has(invocationName)) {
11
+ if (_handlers.has(invocationName)) {
11
12
  throw new Error(`Handle - Handler already registered for "${invocationName}".`);
12
13
  }
13
14
 
14
- handlers.set(invocationName, handler);
15
+ _handlers.set(invocationName, handler);
15
16
 
16
17
  return () => {
17
- if (handlers.get(invocationName) === handler) {
18
- handlers.delete(invocationName);
18
+ if (_handlers.get(invocationName) === handler) {
19
+ _handlers.delete(invocationName);
19
20
  }
20
21
  };
21
22
  };
22
23
 
23
24
  export async function invoke(invocationName, data) {
24
- const handler = handlers.get(invocationName);
25
+ const handler = _handlers.get(invocationName);
25
26
 
26
27
  if (!handler) {
27
28
  throw new Error(`Invoke - No handler registered for "${invocationName}".`);
@@ -36,18 +37,18 @@ export function subscribe(eventNames, listener) {
36
37
  )];
37
38
 
38
39
  for (const eventName of names) {
39
- emitter.on(eventName, listener);
40
+ _emitter.on(eventName, listener);
40
41
  }
41
42
 
42
43
  return () => {
43
44
  for (const eventName of names) {
44
- emitter.off(eventName, listener);
45
+ _emitter.off(eventName, listener);
45
46
  }
46
47
  };
47
48
  };
48
49
 
49
50
  export async function publish(eventName, data) {
50
- const listeners = emitter.listeners(eventName);
51
+ const listeners = _emitter.listeners(eventName);
51
52
 
52
53
  if (listeners.length === 0) return undefined;
53
54
 
@@ -61,6 +62,8 @@ export async function publish(eventName, data) {
61
62
  };
62
63
 
63
64
  export function connectClientEvents(app) {
65
+ if (_connectedApps.has(app)) return;
66
+
64
67
  app.get('/api/client-events', (req, res) => {
65
68
  res.setHeader('Content-Type', 'text/event-stream');
66
69
  res.setHeader('Cache-Control', 'no-cache');
@@ -68,13 +71,15 @@ export function connectClientEvents(app) {
68
71
 
69
72
  res.flushHeaders();
70
73
 
71
- clients.add(res);
74
+ _clients.add(res);
72
75
 
73
76
  // Immediately establish the stream.
74
77
  res.write(': connected\n\n');
75
78
 
76
- req.on('close', () => clients.delete(res));
79
+ req.on('close', () => _clients.delete(res));
77
80
  });
81
+
82
+ _connectedApps.add(app);
78
83
  };
79
84
 
80
85
  export function sendClientEvent(type, data = null) {
@@ -82,7 +87,7 @@ export function sendClientEvent(type, data = null) {
82
87
  `event: ${type}\n` +
83
88
  `data: ${JSON.stringify(data)}\n\n`;
84
89
 
85
- for (const client of clients) {
90
+ for (const client of _clients) {
86
91
  client.write(message);
87
92
  }
88
93
  };
@@ -10,12 +10,11 @@ export async function importModules(srcFolder, filePathPatterns, callback) {
10
10
 
11
11
  for (const filePath of filePaths) {
12
12
  const module = await import(pathToFileURL(filePath).href);
13
-
14
13
  await callback?.(module);
15
14
  }
16
15
  };
17
16
 
18
- export async function importRestEndpoints(srcFolder, filePathPatterns, app) {
17
+ export async function initRestEndpoints(srcFolder, filePathPatterns, app) {
19
18
  await importModules(srcFolder, filePathPatterns, module => {
20
19
  const endpoints = Array.isArray(module.default)
21
20
  ? module.default
@@ -32,11 +31,45 @@ export async function importRestEndpoints(srcFolder, filePathPatterns, app) {
32
31
  connectClientEvents(app);
33
32
  };
34
33
 
34
+ /** Registers a GraphQL route from schema modules and enables client events. */
35
+ export async function initGraphqlEndpoints(srcFolder, filePathPatterns, app) {
36
+ const graphqlRuntime = await import('graphql');
37
+ const { graphql } = graphqlRuntime;
38
+ const modules = [];
39
+
40
+ await importModules(srcFolder, filePathPatterns, module => modules.push(module));
41
+
42
+ const schema = _createGraphqlSchema(graphqlRuntime, modules);
43
+
44
+ app.post('/api/graphql', _createEndpointHandler(async ({
45
+ req, respondSuccess, invoke
46
+ }) => {
47
+ const { query, variables, operationName } = req.body ?? {};
48
+
49
+ if (typeof query !== 'string' || !query.trim()) {
50
+ return respondSuccess({
51
+ errors: [{ message: 'Expected a GraphQL query.' }]
52
+ }, 400);
53
+ }
54
+
55
+ const result = await graphql({
56
+ schema,
57
+ source: query,
58
+ variableValues: variables,
59
+ operationName,
60
+ contextValue: { req, invoke }
61
+ });
62
+
63
+ return respondSuccess(result);
64
+ }));
65
+
66
+ connectClientEvents(app);
67
+ };
68
+
35
69
 
36
70
 
37
71
  //
38
72
  // Helpers
39
-
40
73
  function _createEndpointHandler(expressHandler) {
41
74
  return async function handler(req, res) {
42
75
  const context = {
@@ -64,3 +97,70 @@ function _createEndpointHandler(expressHandler) {
64
97
  }
65
98
  };
66
99
  };
100
+
101
+ function _createGraphqlSchema(graphql, modules) {
102
+ const { assertValidSchema, buildSchema } = graphql;
103
+ const typeDefs = [];
104
+ const operations = {
105
+ Query: { fields: [], resolvers: new Map() },
106
+ Mutation: { fields: [], resolvers: new Map() }
107
+ };
108
+
109
+ for (const module of modules) {
110
+ const { queries, mutations } = module.default ?? {};
111
+ if (!queries && !mutations) {
112
+ throw new TypeError(
113
+ 'GraphQL modules must default-export queries or mutations.'
114
+ );
115
+ }
116
+
117
+ _addOperation('Query', queries);
118
+ _addOperation('Mutation', mutations);
119
+ }
120
+
121
+ for (const [name, { fields }] of Object.entries(operations)) {
122
+ if (fields.length) {
123
+ typeDefs.push(`type ${name} {\n${fields.join('\n')}\n}`);
124
+ }
125
+ }
126
+
127
+ const schema = buildSchema(typeDefs.join('\n'));
128
+ assertValidSchema(schema);
129
+
130
+ for (const [name, { resolvers }] of Object.entries(operations)) {
131
+ const fields = schema.getType(name)?.getFields();
132
+
133
+ for (const [fieldName, resolver] of resolvers) {
134
+ const field = fields?.[fieldName];
135
+ if (!field || typeof resolver !== 'function') {
136
+ throw new Error(
137
+ `Invalid GraphQL resolver: ${name}.${fieldName}`
138
+ );
139
+ }
140
+
141
+ field.resolve = (_source, args, context, info) => (
142
+ resolver(args, context, info)
143
+ );
144
+ }
145
+ }
146
+
147
+ return schema;
148
+
149
+ function _addOperation(name, operation) {
150
+ const { types, schemas, ...methods } = operation ?? {};
151
+ const { fields, resolvers } = operations[name];
152
+
153
+ if (types) typeDefs.push(types);
154
+ if (schemas?.trim()) fields.push(schemas);
155
+
156
+ for (const [fieldName, resolver] of Object.entries(methods)) {
157
+ if (resolvers.has(fieldName)) {
158
+ throw new Error(
159
+ `Duplicate GraphQL resolver: ${name}.${fieldName}`
160
+ );
161
+ }
162
+
163
+ resolvers.set(fieldName, resolver);
164
+ }
165
+ };
166
+ };
@@ -1,3 +1,3 @@
1
1
  export { default as debug } from './_debug.js';
2
- export { importModules, importRestEndpoints } from './_imports.js';
2
+ export { importModules, initRestEndpoints, initGraphqlEndpoints } from './_imports.js';
3
3
  export { handle, invoke, subscribe, publish, sendClientEvent } from './_events.js';
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "author": "Vadim Hermann",
3
3
  "name": "melonykit",
4
- "version": "1.18.2",
4
+ "version": "1.20.0",
5
5
  "type": "module",
6
6
  "description": "Preact based ui and utility functions toolkit",
7
7
  "main": "./dist/index.js",
8
8
  "module": "./dist/index.js",
9
9
  "scripts": {
10
- "build": "NODE_ENV=production vite build && cp -R src/server dist/server",
10
+ "build": "vite build && cp -R src/server dist/server",
11
11
  "dev": "vite",
12
12
  "test": "playwright test",
13
13
  "prepack": "npm run build"
@@ -32,13 +32,14 @@
32
32
  "dependencies": {
33
33
  "@codemirror/commands": "6.11.2",
34
34
  "@codemirror/lang-html": "6.4.12",
35
- "@codemirror/lang-markdown": "6.5.2",
35
+ "@codemirror/lang-markdown": "6.5.3",
36
36
  "@codemirror/language": "6.13.1",
37
37
  "@codemirror/state": "6.7.6",
38
38
  "@codemirror/view": "6.44.2",
39
39
  "@emotion/css": "11.13.5",
40
40
  "dompurify": "3.5.0",
41
- "glob": "14.0.6"
41
+ "glob": "14.0.6",
42
+ "graphql": "17.0.2"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@pivanov/vite-plugin-svg-sprite": "3.1.8",