@dabalabs/lang 0.0.4-beta → 0.0.6-beta

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/dabalang.cjs CHANGED
@@ -239,8 +239,20 @@ var TranslationApplier = class {
239
239
  /** Re-crawls the current DOM — call after content is known to have
240
240
  * changed (e.g. after a route change in an SPA). Not called
241
241
  * automatically; dabalang does one crawl per page load. */
242
+ /** Re-crawls, returning how many text nodes were seen for the first
243
+ * time.
244
+ *
245
+ * That count is the signal a caller needs to decide whether anything
246
+ * actually arrived. Applying a language writes to the DOM, which any
247
+ * MutationObserver watching will see as a change — so "did the DOM
248
+ * mutate" cannot distinguish new content from our own echo, and acting
249
+ * on it would loop. "Did previously-unseen text appear" can: our own
250
+ * writes land on nodes already in sourceByNode, so they count zero. */
242
251
  recrawl() {
243
- this.groups = this.absorb(crawlVisibleText(this.root));
252
+ const crawled = crawlVisibleText(this.root);
253
+ const fresh = crawled.reduce((n, { node }) => this.sourceByNode.has(node) ? n : n + 1, 0);
254
+ this.groups = this.absorb(crawled);
255
+ return fresh;
244
256
  }
245
257
  currentLanguage() {
246
258
  return this.activeLang;
@@ -510,13 +522,7 @@ var NavigationWatcher = class {
510
522
  const onPop = () => this.onRouteMaybeChanged();
511
523
  window.addEventListener("popstate", onPop);
512
524
  this.restorers.push(() => window.removeEventListener("popstate", onPop));
513
- this.observer = new MutationObserver(() => {
514
- if (this.currentPath() !== this.lastPath) {
515
- this.schedule();
516
- } else if (this.passesLeft > 0) {
517
- this.schedule();
518
- }
519
- });
525
+ this.observer = new MutationObserver(() => this.schedule());
520
526
  this.observer.observe(document.body, { childList: true, subtree: true });
521
527
  }
522
528
  currentPath() {
@@ -536,11 +542,10 @@ var NavigationWatcher = class {
536
542
  if (path !== this.lastPath) {
537
543
  this.lastPath = path;
538
544
  this.passesLeft = FOLLOW_UP_PASSES;
539
- this.onNavigated();
540
545
  } else if (this.passesLeft > 0) {
541
546
  this.passesLeft -= 1;
542
- this.onNavigated();
543
547
  }
548
+ this.onNavigated();
544
549
  }, SETTLE_MS);
545
550
  }
546
551
  stop() {
@@ -1123,6 +1128,7 @@ function renderLanguageSwitcher(container, targetLanguages, sourceLang, onSelect
1123
1128
  let closing = false;
1124
1129
  const root = document.createElement("div");
1125
1130
  root.className = "dabalang-switcher-root";
1131
+ root.setAttribute(IGNORE_ATTR, "");
1126
1132
  const trigger = document.createElement("button");
1127
1133
  trigger.type = "button";
1128
1134
  trigger.className = "dabalang-trigger";
@@ -1180,6 +1186,7 @@ function renderLanguageSwitcher(container, targetLanguages, sourceLang, onSelect
1180
1186
  if (backdrop) return;
1181
1187
  const overlay = document.createElement("div");
1182
1188
  overlay.className = "dabalang-backdrop";
1189
+ overlay.setAttribute(IGNORE_ATTR, "");
1183
1190
  overlay.addEventListener("mousedown", (e) => {
1184
1191
  if (e.target === overlay) closeModal();
1185
1192
  });
@@ -1456,7 +1463,7 @@ var DabaLang = class {
1456
1463
  if (this.isSiteTranslated(this.activeLang)) return;
1457
1464
  this.retranslating = this.retranslating.catch(() => void 0).then(async () => {
1458
1465
  if (!this.applier || this.activeLang === null) return;
1459
- this.applier.recrawl();
1466
+ if (this.applier.recrawl() === 0) return;
1460
1467
  try {
1461
1468
  await this.applier.applyLanguage(this.activeLang);
1462
1469
  } catch (err) {