TorstenDittmann / OmniaWrite

A text editor engineered for creative writing.
https://OmniaWrite.com
Apache License 2.0
161 stars 34 forks source link

dep: change momentjs to date-fns #100

Closed dennistobar closed 4 years ago

dennistobar commented 4 years ago

Here is a possible solution to change dependency from momentjs to date-fns. I must use an alias for English locale, because there is no generic English locale. (I use enUS as generic).

As general question, can we use new helpers?, I see some duplication to get relative time and timestamp functions in svelte.

Ah! another drawback: the local must be defined in a new variable, because they need to be defined and date-fns haven't a "global configuration" (date-fns/date-fns#816)

Closes #96 , Closes #99

vercel[bot] commented 4 years ago

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/torstendittmann/omniawrite/nunfq8vsu
✅ Preview: https://omniawrite-git-fork-dennistobar-date-fns.torstendittmann.vercel.app

TorstenDittmann commented 4 years ago

Instead of importing every dependency everytime we are gonna use it, we could import it in the src/utils.js file and export methods from there.

Something like this:

import { formatDistanceToNow, format, fromUnixTime } from "date-fns";
import { es, enUS as en, pt, ru, de } from "date-fns/locale";

export formatDistance = timestamp => formatDistanceToNow(fromUnixTime(timestamp), {
    locale: locales[$settings.language],
    addSuffix: true,
 })

and we can re-use it again like:

import { formatDistance } from "./utils";

formatDistance(date.created);

You probably have to use Svelte get(store) to access the settings store 🙂

Feel free to ask if you have any questions 👍

dennistobar commented 4 years ago

Instead of importing every dependency everytime we are gonna use it, we could import it in the src/utils.js file and export methods from there.

Something like this:

import { formatDistanceToNow, format, fromUnixTime } from "date-fns";
import { es, enUS as en, pt, ru, de } from "date-fns/locale";

export formatDistance = timestamp => formatDistanceToNow(fromUnixTime(timestamp), {
    locale: locales[$settings.language],
    addSuffix: true,
 })

and we can re-use it again like:

import { formatDistance } from "./utils";

formatDistance(date.created);

You probably have to use Svelte get(store) to access the settings store 🙂

Nice to hear about it. I'll refactor today.

TorstenDittmann commented 4 years ago

Amazing work you have done here!

Thanks alot 🙇