morph-embed 0.1.0 → 0.2.1
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 +32 -6
- package/dist/morph-embed.esm.mjs +947 -163
- package/dist/morph-embed.js +947 -163
- package/package.json +1 -1
package/dist/morph-embed.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! Morph Embed SDK v0.1
|
|
1
|
+
/*! Morph Embed SDK v0.2.1 — https://usemorph.xyz — MIT */
|
|
2
2
|
|
|
3
3
|
/* ---- shared/dsl.js ---- */
|
|
4
4
|
/*
|
|
@@ -1320,151 +1320,255 @@
|
|
|
1320
1320
|
|
|
1321
1321
|
|
|
1322
1322
|
/* ---- sdk/src/storage.js ---- */
|
|
1323
|
-
/*
|
|
1324
|
-
* Morph SDK — recipe store.
|
|
1325
|
-
*
|
|
1326
|
-
*
|
|
1327
|
-
*
|
|
1328
|
-
*
|
|
1329
|
-
*
|
|
1330
|
-
*
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
'
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
function
|
|
1398
|
-
const
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
});
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
});
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1323
|
+
/*
|
|
1324
|
+
* Morph SDK — recipe store. SCHEMA v2: a recipe is a LIST OF EDITS
|
|
1325
|
+
* ({id, prompt, ts, enabled, transform}) per page path, which is what makes
|
|
1326
|
+
* per-edit undo, history, and toggling possible in the SDK UI (Nielsen #3,
|
|
1327
|
+
* user control and freedom). v1 recipes (one merged blob) migrate on read.
|
|
1328
|
+
*
|
|
1329
|
+
* Default backing is localStorage (per-browser); a vendor may pass a custom
|
|
1330
|
+
* adapter {get,set,remove} (sync or Promise) to store recipes in their own
|
|
1331
|
+
* backend so a user's generated features follow them across devices.
|
|
1332
|
+
* Storage failures NEVER throw — a broken store must never break the host page.
|
|
1333
|
+
* UMD: Node module in tests, browser global `MorphStorage` in the SDK bundle.
|
|
1334
|
+
*/
|
|
1335
|
+
(function (global, factory) {
|
|
1336
|
+
const api = factory();
|
|
1337
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
|
1338
|
+
else global.MorphStorage = api;
|
|
1339
|
+
})(typeof self !== 'undefined' ? self : this, function () {
|
|
1340
|
+
'use strict';
|
|
1341
|
+
|
|
1342
|
+
const PREFIX = 'morph:sdk:recipe:';
|
|
1343
|
+
const SCHEMA_VERSION = 2;
|
|
1344
|
+
const MAX_PROMPT_LABEL = 300;
|
|
1345
|
+
const MAX_EDITS = 20;
|
|
1346
|
+
|
|
1347
|
+
// Default adapter: window.localStorage, JSON values, fully guarded.
|
|
1348
|
+
function localAdapter() {
|
|
1349
|
+
function ls() {
|
|
1350
|
+
try {
|
|
1351
|
+
return (typeof window !== 'undefined' && window.localStorage) ? window.localStorage : null;
|
|
1352
|
+
} catch (_) { return null; } // SecurityError in sandboxed frames
|
|
1353
|
+
}
|
|
1354
|
+
return {
|
|
1355
|
+
get(key) {
|
|
1356
|
+
const s = ls();
|
|
1357
|
+
if (!s) return undefined;
|
|
1358
|
+
const raw = s.getItem(key);
|
|
1359
|
+
if (raw == null) return undefined;
|
|
1360
|
+
try { return JSON.parse(raw); } catch (_) { return undefined; }
|
|
1361
|
+
},
|
|
1362
|
+
set(key, value) {
|
|
1363
|
+
const s = ls();
|
|
1364
|
+
if (!s) return;
|
|
1365
|
+
s.setItem(key, JSON.stringify(value));
|
|
1366
|
+
},
|
|
1367
|
+
remove(key) {
|
|
1368
|
+
const s = ls();
|
|
1369
|
+
if (!s) return;
|
|
1370
|
+
s.removeItem(key);
|
|
1371
|
+
},
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
function genId() {
|
|
1376
|
+
return 'e_' + Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
// mergeTransforms concats; recipes must UNION. Drop structurally identical
|
|
1380
|
+
// ops (first occurrence wins) so repeating a prompt can't bloat the recipe.
|
|
1381
|
+
function dedupeOps(transform) {
|
|
1382
|
+
if (!transform || !Array.isArray(transform.ops)) return transform;
|
|
1383
|
+
const seen = new Set();
|
|
1384
|
+
const ops = transform.ops.filter((op) => {
|
|
1385
|
+
let key;
|
|
1386
|
+
try { key = JSON.stringify(op); } catch (_) { return true; }
|
|
1387
|
+
if (seen.has(key)) return false;
|
|
1388
|
+
seen.add(key);
|
|
1389
|
+
return true;
|
|
1390
|
+
});
|
|
1391
|
+
return Object.assign({}, transform, { ops });
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// Merge the ENABLED edits, in order, into one transform (deduped). Returns
|
|
1395
|
+
// null when nothing is enabled. mergeFn is DSL.mergeTransforms when the
|
|
1396
|
+
// caller has the DSL; the concat fallback keeps this module dependency-free.
|
|
1397
|
+
function composeEdits(edits, mergeFn) {
|
|
1398
|
+
const enabled = (Array.isArray(edits) ? edits : []).filter((e) => e && e.enabled !== false && e.transform);
|
|
1399
|
+
if (!enabled.length) return null;
|
|
1400
|
+
let merged = enabled[0].transform;
|
|
1401
|
+
for (let i = 1; i < enabled.length; i++) {
|
|
1402
|
+
merged = (typeof mergeFn === 'function')
|
|
1403
|
+
? mergeFn(merged, enabled[i].transform)
|
|
1404
|
+
: Object.assign({}, merged, { ops: (merged.ops || []).concat(enabled[i].transform.ops || []) });
|
|
1405
|
+
}
|
|
1406
|
+
return dedupeOps(merged);
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
// Human label for the whole recipe: unique enabled-edit prompts, in order.
|
|
1410
|
+
function promptLabel(edits) {
|
|
1411
|
+
const seen = new Set();
|
|
1412
|
+
const parts = [];
|
|
1413
|
+
for (const e of Array.isArray(edits) ? edits : []) {
|
|
1414
|
+
if (!e || e.enabled === false) continue;
|
|
1415
|
+
const p = String(e.prompt || '').trim();
|
|
1416
|
+
const k = p.toLowerCase();
|
|
1417
|
+
if (!p || seen.has(k)) continue;
|
|
1418
|
+
seen.add(k);
|
|
1419
|
+
parts.push(p);
|
|
1420
|
+
}
|
|
1421
|
+
return parts.join(' + ').slice(0, MAX_PROMPT_LABEL);
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
// Any stored shape -> v2. v1 recipes (single merged transform) become one
|
|
1425
|
+
// edit so nothing a user saved before the redesign is lost.
|
|
1426
|
+
function migrate(raw, pathKey) {
|
|
1427
|
+
const base = { path: String(pathKey || '/'), enabled: true, schemaVersion: SCHEMA_VERSION, createdAt: (raw && raw.createdAt) || Date.now(), edits: [] };
|
|
1428
|
+
if (!raw || typeof raw !== 'object') return base;
|
|
1429
|
+
if (Array.isArray(raw.edits)) {
|
|
1430
|
+
base.edits = raw.edits.filter((e) => e && typeof e === 'object' && e.transform);
|
|
1431
|
+
return base;
|
|
1432
|
+
}
|
|
1433
|
+
if (raw.transform) {
|
|
1434
|
+
base.edits = [{
|
|
1435
|
+
id: genId(),
|
|
1436
|
+
prompt: String(raw.prompt || 'Earlier changes').slice(0, MAX_PROMPT_LABEL),
|
|
1437
|
+
ts: raw.createdAt || Date.now(),
|
|
1438
|
+
enabled: raw.enabled !== false,
|
|
1439
|
+
transform: raw.transform,
|
|
1440
|
+
}];
|
|
1441
|
+
}
|
|
1442
|
+
return base;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
function createStore(adapter) {
|
|
1446
|
+
const backing = (adapter && typeof adapter === 'object') ? adapter : localAdapter();
|
|
1447
|
+
const keyOf = (pathKey) => PREFIX + String(pathKey || '/');
|
|
1448
|
+
|
|
1449
|
+
// Serialize read-modify-write so rapid appends can't lose edits (same
|
|
1450
|
+
// pattern as extension/src/recipes.js).
|
|
1451
|
+
let writeChain = Promise.resolve();
|
|
1452
|
+
function serialize(fn) {
|
|
1453
|
+
const run = writeChain.then(fn, fn);
|
|
1454
|
+
writeChain = run.then(() => {}, () => {});
|
|
1455
|
+
return run;
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
async function readMigrated(pathKey) {
|
|
1459
|
+
let raw;
|
|
1460
|
+
try { raw = await backing.get(keyOf(pathKey)); } catch (_) { raw = undefined; }
|
|
1461
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
1462
|
+
return migrate(raw, pathKey);
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
async function persist(pathKey, recipe) {
|
|
1466
|
+
await backing.set(keyOf(pathKey), recipe);
|
|
1467
|
+
return recipe;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
// Back-compat read shape: composed transform + merged prompt + the edits.
|
|
1471
|
+
async function loadRecipe(pathKey) {
|
|
1472
|
+
try {
|
|
1473
|
+
const r = await readMigrated(pathKey);
|
|
1474
|
+
if (!r) return null;
|
|
1475
|
+
return {
|
|
1476
|
+
path: r.path,
|
|
1477
|
+
enabled: r.edits.some((e) => e.enabled !== false),
|
|
1478
|
+
prompt: promptLabel(r.edits),
|
|
1479
|
+
transform: composeEdits(r.edits),
|
|
1480
|
+
edits: r.edits,
|
|
1481
|
+
schemaVersion: SCHEMA_VERSION,
|
|
1482
|
+
createdAt: r.createdAt,
|
|
1483
|
+
};
|
|
1484
|
+
} catch (_) { return null; }
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
function saveRecipe(pathKey, recipe) {
|
|
1488
|
+
return serialize(async () => {
|
|
1489
|
+
try {
|
|
1490
|
+
const merged = Object.assign(
|
|
1491
|
+
{ path: String(pathKey || '/'), enabled: true, createdAt: Date.now() },
|
|
1492
|
+
recipe,
|
|
1493
|
+
{ schemaVersion: SCHEMA_VERSION, path: String(pathKey || '/') }
|
|
1494
|
+
);
|
|
1495
|
+
await persist(pathKey, merged);
|
|
1496
|
+
return merged;
|
|
1497
|
+
} catch (_) { return null; }
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
// Add one edit (the unit of undo/history). Returns the new edit or null.
|
|
1502
|
+
function appendEdit(pathKey, transform, meta) {
|
|
1503
|
+
return serialize(async () => {
|
|
1504
|
+
try {
|
|
1505
|
+
const r = (await readMigrated(pathKey)) || migrate(null, pathKey);
|
|
1506
|
+
const edit = {
|
|
1507
|
+
id: genId(),
|
|
1508
|
+
prompt: String((meta && meta.prompt) || '').slice(0, MAX_PROMPT_LABEL),
|
|
1509
|
+
ts: Date.now(),
|
|
1510
|
+
enabled: true,
|
|
1511
|
+
transform,
|
|
1512
|
+
};
|
|
1513
|
+
r.edits.push(edit);
|
|
1514
|
+
if (r.edits.length > MAX_EDITS) r.edits.splice(0, r.edits.length - MAX_EDITS);
|
|
1515
|
+
await persist(pathKey, r);
|
|
1516
|
+
return edit;
|
|
1517
|
+
} catch (_) { return null; }
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
async function listEdits(pathKey) {
|
|
1522
|
+
try {
|
|
1523
|
+
const r = await readMigrated(pathKey);
|
|
1524
|
+
return r ? r.edits : [];
|
|
1525
|
+
} catch (_) { return []; }
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
function setEditEnabled(pathKey, id, enabled) {
|
|
1529
|
+
return serialize(async () => {
|
|
1530
|
+
try {
|
|
1531
|
+
const r = await readMigrated(pathKey);
|
|
1532
|
+
if (!r) return false;
|
|
1533
|
+
const edit = r.edits.find((e) => e.id === id);
|
|
1534
|
+
if (!edit) return false;
|
|
1535
|
+
edit.enabled = enabled !== false;
|
|
1536
|
+
await persist(pathKey, r);
|
|
1537
|
+
return true;
|
|
1538
|
+
} catch (_) { return false; }
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
function removeEdit(pathKey, id) {
|
|
1543
|
+
return serialize(async () => {
|
|
1544
|
+
try {
|
|
1545
|
+
const r = await readMigrated(pathKey);
|
|
1546
|
+
if (!r) return false;
|
|
1547
|
+
const i = r.edits.findIndex((e) => e.id === id);
|
|
1548
|
+
if (i < 0) return false;
|
|
1549
|
+
r.edits.splice(i, 1);
|
|
1550
|
+
await persist(pathKey, r);
|
|
1551
|
+
return true;
|
|
1552
|
+
} catch (_) { return false; }
|
|
1553
|
+
});
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
// v1 API kept for existing callers: appends as an edit under the hood.
|
|
1557
|
+
function appendTransform(pathKey, transform, meta, mergeFn) {
|
|
1558
|
+
return appendEdit(pathKey, transform, meta).then((edit) => (edit ? loadRecipe(pathKey) : null), () => null);
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
function removeRecipe(pathKey) {
|
|
1562
|
+
return serialize(async () => {
|
|
1563
|
+
try { await backing.remove(keyOf(pathKey)); } catch (_) { /* never throws */ }
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
return { loadRecipe, saveRecipe, appendTransform, appendEdit, listEdits, setEditEnabled, removeEdit, removeRecipe };
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
return { createStore, composeEdits, SCHEMA_VERSION };
|
|
1571
|
+
});
|
|
1468
1572
|
|
|
1469
1573
|
|
|
1470
1574
|
/* ---- extension/src/context.js ---- */
|
|
@@ -2165,7 +2269,9 @@
|
|
|
2165
2269
|
function aimActive(on) { if (els && els.aim) els.aim.classList.toggle('on', !!on); }
|
|
2166
2270
|
|
|
2167
2271
|
window.Morph.bar = {
|
|
2168
|
-
|
|
2272
|
+
// No onPick handler -> no picker in this embedding: hide the ⌖ button so
|
|
2273
|
+
// users never see a dead control (the extension always passes onPick).
|
|
2274
|
+
mount(h) { handlers = h || {}; if (!host) build(); if (els && els.aim) els.aim.style.display = handlers.onPick ? '' : 'none'; },
|
|
2169
2275
|
show, hide, toggle,
|
|
2170
2276
|
setStatus, clearStatus, setBusy, showActions,
|
|
2171
2277
|
isVisible: () => visible,
|
|
@@ -2185,6 +2291,447 @@
|
|
|
2185
2291
|
})();
|
|
2186
2292
|
|
|
2187
2293
|
|
|
2294
|
+
/* ---- extension/src/picker.js ---- */
|
|
2295
|
+
/*
|
|
2296
|
+
* Element picker — lets the user click a specific element on the page so the next
|
|
2297
|
+
* reshape is scoped to it (and its descendants) instead of the model guessing
|
|
2298
|
+
* selectors across the whole page.
|
|
2299
|
+
*
|
|
2300
|
+
* start(onPick, onCancel) : enter pick mode (hover highlights, click selects, Esc cancels)
|
|
2301
|
+
* stop() : leave pick mode
|
|
2302
|
+
* selectorFor(el, doc) : PURE — a stable-ish unique CSS selector for el
|
|
2303
|
+
* describe(el) : PURE — a short human label for el
|
|
2304
|
+
*
|
|
2305
|
+
* Attaches to window.Morph.picker. Pure helpers are CJS-exported for tests.
|
|
2306
|
+
*/
|
|
2307
|
+
(function () {
|
|
2308
|
+
'use strict';
|
|
2309
|
+
window.Morph = window.Morph || {};
|
|
2310
|
+
|
|
2311
|
+
// Prefer the spec-correct native CSS.escape (all modern browsers have it). Fall
|
|
2312
|
+
// back (e.g. jsdom / very old engines) to a regex that backslash-escapes any
|
|
2313
|
+
// non-identifier char AND escapes a leading digit per the CSS spec — otherwise
|
|
2314
|
+
// an id like "2col" yields the INVALID selector "#2col", which throws at
|
|
2315
|
+
// querySelectorAll and forces a weaker, possibly non-unique path.
|
|
2316
|
+
function cssEscape(s) {
|
|
2317
|
+
s = String(s);
|
|
2318
|
+
if (typeof window !== 'undefined' && window.CSS && typeof window.CSS.escape === 'function') {
|
|
2319
|
+
return window.CSS.escape(s);
|
|
2320
|
+
}
|
|
2321
|
+
return s.replace(/[^a-zA-Z0-9_-]/g, (c) => '\\' + c).replace(/^([0-9])/, (m, d) => '\\3' + d + ' ');
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
// A reasonably-unique selector: prefer a unique #id (anchoring the path there if
|
|
2325
|
+
// an ancestor has one), else a short `>`-path with :nth-of-type disambiguation.
|
|
2326
|
+
function selectorFor(el, doc) {
|
|
2327
|
+
doc = doc || (el && el.ownerDocument) || (typeof document !== 'undefined' ? document : null);
|
|
2328
|
+
if (!el || el.nodeType !== 1 || !doc) return '';
|
|
2329
|
+
if (el.id) {
|
|
2330
|
+
const sel = '#' + cssEscape(el.id);
|
|
2331
|
+
try { if (doc.querySelectorAll(sel).length === 1) return sel; } catch (_) { /* keep going */ }
|
|
2332
|
+
}
|
|
2333
|
+
const parts = [];
|
|
2334
|
+
let node = el;
|
|
2335
|
+
let depth = 0;
|
|
2336
|
+
while (node && node.nodeType === 1 && node !== doc.body && node !== doc.documentElement && depth < 6) {
|
|
2337
|
+
if (node.id) {
|
|
2338
|
+
const idSel = '#' + cssEscape(node.id);
|
|
2339
|
+
let unique = false;
|
|
2340
|
+
try { unique = doc.querySelectorAll(idSel).length === 1; } catch (_) { unique = false; }
|
|
2341
|
+
if (unique) { parts.unshift(idSel); return parts.join(' > '); }
|
|
2342
|
+
}
|
|
2343
|
+
let part = node.tagName.toLowerCase();
|
|
2344
|
+
const parent = node.parentNode;
|
|
2345
|
+
if (parent && parent.children) {
|
|
2346
|
+
const sameTag = Array.prototype.filter.call(parent.children, (c) => c.tagName === node.tagName);
|
|
2347
|
+
if (sameTag.length > 1) part += ':nth-of-type(' + (sameTag.indexOf(node) + 1) + ')';
|
|
2348
|
+
}
|
|
2349
|
+
parts.unshift(part);
|
|
2350
|
+
node = node.parentNode;
|
|
2351
|
+
depth++;
|
|
2352
|
+
}
|
|
2353
|
+
return parts.join(' > ');
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
function describe(el) {
|
|
2357
|
+
if (!el || el.nodeType !== 1) return 'element';
|
|
2358
|
+
let s = el.tagName.toLowerCase();
|
|
2359
|
+
if (el.id) s += '#' + el.id;
|
|
2360
|
+
else if (el.classList && el.classList.length) s += '.' + el.classList[0];
|
|
2361
|
+
const text = (el.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 24);
|
|
2362
|
+
if (text) s += ' “' + text + '”';
|
|
2363
|
+
return s;
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
// --- interactive pick mode (needs a live DOM) -----------------------------
|
|
2367
|
+
let active = false, overlay = null, current = null, pickCb = null, cancelCb = null;
|
|
2368
|
+
|
|
2369
|
+
function ensureOverlay() {
|
|
2370
|
+
if (overlay) return overlay;
|
|
2371
|
+
overlay = document.createElement('div');
|
|
2372
|
+
overlay.setAttribute('data-morph-pick-overlay', '1');
|
|
2373
|
+
const s = overlay.style;
|
|
2374
|
+
s.position = 'fixed'; s.zIndex = '2147483646'; s.pointerEvents = 'none';
|
|
2375
|
+
s.border = '2px solid #ffffff'; s.boxShadow = '0 0 0 1px #000, 0 0 0 4px rgba(255,255,255,0.25)';
|
|
2376
|
+
s.background = 'rgba(255,255,255,0.06)'; s.borderRadius = '2px'; s.display = 'none';
|
|
2377
|
+
s.transition = 'left .04s linear, top .04s linear, width .04s linear, height .04s linear';
|
|
2378
|
+
return overlay;
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
// Never let the picker target Morph's own UI (the bar host or this overlay).
|
|
2382
|
+
function isOurs(el) {
|
|
2383
|
+
try { return !!(el && el.closest && el.closest('#morph-host-root, [data-morph-pick-overlay]')); }
|
|
2384
|
+
catch (_) { return false; }
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
function moveOverlay(el) {
|
|
2388
|
+
const r = el.getBoundingClientRect();
|
|
2389
|
+
const s = overlay.style;
|
|
2390
|
+
s.display = 'block';
|
|
2391
|
+
s.left = r.left + 'px'; s.top = r.top + 'px'; s.width = r.width + 'px'; s.height = r.height + 'px';
|
|
2392
|
+
}
|
|
2393
|
+
|
|
2394
|
+
function onMove(e) {
|
|
2395
|
+
const el = e.target;
|
|
2396
|
+
if (!el || isOurs(el)) { overlay.style.display = 'none'; current = null; return; }
|
|
2397
|
+
current = el;
|
|
2398
|
+
try { moveOverlay(el); } catch (_) { /* never break the page */ }
|
|
2399
|
+
}
|
|
2400
|
+
function onClick(e) {
|
|
2401
|
+
if (isOurs(e.target)) return; // let our own UI receive clicks
|
|
2402
|
+
e.preventDefault(); e.stopPropagation();
|
|
2403
|
+
finish(current || e.target);
|
|
2404
|
+
}
|
|
2405
|
+
function onKey(e) { if (e.key === 'Escape') { e.preventDefault(); cancel(); } }
|
|
2406
|
+
|
|
2407
|
+
function teardown() {
|
|
2408
|
+
active = false; current = null;
|
|
2409
|
+
try {
|
|
2410
|
+
document.removeEventListener('mousemove', onMove, true);
|
|
2411
|
+
document.removeEventListener('click', onClick, true);
|
|
2412
|
+
document.removeEventListener('keydown', onKey, true);
|
|
2413
|
+
if (document.documentElement) document.documentElement.style.cursor = '';
|
|
2414
|
+
if (overlay) overlay.style.display = 'none';
|
|
2415
|
+
} catch (_) { /* noop */ }
|
|
2416
|
+
}
|
|
2417
|
+
function finish(el) { const cb = pickCb; teardown(); if (cb) { try { cb(el); } catch (_) {} } }
|
|
2418
|
+
function cancel() { const cb = cancelCb; teardown(); if (cb) { try { cb(); } catch (_) {} } }
|
|
2419
|
+
|
|
2420
|
+
function start(onPick, onCancel) {
|
|
2421
|
+
if (active) return;
|
|
2422
|
+
active = true; pickCb = onPick; cancelCb = onCancel;
|
|
2423
|
+
try {
|
|
2424
|
+
ensureOverlay();
|
|
2425
|
+
(document.body || document.documentElement).appendChild(overlay);
|
|
2426
|
+
document.addEventListener('mousemove', onMove, true);
|
|
2427
|
+
document.addEventListener('click', onClick, true);
|
|
2428
|
+
document.addEventListener('keydown', onKey, true);
|
|
2429
|
+
if (document.documentElement) document.documentElement.style.cursor = 'crosshair';
|
|
2430
|
+
} catch (_) { teardown(); }
|
|
2431
|
+
}
|
|
2432
|
+
function stop() { teardown(); }
|
|
2433
|
+
function isActive() { return active; }
|
|
2434
|
+
|
|
2435
|
+
window.Morph.picker = { start, stop, isActive, selectorFor, describe };
|
|
2436
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = { selectorFor, describe };
|
|
2437
|
+
})();
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
/* ---- sdk/src/ui.js ---- */
|
|
2441
|
+
/*
|
|
2442
|
+
* Morph SDK — UI layer: launcher + edits panel + undo toast, isolated in a
|
|
2443
|
+
* closed-over Shadow DOM so host-page CSS can't leak in either direction.
|
|
2444
|
+
*
|
|
2445
|
+
* Every behavior here is deliberate, from the sprint's research pass:
|
|
2446
|
+
* - The launcher is small, corner-pinned, and NEVER auto-opens (Baymard's
|
|
2447
|
+
* live-chat testing: sticky elements that act on their own are "highly
|
|
2448
|
+
* disruptive"; everything is user-initiated).
|
|
2449
|
+
* - First-run pulse fires exactly once (persisted flag) and not at all under
|
|
2450
|
+
* prefers-reduced-motion (NN/g animation-usability).
|
|
2451
|
+
* - Edits apply immediately and the toast offers Undo afterwards — the Gmail
|
|
2452
|
+
* pattern; confirmation-first dialogs habituate and fail (NN/g).
|
|
2453
|
+
* - The panel gives per-edit visibility, toggle, and delete — HAX G17
|
|
2454
|
+
* "provide global controls", Nielsen #3 "user control and freedom".
|
|
2455
|
+
* - The launcher menu never has more than two options (Hick's law), and the
|
|
2456
|
+
* whole surface is at most two disclosure levels deep (NN/g progressive
|
|
2457
|
+
* disclosure).
|
|
2458
|
+
* UMD: Node module in tests, browser global `MorphUI` in the SDK bundle.
|
|
2459
|
+
*/
|
|
2460
|
+
(function (global, factory) {
|
|
2461
|
+
const api = factory();
|
|
2462
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
|
2463
|
+
else global.MorphUI = api;
|
|
2464
|
+
})(typeof self !== 'undefined' ? self : this, function () {
|
|
2465
|
+
'use strict';
|
|
2466
|
+
|
|
2467
|
+
const HOST_ID = 'morph-sdk-ui';
|
|
2468
|
+
const NUDGE_FLAG = 'morph:sdk:nudged';
|
|
2469
|
+
|
|
2470
|
+
const CSS = `
|
|
2471
|
+
:host { all: initial; }
|
|
2472
|
+
* { box-sizing: border-box; font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
|
|
2473
|
+
.morph-ui-root { position: fixed; z-index: 2147483600; bottom: 20px; right: 20px;
|
|
2474
|
+
display: flex; flex-direction: column; align-items: flex-end; gap: 10px; }
|
|
2475
|
+
.morph-ui-root.pos-bottom-left { right: auto; left: 20px; align-items: flex-start; }
|
|
2476
|
+
|
|
2477
|
+
.morph-fab { width: 46px; height: 46px; border-radius: 50%; border: none; cursor: pointer;
|
|
2478
|
+
background: var(--morph-accent, #111114); color: #fff; font-size: 20px; line-height: 1;
|
|
2479
|
+
box-shadow: 0 4px 18px rgba(0,0,0,.28); display: flex; align-items: center; justify-content: center;
|
|
2480
|
+
position: relative; transition: transform .12s ease; }
|
|
2481
|
+
.morph-fab:hover { transform: scale(1.06); }
|
|
2482
|
+
.morph-fab:focus-visible { outline: 2px solid #4c8dff; outline-offset: 2px; }
|
|
2483
|
+
.morph-fab.pulse { animation: morphPulse 1.6s ease-out 2; }
|
|
2484
|
+
@keyframes morphPulse {
|
|
2485
|
+
0% { box-shadow: 0 0 0 0 rgba(76,141,255,.55), 0 4px 18px rgba(0,0,0,.28); }
|
|
2486
|
+
100% { box-shadow: 0 0 0 16px rgba(76,141,255,0), 0 4px 18px rgba(0,0,0,.28); }
|
|
2487
|
+
}
|
|
2488
|
+
@media (prefers-reduced-motion: reduce) { .morph-fab.pulse { animation: none; } }
|
|
2489
|
+
|
|
2490
|
+
.morph-badge { position: absolute; top: -4px; right: -4px; min-width: 18px; height: 18px;
|
|
2491
|
+
border-radius: 9px; background: #e8554d; color: #fff; font-size: 11px; font-weight: 700;
|
|
2492
|
+
display: none; align-items: center; justify-content: center; padding: 0 5px; }
|
|
2493
|
+
.morph-badge.show { display: flex; }
|
|
2494
|
+
|
|
2495
|
+
.morph-menu { display: none; flex-direction: column; background: #fff; border-radius: 10px;
|
|
2496
|
+
box-shadow: 0 8px 30px rgba(0,0,0,.22); overflow: hidden; min-width: 200px; }
|
|
2497
|
+
.morph-menu.open { display: flex; }
|
|
2498
|
+
.morph-menu-item { border: none; background: none; text-align: left; padding: 12px 16px;
|
|
2499
|
+
font-size: 13.5px; color: #17171c; cursor: pointer; display: flex; gap: 9px; align-items: center; }
|
|
2500
|
+
.morph-menu-item:hover { background: #f2f3f7; }
|
|
2501
|
+
|
|
2502
|
+
.morph-panel { display: none; width: 320px; max-height: 420px; overflow: auto; background: #fff;
|
|
2503
|
+
border-radius: 12px; box-shadow: 0 10px 38px rgba(0,0,0,.26); padding: 14px 14px 10px; }
|
|
2504
|
+
.morph-panel.open { display: block; }
|
|
2505
|
+
.morph-panel h3 { margin: 0 0 2px; font-size: 13px; color: #17171c; }
|
|
2506
|
+
.morph-panel .morph-panel-sub { margin: 0 0 10px; font-size: 11px; color: #7a7a85; }
|
|
2507
|
+
.morph-edit-row { display: flex; align-items: center; gap: 8px; padding: 8px 4px;
|
|
2508
|
+
border-top: 1px solid #eef0f4; }
|
|
2509
|
+
.morph-edit-label { flex: 1; min-width: 0; }
|
|
2510
|
+
.morph-edit-label .p { font-size: 12.5px; color: #17171c; overflow: hidden; text-overflow: ellipsis;
|
|
2511
|
+
white-space: nowrap; }
|
|
2512
|
+
.morph-edit-label .t { font-size: 10.5px; color: #9a9aa5; }
|
|
2513
|
+
.morph-edit-row.off .morph-edit-label .p { color: #9a9aa5; text-decoration: line-through; }
|
|
2514
|
+
.morph-edit-toggle { width: 32px; height: 18px; border-radius: 9px; border: none; cursor: pointer;
|
|
2515
|
+
background: #cfd3dc; position: relative; flex: none; }
|
|
2516
|
+
.morph-edit-toggle::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px;
|
|
2517
|
+
border-radius: 50%; background: #fff; transition: left .12s ease; }
|
|
2518
|
+
.morph-edit-toggle[aria-checked="true"] { background: var(--morph-accent, #2f9e63); }
|
|
2519
|
+
.morph-edit-toggle[aria-checked="true"]::after { left: 16px; }
|
|
2520
|
+
.morph-edit-delete { border: none; background: none; color: #b9bcc6; cursor: pointer; font-size: 15px;
|
|
2521
|
+
padding: 4px; flex: none; }
|
|
2522
|
+
.morph-edit-delete:hover { color: #e8554d; }
|
|
2523
|
+
.morph-panel-empty { padding: 14px 4px 10px; font-size: 12.5px; color: #6f6f7a; line-height: 1.5; }
|
|
2524
|
+
.morph-panel-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 10px;
|
|
2525
|
+
padding-top: 8px; border-top: 1px solid #eef0f4; }
|
|
2526
|
+
.morph-panel-reset { border: none; background: none; color: #e8554d; font-size: 12px; cursor: pointer; padding: 4px; }
|
|
2527
|
+
.morph-panel-close { border: none; background: none; color: #7a7a85; font-size: 12px; cursor: pointer; padding: 4px; }
|
|
2528
|
+
|
|
2529
|
+
.morph-toast { position: fixed; left: 50%; transform: translateX(-50%) translateY(8px); bottom: 24px;
|
|
2530
|
+
display: none; align-items: center; gap: 12px; background: #17171c; color: #fff; border-radius: 10px;
|
|
2531
|
+
padding: 11px 16px; font-size: 13px; box-shadow: 0 8px 30px rgba(0,0,0,.32); opacity: 0;
|
|
2532
|
+
transition: opacity .15s ease, transform .15s ease; max-width: min(92vw, 460px); }
|
|
2533
|
+
.morph-toast.show { display: flex; opacity: 1; transform: translateX(-50%) translateY(0); }
|
|
2534
|
+
.morph-toast-action { border: none; background: none; color: #7db4ff; font-weight: 700; font-size: 13px;
|
|
2535
|
+
cursor: pointer; padding: 2px 4px; flex: none; }
|
|
2536
|
+
`;
|
|
2537
|
+
|
|
2538
|
+
function createUI(opts) {
|
|
2539
|
+
opts = opts || {};
|
|
2540
|
+
const win = opts.win;
|
|
2541
|
+
const position = opts.position === undefined ? 'bottom-right' : opts.position;
|
|
2542
|
+
const getFlag = opts.getFlag || defaultGetFlag(win);
|
|
2543
|
+
const setFlag = opts.setFlag || defaultSetFlag(win);
|
|
2544
|
+
const reducedMotion = opts.reducedMotion !== undefined ? !!opts.reducedMotion : prefersReducedMotion(win);
|
|
2545
|
+
|
|
2546
|
+
// Vendor opted out of the launcher entirely -> inert API (they drive
|
|
2547
|
+
// everything through Morph.open() and the bar).
|
|
2548
|
+
if (position === false) {
|
|
2549
|
+
const noop = () => {};
|
|
2550
|
+
return { mount: noop, setCount: noop, showToast: noop, renderPanel: noop, showPanel: noop, hidePanel: noop };
|
|
2551
|
+
}
|
|
2552
|
+
|
|
2553
|
+
let root = null, fab = null, badge = null, menu = null, panel = null, toast = null;
|
|
2554
|
+
let toastTimer = null;
|
|
2555
|
+
let handlers = { onPrimary: null, onPanel: null };
|
|
2556
|
+
let panelHandlers = null;
|
|
2557
|
+
let count = 0;
|
|
2558
|
+
|
|
2559
|
+
function el(tag, cls, text) {
|
|
2560
|
+
const n = win.document.createElement(tag);
|
|
2561
|
+
if (cls) n.className = cls;
|
|
2562
|
+
if (text !== undefined) n.textContent = text;
|
|
2563
|
+
return n;
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
function mount(h) {
|
|
2567
|
+
handlers = h || handlers;
|
|
2568
|
+
if (win.document.getElementById(HOST_ID)) return;
|
|
2569
|
+
const host = el('div');
|
|
2570
|
+
host.id = HOST_ID;
|
|
2571
|
+
const shadow = host.attachShadow({ mode: 'open' });
|
|
2572
|
+
const style = el('style'); style.textContent = CSS;
|
|
2573
|
+
shadow.appendChild(style);
|
|
2574
|
+
|
|
2575
|
+
root = el('div', 'morph-ui-root' + (position === 'bottom-left' ? ' pos-bottom-left' : ''));
|
|
2576
|
+
if (opts.accent) root.style.setProperty('--morph-accent', String(opts.accent));
|
|
2577
|
+
|
|
2578
|
+
panel = el('div', 'morph-panel');
|
|
2579
|
+
menu = el('div', 'morph-menu');
|
|
2580
|
+
fab = el('button', 'morph-fab', '✦');
|
|
2581
|
+
fab.setAttribute('aria-label', 'Customize this page with Morph (Alt+M)');
|
|
2582
|
+
fab.setAttribute('title', 'Customize this page (Alt+M)');
|
|
2583
|
+
badge = el('span', 'morph-badge');
|
|
2584
|
+
badge.setAttribute('aria-hidden', 'true');
|
|
2585
|
+
fab.appendChild(badge);
|
|
2586
|
+
|
|
2587
|
+
toast = el('div', 'morph-toast');
|
|
2588
|
+
toast.setAttribute('role', 'status');
|
|
2589
|
+
toast.setAttribute('aria-live', 'polite');
|
|
2590
|
+
|
|
2591
|
+
fab.addEventListener('click', () => {
|
|
2592
|
+
hidePanel();
|
|
2593
|
+
if (count > 0) { menu.classList.toggle('open'); renderMenu(); return; }
|
|
2594
|
+
menu.classList.remove('open');
|
|
2595
|
+
if (handlers.onPrimary) handlers.onPrimary();
|
|
2596
|
+
});
|
|
2597
|
+
|
|
2598
|
+
// One escape hatch for everything floating (Nielsen #3).
|
|
2599
|
+
win.document.addEventListener('keydown', (e) => {
|
|
2600
|
+
if (e.key === 'Escape') { hidePanel(); menu.classList.remove('open'); }
|
|
2601
|
+
});
|
|
2602
|
+
|
|
2603
|
+
root.appendChild(panel);
|
|
2604
|
+
root.appendChild(menu);
|
|
2605
|
+
root.appendChild(fab);
|
|
2606
|
+
shadow.appendChild(root);
|
|
2607
|
+
shadow.appendChild(toast);
|
|
2608
|
+
win.document.documentElement.appendChild(host);
|
|
2609
|
+
|
|
2610
|
+
// First-run nudge: one pulse, ever, and none under reduced motion.
|
|
2611
|
+
let seen;
|
|
2612
|
+
try { seen = getFlag(NUDGE_FLAG); } catch (_) { seen = true; }
|
|
2613
|
+
if (!seen && !reducedMotion) {
|
|
2614
|
+
fab.classList.add('pulse');
|
|
2615
|
+
try { setFlag(NUDGE_FLAG, '1'); } catch (_) { /* private mode */ }
|
|
2616
|
+
} else if (!seen) {
|
|
2617
|
+
try { setFlag(NUDGE_FLAG, '1'); } catch (_) { /* private mode */ }
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
|
|
2621
|
+
function renderMenu() {
|
|
2622
|
+
if (!menu) return;
|
|
2623
|
+
menu.textContent = '';
|
|
2624
|
+
const make = el('button', 'morph-menu-item', '✦ Make a change');
|
|
2625
|
+
make.addEventListener('click', () => { menu.classList.remove('open'); if (handlers.onPrimary) handlers.onPrimary(); });
|
|
2626
|
+
const edits = el('button', 'morph-menu-item', '☰ Your edits (' + count + ')');
|
|
2627
|
+
edits.addEventListener('click', () => { menu.classList.remove('open'); if (handlers.onPanel) handlers.onPanel(); });
|
|
2628
|
+
menu.appendChild(make);
|
|
2629
|
+
menu.appendChild(edits);
|
|
2630
|
+
}
|
|
2631
|
+
|
|
2632
|
+
function setCount(n) {
|
|
2633
|
+
count = Number(n) || 0;
|
|
2634
|
+
if (!badge) return;
|
|
2635
|
+
badge.textContent = String(count);
|
|
2636
|
+
badge.classList.toggle('show', count > 0);
|
|
2637
|
+
}
|
|
2638
|
+
|
|
2639
|
+
// Act-then-undo toast (the Gmail pattern). One at a time; new replaces old.
|
|
2640
|
+
function showToast(t) {
|
|
2641
|
+
if (!toast) return;
|
|
2642
|
+
t = t || {};
|
|
2643
|
+
if (toastTimer) { clearTimeout(toastTimer); toastTimer = null; }
|
|
2644
|
+
toast.textContent = '';
|
|
2645
|
+
const txt = el('span', 'morph-toast-text', String(t.text || ''));
|
|
2646
|
+
toast.appendChild(txt);
|
|
2647
|
+
if (t.actionLabel) {
|
|
2648
|
+
const act = el('button', 'morph-toast-action', String(t.actionLabel));
|
|
2649
|
+
act.addEventListener('click', () => { hideToast(); if (t.onAction) t.onAction(); });
|
|
2650
|
+
toast.appendChild(act);
|
|
2651
|
+
}
|
|
2652
|
+
toast.classList.add('show');
|
|
2653
|
+
const ttl = typeof t.ttlMs === 'number' ? t.ttlMs : 10000;
|
|
2654
|
+
toastTimer = setTimeout(hideToast, ttl);
|
|
2655
|
+
if (toastTimer && toastTimer.unref) toastTimer.unref();
|
|
2656
|
+
}
|
|
2657
|
+
function hideToast() {
|
|
2658
|
+
if (toast) toast.classList.remove('show');
|
|
2659
|
+
if (toastTimer) { clearTimeout(toastTimer); toastTimer = null; }
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
function fmtWhen(ts) {
|
|
2663
|
+
try {
|
|
2664
|
+
const d = new Date(ts);
|
|
2665
|
+
return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }) + ', ' +
|
|
2666
|
+
d.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' });
|
|
2667
|
+
} catch (_) { return ''; }
|
|
2668
|
+
}
|
|
2669
|
+
|
|
2670
|
+
// "Your edits on this page": visibility + per-edit control (HAX G17).
|
|
2671
|
+
function renderPanel(edits, h) {
|
|
2672
|
+
panelHandlers = h || panelHandlers || {};
|
|
2673
|
+
if (!panel) return;
|
|
2674
|
+
panel.textContent = '';
|
|
2675
|
+
panel.appendChild(el('h3', '', 'Your edits on this page'));
|
|
2676
|
+
panel.appendChild(el('p', 'morph-panel-sub', 'These re-apply every time you visit. Toggle one off to preview the page without it.'));
|
|
2677
|
+
|
|
2678
|
+
if (!edits || !edits.length) {
|
|
2679
|
+
panel.appendChild(el('div', 'morph-panel-empty',
|
|
2680
|
+
'Nothing yet. Click the ✦ button and describe a change — “make this easier to read”, “add a box totaling this table” — and it will live here.'));
|
|
2681
|
+
} else {
|
|
2682
|
+
edits.forEach((e) => {
|
|
2683
|
+
const row = el('div', 'morph-edit-row' + (e.enabled === false ? ' off' : ''));
|
|
2684
|
+
const label = el('div', 'morph-edit-label');
|
|
2685
|
+
label.appendChild(el('div', 'p', e.prompt || 'Untitled edit'));
|
|
2686
|
+
label.appendChild(el('div', 't', fmtWhen(e.ts)));
|
|
2687
|
+
const toggle = el('button', 'morph-edit-toggle');
|
|
2688
|
+
toggle.setAttribute('role', 'switch');
|
|
2689
|
+
toggle.setAttribute('aria-checked', e.enabled === false ? 'false' : 'true');
|
|
2690
|
+
toggle.setAttribute('aria-label', 'Toggle edit: ' + (e.prompt || 'untitled'));
|
|
2691
|
+
toggle.addEventListener('click', () => {
|
|
2692
|
+
if (panelHandlers.onToggle) panelHandlers.onToggle(e.id, e.enabled === false);
|
|
2693
|
+
});
|
|
2694
|
+
const del = el('button', 'morph-edit-delete', '✕');
|
|
2695
|
+
del.setAttribute('aria-label', 'Remove edit: ' + (e.prompt || 'untitled'));
|
|
2696
|
+
del.addEventListener('click', () => { if (panelHandlers.onDelete) panelHandlers.onDelete(e.id); });
|
|
2697
|
+
row.appendChild(label);
|
|
2698
|
+
row.appendChild(toggle);
|
|
2699
|
+
row.appendChild(del);
|
|
2700
|
+
panel.appendChild(row);
|
|
2701
|
+
});
|
|
2702
|
+
}
|
|
2703
|
+
|
|
2704
|
+
const foot = el('div', 'morph-panel-foot');
|
|
2705
|
+
const reset = el('button', 'morph-panel-reset', 'Reset this page');
|
|
2706
|
+
reset.addEventListener('click', () => { if (panelHandlers.onReset) panelHandlers.onReset(); });
|
|
2707
|
+
const close = el('button', 'morph-panel-close', 'Close');
|
|
2708
|
+
close.addEventListener('click', () => { hidePanel(); if (panelHandlers.onClose) panelHandlers.onClose(); });
|
|
2709
|
+
foot.appendChild(reset);
|
|
2710
|
+
foot.appendChild(close);
|
|
2711
|
+
panel.appendChild(foot);
|
|
2712
|
+
}
|
|
2713
|
+
|
|
2714
|
+
function showPanel() { if (panel) { menu && menu.classList.remove('open'); panel.classList.add('open'); } }
|
|
2715
|
+
function hidePanel() { if (panel) panel.classList.remove('open'); }
|
|
2716
|
+
|
|
2717
|
+
return { mount, setCount, showToast, renderPanel, showPanel, hidePanel };
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
function defaultGetFlag(win) {
|
|
2721
|
+
return (k) => { try { return win.localStorage.getItem(k); } catch (_) { return '1'; } };
|
|
2722
|
+
}
|
|
2723
|
+
function defaultSetFlag(win) {
|
|
2724
|
+
return (k, v) => { try { win.localStorage.setItem(k, v); } catch (_) { /* noop */ } };
|
|
2725
|
+
}
|
|
2726
|
+
function prefersReducedMotion(win) {
|
|
2727
|
+
try { return !!(win.matchMedia && win.matchMedia('(prefers-reduced-motion: reduce)').matches); }
|
|
2728
|
+
catch (_) { return false; }
|
|
2729
|
+
}
|
|
2730
|
+
|
|
2731
|
+
return { createUI };
|
|
2732
|
+
});
|
|
2733
|
+
|
|
2734
|
+
|
|
2188
2735
|
/* ---- sdk/src/embed.js ---- */
|
|
2189
2736
|
/*
|
|
2190
2737
|
* Morph SDK — embed entry. Wires the engine (MorphDSL), the vendor registry,
|
|
@@ -2209,6 +2756,56 @@
|
|
|
2209
2756
|
|
|
2210
2757
|
const DEFAULT_BACKEND = 'https://morph-api-234584917638.us-central1.run.app';
|
|
2211
2758
|
|
|
2759
|
+
// Hotkey matching on the PHYSICAL key (e.code), not the produced character:
|
|
2760
|
+
// on macOS Option acts as a character modifier (Option+M types 'µ'), so an
|
|
2761
|
+
// e.key comparison never fires there. Modifier state must match EXACTLY —
|
|
2762
|
+
// that also keeps AltGr (reported as ctrl+alt on Windows) from triggering
|
|
2763
|
+
// an alt-only shortcut while someone types accented characters.
|
|
2764
|
+
function matchesTrigger(e, trigger) {
|
|
2765
|
+
if (!e || typeof trigger !== 'string' || !trigger) return false;
|
|
2766
|
+
// IME guard: isComposing can be false on the first/last composition
|
|
2767
|
+
// keystroke while keyCode is still the spec-mandated 229 — check both.
|
|
2768
|
+
if (e.isComposing || e.keyCode === 229) return false;
|
|
2769
|
+
const parts = trigger.toLowerCase().split('+').map((s) => s.trim()).filter(Boolean);
|
|
2770
|
+
if (!parts.length) return false;
|
|
2771
|
+
const key = parts[parts.length - 1];
|
|
2772
|
+
const mods = new Set(parts.slice(0, -1));
|
|
2773
|
+
const want = {
|
|
2774
|
+
alt: mods.has('alt') || mods.has('option'),
|
|
2775
|
+
ctrl: mods.has('ctrl') || mods.has('control'),
|
|
2776
|
+
shift: mods.has('shift'),
|
|
2777
|
+
meta: mods.has('meta') || mods.has('cmd') || mods.has('command'),
|
|
2778
|
+
};
|
|
2779
|
+
if (!!e.altKey !== want.alt || !!e.ctrlKey !== want.ctrl ||
|
|
2780
|
+
!!e.shiftKey !== want.shift || !!e.metaKey !== want.meta) return false;
|
|
2781
|
+
if (typeof e.code === 'string' && e.code) {
|
|
2782
|
+
if (/^[a-z]$/.test(key)) return e.code === 'Key' + key.toUpperCase();
|
|
2783
|
+
if (/^[0-9]$/.test(key)) return e.code === 'Digit' + key;
|
|
2784
|
+
}
|
|
2785
|
+
return typeof e.key === 'string' && e.key.toLowerCase() === key;
|
|
2786
|
+
}
|
|
2787
|
+
|
|
2788
|
+
// Don't hijack keys while the user is typing in the host app (Mousetrap /
|
|
2789
|
+
// github-hotkey convention). composedPath()[0] sees the REAL target even
|
|
2790
|
+
// when the event comes from inside a shadow root (retargeting would
|
|
2791
|
+
// otherwise hide the input element from us).
|
|
2792
|
+
function isTypingTarget(e) {
|
|
2793
|
+
let t = e && e.target;
|
|
2794
|
+
try { if (e && typeof e.composedPath === 'function') t = e.composedPath()[0] || t; } catch (_) { /* keep target */ }
|
|
2795
|
+
if (!t) return false;
|
|
2796
|
+
const tag = String(t.tagName || '').toUpperCase();
|
|
2797
|
+
return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || t.isContentEditable === true;
|
|
2798
|
+
}
|
|
2799
|
+
|
|
2800
|
+
// Wait narration (NN/g response-time bands: acknowledge <100ms, actively
|
|
2801
|
+
// manage the 2-10s AI band; a moving/staged indicator triples patience).
|
|
2802
|
+
const STAGE_MESSAGES = [
|
|
2803
|
+
'Reading this page…',
|
|
2804
|
+
'Designing your change…',
|
|
2805
|
+
'Still working — bigger changes can take a few seconds…',
|
|
2806
|
+
];
|
|
2807
|
+
const STAGE_DELAYS = [1500, 6000];
|
|
2808
|
+
|
|
2212
2809
|
function createEmbed(deps) {
|
|
2213
2810
|
const config = (deps && deps.config) || {};
|
|
2214
2811
|
const win = deps.win;
|
|
@@ -2216,8 +2813,18 @@
|
|
|
2216
2813
|
const registry = deps.registry;
|
|
2217
2814
|
const store = deps.store;
|
|
2218
2815
|
const bar = deps.bar;
|
|
2816
|
+
const ui = deps.ui || null;
|
|
2817
|
+
const picker = deps.picker || null;
|
|
2219
2818
|
const extractContext = deps.extractContext;
|
|
2220
2819
|
const fetchFn = deps.fetchFn;
|
|
2820
|
+
const stageDelays = deps.stageDelays || STAGE_DELAYS;
|
|
2821
|
+
// Merge enabled edits into one transform (storage exports the canonical
|
|
2822
|
+
// deduping composer; the fallback keeps createEmbed usable without it).
|
|
2823
|
+
const compose = deps.compose || function (edits) {
|
|
2824
|
+
const on = (edits || []).filter((e) => e && e.enabled !== false && e.transform);
|
|
2825
|
+
if (!on.length) return null;
|
|
2826
|
+
return on.slice(1).reduce((m, e) => DSL.mergeTransforms(m, e.transform), on[0].transform);
|
|
2827
|
+
};
|
|
2221
2828
|
|
|
2222
2829
|
let backend = DEFAULT_BACKEND;
|
|
2223
2830
|
let currentHandle = null; // live undo handle for everything applied this session
|
|
@@ -2227,16 +2834,82 @@
|
|
|
2227
2834
|
try { return win.location.pathname || '/'; } catch (_) { return '/'; }
|
|
2228
2835
|
}
|
|
2229
2836
|
|
|
2837
|
+
async function refreshCount() {
|
|
2838
|
+
if (!ui) return;
|
|
2839
|
+
try {
|
|
2840
|
+
const edits = await store.listEdits(pathKey());
|
|
2841
|
+
ui.setCount(edits.filter((e) => e.enabled !== false).length);
|
|
2842
|
+
} catch (_) { /* badge is best-effort */ }
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2230
2845
|
function init() {
|
|
2231
2846
|
if (typeof config.key !== 'string' || !config.key) {
|
|
2232
2847
|
throw new TypeError('Morph: init requires a publishable key (config.key)');
|
|
2233
2848
|
}
|
|
2234
2849
|
backend = (typeof config.backend === 'string' && config.backend)
|
|
2235
2850
|
? config.backend.replace(/\/$/, '') : DEFAULT_BACKEND;
|
|
2236
|
-
bar
|
|
2851
|
+
// onPick only when a picker exists — the bar hides the ⌖ button when
|
|
2852
|
+
// no handler is passed, so users never see a dead control.
|
|
2853
|
+
const handlers = { onSubmit, onUndo, onHide };
|
|
2854
|
+
if (picker) handlers.onPick = onPick;
|
|
2855
|
+
bar.mount(handlers);
|
|
2237
2856
|
return api;
|
|
2238
2857
|
}
|
|
2239
2858
|
|
|
2859
|
+
// ---- element targeting (mirrors extension/src/content.js) --------------
|
|
2860
|
+
let activeTarget = null; // { selector, label } while the user has a pick
|
|
2861
|
+
|
|
2862
|
+
function escapeHtml(s) {
|
|
2863
|
+
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
2864
|
+
}
|
|
2865
|
+
function outlineTarget(el) {
|
|
2866
|
+
try {
|
|
2867
|
+
if (!win.document.getElementById('morph-sdk-target-style')) {
|
|
2868
|
+
const st = win.document.createElement('style');
|
|
2869
|
+
st.id = 'morph-sdk-target-style';
|
|
2870
|
+
st.textContent = '[data-morph-targeted="1"]{outline:2px solid #4c8dff !important;outline-offset:2px !important;}';
|
|
2871
|
+
win.document.documentElement.appendChild(st);
|
|
2872
|
+
}
|
|
2873
|
+
el.setAttribute('data-morph-targeted', '1');
|
|
2874
|
+
} catch (_) { /* cosmetic only */ }
|
|
2875
|
+
}
|
|
2876
|
+
function clearTargetState() {
|
|
2877
|
+
activeTarget = null;
|
|
2878
|
+
try {
|
|
2879
|
+
const el = win.document.querySelector('[data-morph-targeted="1"]');
|
|
2880
|
+
if (el) el.removeAttribute('data-morph-targeted');
|
|
2881
|
+
} catch (_) { /* noop */ }
|
|
2882
|
+
try { bar.clearTarget(); } catch (_) { /* bar optional */ }
|
|
2883
|
+
}
|
|
2884
|
+
function targetHtml() {
|
|
2885
|
+
try {
|
|
2886
|
+
const el = activeTarget && win.document.querySelector(activeTarget.selector);
|
|
2887
|
+
return el ? String(el.outerHTML).slice(0, 4000) : '';
|
|
2888
|
+
} catch (_) { return ''; }
|
|
2889
|
+
}
|
|
2890
|
+
function onPick() {
|
|
2891
|
+
if (!picker || !picker.start) return;
|
|
2892
|
+
try { bar.setStatus('Click any element on the page to target it — Esc to cancel.', ''); } catch (_) { /* noop */ }
|
|
2893
|
+
picker.start((el) => {
|
|
2894
|
+
try {
|
|
2895
|
+
const selector = picker.selectorFor(el, win.document);
|
|
2896
|
+
if (!selector) { bar.setStatus('Couldn’t target that element — try another.', 'err'); return; }
|
|
2897
|
+
activeTarget = { selector, label: picker.describe(el) };
|
|
2898
|
+
outlineTarget(el);
|
|
2899
|
+
bar.setTarget({ label: activeTarget.label, onClear: clearTargetState });
|
|
2900
|
+
bar.setStatus('Targeting <b>' + escapeHtml(activeTarget.label) + '</b>. Now type how to change it.', 'ok');
|
|
2901
|
+
if (bar.focusInput) bar.focusInput();
|
|
2902
|
+
} catch (_) { /* never break the page */ }
|
|
2903
|
+
}, () => {
|
|
2904
|
+
try { bar.aimActive(false); bar.setStatus('Pick cancelled.', ''); } catch (_) { /* noop */ }
|
|
2905
|
+
});
|
|
2906
|
+
}
|
|
2907
|
+
// Closing the bar ends targeting: cancel any in-progress pick, drop the chip.
|
|
2908
|
+
function onHide() {
|
|
2909
|
+
try { if (picker && picker.stop) picker.stop(); } catch (_) { /* noop */ }
|
|
2910
|
+
clearTargetState();
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2240
2913
|
// Confirm gate for actions. window.confirm is trusted browser chrome the
|
|
2241
2914
|
// page can't spoof; a bar-styled dialog can replace it later.
|
|
2242
2915
|
function confirmFn(name, description) {
|
|
@@ -2279,25 +2952,59 @@
|
|
|
2279
2952
|
return { transform: Object.assign({}, transform, { ops }), data, failedCount: failedSources.size };
|
|
2280
2953
|
}
|
|
2281
2954
|
|
|
2955
|
+
// Replace whatever this session applied with `transform` (fresh handle).
|
|
2956
|
+
function applyFresh(transform, data) {
|
|
2957
|
+
if (currentHandle) DSL.undoTransform(currentHandle);
|
|
2958
|
+
const handle = DSL.applyTransform(transform, win.document, { groupId: 'morph-sdk', data, onAction });
|
|
2959
|
+
currentHandle = handle;
|
|
2960
|
+
activeTransform = transform;
|
|
2961
|
+
return handle;
|
|
2962
|
+
}
|
|
2963
|
+
|
|
2282
2964
|
async function applyMerged(incoming, data) {
|
|
2283
2965
|
// Stack on top of anything already applied this session (additive), so
|
|
2284
2966
|
// successive prompts accumulate instead of replacing each other.
|
|
2285
2967
|
const merged = activeTransform ? DSL.mergeTransforms(activeTransform, incoming) : incoming;
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2968
|
+
return applyFresh(merged, data);
|
|
2969
|
+
}
|
|
2970
|
+
|
|
2971
|
+
// Rebuild the applied state from the store (after per-edit remove/toggle).
|
|
2972
|
+
async function reapplyFromStore() {
|
|
2973
|
+
const edits = await store.listEdits(pathKey());
|
|
2974
|
+
const merged = compose(edits);
|
|
2975
|
+
if (!merged || !merged.ops || !merged.ops.length) {
|
|
2976
|
+
if (currentHandle) { DSL.undoTransform(currentHandle); currentHandle = null; }
|
|
2977
|
+
activeTransform = null;
|
|
2978
|
+
return;
|
|
2979
|
+
}
|
|
2980
|
+
const gated = dropUnknownSourceOps(merged);
|
|
2981
|
+
const { transform, data } = await resolveAllData(gated);
|
|
2982
|
+
if (!transform.ops.length) {
|
|
2983
|
+
if (currentHandle) { DSL.undoTransform(currentHandle); currentHandle = null; }
|
|
2984
|
+
activeTransform = null;
|
|
2985
|
+
return;
|
|
2986
|
+
}
|
|
2987
|
+
applyFresh(transform, data);
|
|
2291
2988
|
}
|
|
2292
2989
|
|
|
2293
2990
|
async function onSubmit(prompt) {
|
|
2294
2991
|
try { bar.setBusy(true); bar.showActions(false); } catch (_) { /* bar optional in tests */ }
|
|
2992
|
+
// Acknowledge instantly, then narrate the wait in stages.
|
|
2993
|
+
const stageTimers = [];
|
|
2994
|
+
try {
|
|
2995
|
+
bar.setStatus(STAGE_MESSAGES[0], '');
|
|
2996
|
+
stageTimers.push(setTimeout(() => { try { bar.setStatus(STAGE_MESSAGES[1], ''); } catch (_) { /* noop */ } }, stageDelays[0]));
|
|
2997
|
+
stageTimers.push(setTimeout(() => { try { bar.setStatus(STAGE_MESSAGES[2], ''); } catch (_) { /* noop */ } }, stageDelays[1]));
|
|
2998
|
+
} catch (_) { /* noop */ }
|
|
2999
|
+
const clearStages = () => { while (stageTimers.length) clearTimeout(stageTimers.pop()); };
|
|
3000
|
+
|
|
2295
3001
|
let context = '';
|
|
2296
3002
|
try { context = extractContext() || ''; } catch (_) { /* snapshot is best-effort */ }
|
|
2297
3003
|
|
|
2298
3004
|
let resp;
|
|
2299
3005
|
try {
|
|
2300
3006
|
const body = { prompt, context, url: win.location.href };
|
|
3007
|
+
if (activeTarget) body.target = { selector: activeTarget.selector, html: targetHtml() };
|
|
2301
3008
|
if (registry.hasSources() || registry.hasActions()) body.manifests = registry.manifests();
|
|
2302
3009
|
const r = await fetchFn(backend + '/api/transform', {
|
|
2303
3010
|
method: 'POST',
|
|
@@ -2310,9 +3017,11 @@
|
|
|
2310
3017
|
}
|
|
2311
3018
|
resp = await r.json();
|
|
2312
3019
|
} catch (e) {
|
|
3020
|
+
clearStages();
|
|
2313
3021
|
try { bar.setBusy(false); bar.setStatus('Could not reach Morph: ' + String((e && e.message) || e), 'err'); } catch (_) { /* noop */ }
|
|
2314
3022
|
return;
|
|
2315
3023
|
}
|
|
3024
|
+
clearStages();
|
|
2316
3025
|
|
|
2317
3026
|
try {
|
|
2318
3027
|
const v = DSL.validateTransform(resp && resp.transform);
|
|
@@ -2335,23 +3044,79 @@
|
|
|
2335
3044
|
return;
|
|
2336
3045
|
}
|
|
2337
3046
|
const anchored = DSL.withAnchors ? DSL.withAnchors(transform, win.document) : transform;
|
|
2338
|
-
await store.
|
|
3047
|
+
const edit = await store.appendEdit(pathKey(), anchored, { prompt });
|
|
2339
3048
|
const note = handle.failed ? ' (' + handle.failed + ' op(s) skipped)' : '';
|
|
2340
|
-
|
|
3049
|
+
const explanation = (resp.transform && resp.transform.explanation) || 'Done.';
|
|
3050
|
+
bar.setStatus('✓ ' + explanation + ' Saved for this page.' + note, 'ok');
|
|
2341
3051
|
bar.showActions(true);
|
|
2342
3052
|
try { bar.clearInput(); } catch (_) { /* noop */ }
|
|
3053
|
+
await refreshCount();
|
|
3054
|
+
// Act-then-undo (the Gmail pattern): the change is already saved;
|
|
3055
|
+
// the toast is the low-cost escape hatch — never a confirm dialog.
|
|
3056
|
+
if (ui && edit) {
|
|
3057
|
+
ui.showToast({
|
|
3058
|
+
text: '✓ ' + explanation + ' Saved for this page.',
|
|
3059
|
+
actionLabel: 'Undo',
|
|
3060
|
+
onAction: () => removeEdit(edit.id),
|
|
3061
|
+
});
|
|
3062
|
+
}
|
|
2343
3063
|
} catch (e) {
|
|
2344
3064
|
try { bar.setBusy(false); bar.setStatus('Something went wrong applying that change.', 'err'); } catch (_) { /* noop */ }
|
|
2345
3065
|
}
|
|
2346
3066
|
}
|
|
2347
3067
|
|
|
2348
|
-
|
|
3068
|
+
// Per-edit dismissal (HAX G8/G9): drop ONE edit, keep the rest.
|
|
3069
|
+
async function removeEdit(id) {
|
|
3070
|
+
await store.removeEdit(pathKey(), id);
|
|
3071
|
+
await reapplyFromStore();
|
|
3072
|
+
await refreshCount();
|
|
3073
|
+
try { bar.setStatus('Change removed.', ''); } catch (_) { /* noop */ }
|
|
3074
|
+
if (ui) await openPanelIfRendered();
|
|
3075
|
+
}
|
|
3076
|
+
|
|
3077
|
+
async function toggleEdit(id, enabled) {
|
|
3078
|
+
await store.setEditEnabled(pathKey(), id, enabled);
|
|
3079
|
+
await reapplyFromStore();
|
|
3080
|
+
await refreshCount();
|
|
3081
|
+
if (ui) await openPanelIfRendered();
|
|
3082
|
+
}
|
|
3083
|
+
|
|
3084
|
+
// Full reset — the "emergency exit" (Nielsen #3). Bar's Undo maps here.
|
|
3085
|
+
async function resetPage() {
|
|
2349
3086
|
if (currentHandle) { DSL.undoTransform(currentHandle); currentHandle = null; }
|
|
2350
3087
|
activeTransform = null;
|
|
2351
3088
|
await store.removeRecipe(pathKey());
|
|
3089
|
+
await refreshCount();
|
|
3090
|
+
if (ui) await openPanelIfRendered();
|
|
2352
3091
|
try { bar.setStatus('Reverted this page to its original look.', ''); bar.showActions(false); } catch (_) { /* noop */ }
|
|
2353
3092
|
}
|
|
2354
3093
|
|
|
3094
|
+
async function onUndo() { return resetPage(); }
|
|
3095
|
+
|
|
3096
|
+
// "Your edits on this page" — visibility + per-edit control (HAX G17).
|
|
3097
|
+
let panelRendered = false;
|
|
3098
|
+
async function renderPanel() {
|
|
3099
|
+
if (!ui) return;
|
|
3100
|
+
const edits = await store.listEdits(pathKey());
|
|
3101
|
+
ui.renderPanel(edits, {
|
|
3102
|
+
onToggle: (id, enabled) => toggleEdit(id, enabled),
|
|
3103
|
+
onDelete: (id) => removeEdit(id),
|
|
3104
|
+
onReset: () => {
|
|
3105
|
+
// Destroying ALL edits is the one legitimately confirm-worthy act.
|
|
3106
|
+
let ok = true;
|
|
3107
|
+
try { ok = win.confirm('Remove all your edits on this page?'); } catch (_) { /* headless */ }
|
|
3108
|
+
if (ok) resetPage();
|
|
3109
|
+
},
|
|
3110
|
+
onClose: () => {},
|
|
3111
|
+
});
|
|
3112
|
+
panelRendered = true;
|
|
3113
|
+
}
|
|
3114
|
+
async function openPanelIfRendered() { if (panelRendered) await renderPanel(); }
|
|
3115
|
+
async function openPanel() {
|
|
3116
|
+
await renderPanel();
|
|
3117
|
+
if (ui) ui.showPanel();
|
|
3118
|
+
}
|
|
3119
|
+
|
|
2355
3120
|
// Reapply the saved recipe: CSS immediately (FOUC kill), the full
|
|
2356
3121
|
// transform (with fresh data) once the DOM is ready.
|
|
2357
3122
|
async function applySaved() {
|
|
@@ -2378,9 +3143,10 @@
|
|
|
2378
3143
|
} else {
|
|
2379
3144
|
await full();
|
|
2380
3145
|
}
|
|
3146
|
+
await refreshCount();
|
|
2381
3147
|
}
|
|
2382
3148
|
|
|
2383
|
-
const api = { init, onSubmit, onUndo, applySaved };
|
|
3149
|
+
const api = { init, onSubmit, onUndo, applySaved, removeEdit, toggleEdit, resetPage, openPanel };
|
|
2384
3150
|
return api;
|
|
2385
3151
|
}
|
|
2386
3152
|
|
|
@@ -2396,19 +3162,37 @@
|
|
|
2396
3162
|
const doFetch = typeof window.fetch === 'function'
|
|
2397
3163
|
? window.fetch.bind(window)
|
|
2398
3164
|
: () => Promise.reject(new Error('Morph: fetch unavailable'));
|
|
3165
|
+
// Visible launcher (discoverability: HAX G7 — an invisible hotkey is
|
|
3166
|
+
// not an entry point). config.launcher: false to hide, or
|
|
3167
|
+
// { position: 'bottom-left', accent: '#hex' } to theme.
|
|
3168
|
+
const launcherCfg = config.launcher === false ? { position: false } : (config.launcher || {});
|
|
3169
|
+
const ui = window.MorphUI.createUI({
|
|
3170
|
+
win: window,
|
|
3171
|
+
position: launcherCfg.position === undefined ? 'bottom-right' : launcherCfg.position,
|
|
3172
|
+
accent: launcherCfg.accent,
|
|
3173
|
+
});
|
|
2399
3174
|
const embed = createEmbed({
|
|
2400
3175
|
config, win: window, DSL: window.MorphDSL, registry, store,
|
|
2401
|
-
bar: window.Morph.bar,
|
|
2402
|
-
|
|
3176
|
+
bar: window.Morph.bar, ui, picker: window.Morph.picker,
|
|
3177
|
+
extractContext: window.Morph.extractContext,
|
|
3178
|
+
fetchFn: doFetch, compose: window.MorphStorage.composeEdits,
|
|
2403
3179
|
});
|
|
2404
3180
|
embed.init();
|
|
3181
|
+
ui.mount({
|
|
3182
|
+
onPrimary: () => window.Morph.bar.show(),
|
|
3183
|
+
onPanel: () => { embed.openPanel().catch(() => {}); },
|
|
3184
|
+
});
|
|
2405
3185
|
// Hotkey (default alt+m) + a programmatic opener for vendor-built buttons.
|
|
3186
|
+
// Physical-key matching (e.code) so macOS Option+M — which types 'µ' —
|
|
3187
|
+
// still fires; see matchesTrigger above.
|
|
2406
3188
|
const trigger = typeof config.trigger === 'string' ? config.trigger.toLowerCase() : 'alt+m';
|
|
2407
3189
|
if (trigger !== 'none') {
|
|
2408
|
-
|
|
3190
|
+
// capture:true so host-page handlers can't swallow the shortcut;
|
|
3191
|
+
// typing guard so we never steal keystrokes from the host's inputs.
|
|
2409
3192
|
window.addEventListener('keydown', (e) => {
|
|
2410
|
-
if (
|
|
2411
|
-
|
|
3193
|
+
if (isTypingTarget(e)) return;
|
|
3194
|
+
if (matchesTrigger(e, trigger)) { e.preventDefault(); e.stopPropagation(); window.Morph.bar.toggle(); }
|
|
3195
|
+
}, { capture: true });
|
|
2412
3196
|
}
|
|
2413
3197
|
window.Morph.open = () => window.Morph.bar.show();
|
|
2414
3198
|
embed.applySaved().catch(() => {});
|
|
@@ -2416,5 +3200,5 @@
|
|
|
2416
3200
|
};
|
|
2417
3201
|
}
|
|
2418
3202
|
|
|
2419
|
-
return { createEmbed };
|
|
3203
|
+
return { createEmbed, matchesTrigger, isTypingTarget };
|
|
2420
3204
|
});
|