Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intl.Segmenter isn't available in Firefox #818

Open
PatrikStenmark opened this issue Mar 18, 2024 · 6 comments
Open

Intl.Segmenter isn't available in Firefox #818

PatrikStenmark opened this issue Mar 18, 2024 · 6 comments

Comments

@PatrikStenmark
Copy link
Contributor

It seems that the changes in 89e2e7a broke Silverbullet running in Firefox, due to the usage of Intl.Segmenter. This is only available in Firefox Nightly.

Is there any thoughts around browser compatibility?

Would it be a good idea to add a check for Intl.Segmenter availability and fall back to just splitting by whitespace and taking the n words before/after the index? I might be able to make a PR for that if that seems like a good idea.

@zefhemel
Copy link
Collaborator

Actually we polyfill Intl and Temporal already in the client (the browser's main thread), so it's already available to Space Script: https://github.com/silverbulletmd/silverbullet/blob/main/common/space_script.ts#L12 , what I missed is that this polyfill isn't available in web workers, which plugs run in 🤦🏻

Two possible fixes: also expose this polyfill in plugs code, the other is indeed to fall back to a simpler implementation. I'd have to check how big the polyfill is (because this size would be added to each individual plug JS bundle), but perhaps the fallback you suggest is the more pragmatic solution.

@zefhemel
Copy link
Collaborator

Thinking about this more: let's go with the fallback approach, if you could give that a try I'd much appreciate it!

@PatrikStenmark
Copy link
Contributor Author

I'm willing to try. It might take a couple of days though. My computer decided to spontaneously die so I have to wait for a new one.

@daniel-michel
Copy link
Contributor

I just tried to create a function for displaying a relative time and noticed the issue that I cannot use Intl.RelativeTimeFormat.

It looks like the Intl exported from js-temporal/polyfill exports an Intl object which only has the DateTimeFormat. Also it maybe does not polyfill it, but just exports the original one? https://github.com/js-temporal/temporal-polyfill/blob/e1a1452b712dbaddf1fb856b869667510b6768fc/lib/intl.ts#L41

I guess this also causes some of the properties from Intl to be removed in a space-script. I get this in the terminal when logging Intl in a space-script:

[Object: null prototype] {
  DateTimeFormat: [Function: DateTimeFormat] {
    supportedLocalesOf: [Function (anonymous)]
  }
}
This is the script I tried to write to get relative times.
silverbullet.registerFunction("relativeTime", (time, baseTime = new Date()) => {
  const relForm = new Intl.RelativeTimeFormat('en');
  const relative = (+new Date(time) - +new Date(baseTime)) / 1000;
  const absDiff = Math.abs(relative);
  const units = [
    [1, "second"],
    [60, "minute"],
    [60 * 60, "hour"],
    [60 * 60 * 24, "day"],
    [60 * 60 * 24 * 7, "week"],
    [60 * 60 * 24 * 30, "month"],
    [60 * 60 * 24 * 365, "year"],
  ];
  if (absDiff < 1) {
    return relative < 0 ? "just now" : "now";
  }
  for (let i = 0; i < units.length; i++) {
    const [factor, unit] = units[i];
    if (i === units.length - 1 || absDiff / units[i + 1][0] < 1) {
      return relForm.format(relative / factor, unit);
    }
  }
});

@zefhemel
Copy link
Collaborator

zefhemel commented Apr 4, 2024

@daniel-michel I think you fixed this since with #836 correct?

@daniel-michel
Copy link
Contributor

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants