dynim-core 1.0.5 → 1.0.7

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.
@@ -23,6 +23,8 @@ export interface BuilderConfig {
23
23
  }>;
24
24
  /** External CodeClient to use (shares state with parent) - if not provided, creates its own */
25
25
  codeClient?: CodeClient;
26
+ /** Function to load a bundle URL (typically bundleManager.load) */
27
+ loadBundle?: (bundleUrl: string) => Promise<void>;
26
28
  }
27
29
  export interface Builder {
28
30
  enter: () => void;
@@ -1 +1 @@
1
- {"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/builder/builder.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAA6B,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AAQxE,OAAO,EAAoB,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AAIlE,MAAM,WAAW,aAAa;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,UAAU,CAAC,EAAE,YAAY,CAAC;IAC1B,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uCAAuC;IACvC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,wCAAwC;IACxC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,qCAAqC;IACrC,UAAU,CAAC,EAAE,MAAM,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACrE,+FAA+F;IAC/F,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB;AAED,MAAM,WAAW,OAAO;IACtB,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,Q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AE,MAAM,OAAO,CAAC;IACxB,cAAc,EAAE,MAAM,OAAO,CAAC;IAC9B,YAAY,EAAE,MAAM,OAAO,CAAC;IAC5B,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED,wBAAgB,aAAa,CAAC,MAAM,GAAE,aAAkB,GAAG,OAAO,CAoejE;;;;AAMD,wBAAyC"}
@@ -15,7 +15,7 @@ import { createCodeClient } from './code-client';
15
15
  import { createBuilderClient } from './builder-client';
16
16
  import { buildElementIdentifier, getRelevantStyles } from './element-utils';
17
17
  export function createBuilder(config = {}) {
18
- const { logo = 'Builder', onExit, onEnter, chatConfig = {}, contentRoot = document.body, pageId = window.location.pathname, apiBase = 'http://localhost:8080', sessionToken, refreshToken, getSession, codeClient: externalCodeClient } = config;
18
+ const { logo = 'Builder', onExit, onEnter, chatConfig = {}, contentRoot = document.body, pageId = window.location.pathname, apiBase = 'http://localhost:8080', sessionToken, refreshToken, getSession, codeClient: externalCodeClient, loadBundle } = config;
19
19
  let isActive = false;
20
20
  let chatWidget = null;
21
21
  let state = null;
@@ -38,6 +38,18 @@ export function createBuilder(config = {}) {
38
38
  </div>
39
39
  <div class="builder-bar__divider"></div>
40
40
  <div class="builder-bar__actions">
41
+ <button class="builder-bar__btn builder-bar__btn--preview">
42
+ <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
43
+ <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>
44
+ </svg>
45
+ Preview
46
+ </button>
47
+ <button class="builder-bar__btn builder-bar__btn--save" disabled>
48
+ <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
49
+ <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
50
+ </svg>
51
+ Save
52
+ </button>
41
53
  <button class="builder-bar__btn builder-bar__btn--exit">
42
54
  <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
43
55
  <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
@@ -50,6 +62,8 @@ export function createBuilder(config = {}) {
50
62
  let builderClient = null;
51
63
  // Track unsubscribe function for external client
52
64
  let unsubscribeFromClient = null;
65
+ // Track unsubscribe function for bundleReady state
66
+ let unsubscribeCodeClient = null;
53
67
  function initCodeClient(stateRef) {
54
68
  // Handler for message updates (used by both internal and external client)
55
69
  const handleMessageUpdate = (message) => {
@@ -235,6 +249,16 @@ export function createBuilder(config = {}) {
235
249
  });
236
250
  initCodeClient(state);
237
251
  initBuilderClient();
252
+ // Subscribe to code client to track bundleReady state
253
+ if (client) {
254
+ unsubscribeCodeClient = client.onMessage((message) => {
255
+ const saveBtn = root.querySelector('.builder-bar__btn--save');
256
+ if (saveBtn) {
257
+ // Enable when bundleReady (changes exist to save)
258
+ saveBtn.disabled = !message.bundleReady;
259
+ }
260
+ });
261
+ }
238
262
  // Pre-warm the cache for faster chat responses (projectId comes from JWT)
239
263
  if (client) {
240
264
  client.warmCache();
@@ -282,6 +306,9 @@ export function createBuilder(config = {}) {
282
306
  freezeOverlay = null;
283
307
  editorState = null;
284
308
  treeState = null;
309
+ // Cleanup bundleReady subscription
310
+ unsubscribeCodeClient?.();
311
+ unsubscribeCodeClient = null;
285
312
  if (chatWidget) {
286
313
  chatWidget.close();
287
314
  chatWidget.root.style.display = 'none';
@@ -292,7 +319,29 @@ export function createBuilder(config = {}) {
292
319
  if (!isActive)
293
320
  return;
294
321
  isActive = false;
295
- // Notify server of exit
322
+ // Step 1: Abandon temp edits on server
323
+ if (client) {
324
+ try {
325
+ await client.abandonCode();
326
+ console.log('[Builder] Temp edits abandoned');
327
+ // Step 2: Load permanent bundle (now that temp edits are gone)
328
+ if (loadBundle) {
329
+ const savedBundleUrl = `${apiBase}/api/code/bundle`;
330
+ console.log('[Builder] Loading permanent bundle:', savedBundleUrl);
331
+ try {
332
+ await loadBundle(savedBundleUrl);
333
+ console.log('[Builder] Permanent bundle loaded');
334
+ }
335
+ catch (bundleError) {
336
+ console.error('[Builder] Failed to load permanent bundle:', bundleError);
337
+ }
338
+ }
339
+ }
340
+ catch (error) {
341
+ console.error('[Builder] Exit/abandon failed:', error);
342
+ }
343
+ }
344
+ // Step 3: Notify server (optional additional cleanup)
296
345
  if (builderClient) {
297
346
  try {
298
347
  await builderClient.exit(pageId, true);
@@ -302,8 +351,56 @@ export function createBuilder(config = {}) {
302
351
  console.error('[Builder] Exit notification failed:', error);
303
352
  }
304
353
  }
354
+ // Step 4: Cleanup UI
305
355
  cleanup();
306
356
  }
357
+ async function handlePreview() {
358
+ if (!builderClient) {
359
+ console.error('[Builder] Builder client not initialized');
360
+ return;
361
+ }
362
+ try {
363
+ console.log('[Builder] Requesting preview...');
364
+ const result = await builderClient.preview(pageId);
365
+ console.log('[Builder] Preview result:', result);
366
+ if (result.previewUrl) {
367
+ window.open(result.previewUrl, '_blank');
368
+ }
369
+ }
370
+ catch (error) {
371
+ console.error('[Builder] Preview failed:', error);
372
+ }
373
+ }
374
+ async function handleSave() {
375
+ if (!client) {
376
+ console.error('[Builder] Code client not initialized');
377
+ return;
378
+ }
379
+ try {
380
+ console.log('[Builder] Saving code edits...');
381
+ await client.saveCode();
382
+ console.log('[Builder] Save complete - exiting builder');
383
+ // After save, the current bundle IS the permanent state
384
+ // Skip abandon/reload and just cleanup UI
385
+ if (!isActive)
386
+ return;
387
+ isActive = false;
388
+ // Notify server of exit (no discard since we saved)
389
+ if (builderClient) {
390
+ try {
391
+ await builderClient.exit(pageId, false);
392
+ console.log('[Builder] Exit notification sent');
393
+ }
394
+ catch (error) {
395
+ console.error('[Builder] Exit notification failed:', error);
396
+ }
397
+ }
398
+ cleanup();
399
+ }
400
+ catch (error) {
401
+ console.error('[Builder] Save failed:', error);
402
+ }
403
+ }
307
404
  root.addEventListener('click', (e) => {
308
405
  const target = e.target;
309
406
  if (target.closest('.builder-bar__btn--exit')) {
@@ -311,6 +408,16 @@ export function createBuilder(config = {}) {
311
408
  exit();
312
409
  return;
313
410
  }
411
+ if (target.closest('.builder-bar__btn--preview')) {
412
+ e.stopPropagation();
413
+ handlePreview();
414
+ return;
415
+ }
416
+ if (target.closest('.builder-bar__btn--save')) {
417
+ e.stopPropagation();
418
+ handleSave();
419
+ return;
420
+ }
314
421
  });
315
422
  document.addEventListener('click', (e) => {
316
423
  const target = e.target;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynim-core",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -109,6 +109,10 @@
109
109
  transform: scale(0.96);
110
110
  }
111
111
 
112
+ .builder-bar__btn--save.builder-bar__btn--saved {
113
+ background: #22c55e !important;
114
+ }
115
+
112
116
  .builder-bar__btn--preview {
113
117
  background: rgba(255, 255, 255, 0.08);
114
118
  color: #94a3b8;