@dabalabs/lang 0.0.4-beta → 0.0.5-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() {
@@ -1456,7 +1461,7 @@ var DabaLang = class {
1456
1461
  if (this.isSiteTranslated(this.activeLang)) return;
1457
1462
  this.retranslating = this.retranslating.catch(() => void 0).then(async () => {
1458
1463
  if (!this.applier || this.activeLang === null) return;
1459
- this.applier.recrawl();
1464
+ if (this.applier.recrawl() === 0) return;
1460
1465
  try {
1461
1466
  await this.applier.applyLanguage(this.activeLang);
1462
1467
  } catch (err) {