svelte-intlayer 9.8.4 → 9.9.0
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 +1 -0
- package/dist/client/useDictionaryAsync.d.ts +3 -4
- package/dist/client/useDictionaryAsync.js +19 -28
- package/dist/client/useDictionaryDynamic.js +22 -90
- package/dist/editor/editedContentSource.d.ts +7 -0
- package/dist/editor/editedContentSource.js +25 -0
- package/dist/html/HTMLWithSelector.svelte +5 -1
- package/dist/markdown/MarkdownWithSelector.svelte +4 -3
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -190,6 +190,7 @@ Explore our comprehensive documentation to get started with Intlayer and learn h
|
|
|
190
190
|
<li><a href="https://intlayer.org/doc/environment/vite-and-react" rel=''>Vite + React using Compiler</a></li>
|
|
191
191
|
<li><a href="https://intlayer.org/doc/environment/vite-and-react/react-router-v7" rel=''>React-router-v7</a></li>
|
|
192
192
|
<li><a href="https://intlayer.org/doc/environment/remix-3" rel=''>Remix 3</a></li>
|
|
193
|
+
<li><a href="https://intlayer.org/doc/environment/vinext" rel=''>vinext (Next.js on Vite)</a></li>
|
|
193
194
|
<li><a href="https://intlayer.org/doc/environment/tanstack-start" rel=''>Tanstack start</a>
|
|
194
195
|
<ul>
|
|
195
196
|
<li><a href="https://intlayer.org/doc/environment/tanstack-start/solid" rel=''>Solid</a></li>
|
|
@@ -3,9 +3,8 @@ import type { LocalesValues, StrictModeLocaleMap } from '@intlayer/types/module_
|
|
|
3
3
|
import { type Readable } from 'svelte/store';
|
|
4
4
|
/**
|
|
5
5
|
* Svelte hook for handling dynamic dictionary loading
|
|
6
|
-
* @param dictionaryPromise
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
9
|
-
* @returns Reactive store with loaded dictionary content
|
|
6
|
+
* @param dictionaryPromise Locale-keyed dictionary loaders
|
|
7
|
+
* @param locale Target locale (optional, defaults to the context locale)
|
|
8
|
+
* @returns Reactive store with the loaded dictionary, `null` while loading
|
|
10
9
|
*/
|
|
11
10
|
export declare const useDictionaryAsync: <const T extends Dictionary>(dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>, locale?: LocalesValues) => Promise<Readable<T | null>>;
|
|
@@ -1,38 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadDynamicDictionary } from '@intlayer/core/dictionaryManipulator';
|
|
2
|
+
import { derived } from 'svelte/store';
|
|
2
3
|
import { getIntlayerContext } from './intlayerContext';
|
|
3
4
|
import { intlayerStore } from './intlayerStore';
|
|
4
5
|
/**
|
|
5
6
|
* Svelte hook for handling dynamic dictionary loading
|
|
6
|
-
* @param dictionaryPromise
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
9
|
-
* @returns Reactive store with loaded dictionary content
|
|
7
|
+
* @param dictionaryPromise Locale-keyed dictionary loaders
|
|
8
|
+
* @param locale Target locale (optional, defaults to the context locale)
|
|
9
|
+
* @returns Reactive store with the loaded dictionary, `null` while loading
|
|
10
10
|
*/
|
|
11
11
|
export const useDictionaryAsync = async (dictionaryPromise, locale) => {
|
|
12
12
|
const context = getIntlayerContext();
|
|
13
|
-
|
|
14
|
-
// Create a derived store that loads the dictionary when locale changes
|
|
15
|
-
return derived([intlayerStore], ([$store]) => {
|
|
13
|
+
return derived(intlayerStore, ($store, set) => {
|
|
16
14
|
const targetLocale = locale ?? context?.locale ?? $store.locale;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
catch (error) {
|
|
30
|
-
console.error(`Failed to load dictionary for key: ${String(targetLocale)}`, error);
|
|
31
|
-
dictionaryStore.set(null);
|
|
32
|
-
}
|
|
15
|
+
let isCancelled = false;
|
|
16
|
+
loadDynamicDictionary(dictionaryPromise, targetLocale).then((dictionary) => {
|
|
17
|
+
if (!isCancelled)
|
|
18
|
+
set(dictionary ?? null);
|
|
19
|
+
}, (error) => {
|
|
20
|
+
if (isCancelled)
|
|
21
|
+
return;
|
|
22
|
+
console.error(`Failed to load dictionary for locale: ${String(targetLocale)}`, error);
|
|
23
|
+
set(null);
|
|
24
|
+
});
|
|
25
|
+
return () => {
|
|
26
|
+
isCancelled = true;
|
|
33
27
|
};
|
|
34
|
-
|
|
35
|
-
// Return the current state, actual loading happens asynchronously
|
|
36
|
-
return null;
|
|
37
|
-
});
|
|
28
|
+
}, null);
|
|
38
29
|
};
|
|
@@ -1,33 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createDynamicContentStore, loadingContentPlaceholder, } from '@intlayer/core/dictionaryManipulator';
|
|
2
2
|
import { derived } from 'svelte/store';
|
|
3
3
|
import { getDictionary } from '../getDictionary';
|
|
4
4
|
import { getIntlayerContext } from './intlayerContext';
|
|
5
5
|
import { intlayerStore } from './intlayerStore';
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
get: (_target, prop) => {
|
|
9
|
-
if (prop === Symbol.toPrimitive) {
|
|
10
|
-
return () => undefined;
|
|
11
|
-
}
|
|
12
|
-
if (prop === 'toString') {
|
|
13
|
-
return () => '';
|
|
14
|
-
}
|
|
15
|
-
if (prop === 'then') {
|
|
16
|
-
return undefined;
|
|
17
|
-
}
|
|
18
|
-
return recursiveProxy;
|
|
19
|
-
},
|
|
20
|
-
apply: () => recursiveProxy,
|
|
21
|
-
});
|
|
6
|
+
/** Content cache shared by every dynamic read of the page. */
|
|
7
|
+
const dynamicContentStore = createDynamicContentStore((dictionary, argument) => getDictionary(dictionary, argument));
|
|
22
8
|
const loadingProxy = () => new Proxy({ isLoading: true, error: null }, {
|
|
23
9
|
get: (_target, prop) => {
|
|
24
10
|
if (prop === 'isLoading')
|
|
25
11
|
return true;
|
|
26
12
|
if (prop === 'error')
|
|
27
13
|
return null;
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
return recursiveProxy;
|
|
14
|
+
// Any other access is safe while loading and renders ''
|
|
15
|
+
return loadingContentPlaceholder;
|
|
31
16
|
},
|
|
32
17
|
});
|
|
33
18
|
/** Merges the resolved content with the loading/error state. */
|
|
@@ -54,84 +39,31 @@ const withState = (value, isLoading = false) => {
|
|
|
54
39
|
*/
|
|
55
40
|
export function useDictionaryDynamic(dictionaryPromise, key, localeOrSelector) {
|
|
56
41
|
const context = getIntlayerContext();
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
localeOrSelector !== null;
|
|
60
|
-
const callSelector = isSelector
|
|
61
|
-
? localeOrSelector
|
|
62
|
-
: undefined;
|
|
63
|
-
// Layers the provider's ambient variant under the call-site selector, so only
|
|
64
|
-
// the targeted chunk is loaded. A call-site variant always wins.
|
|
65
|
-
const variant = callSelector?.variant ?? resolveProviderVariant(context?.variant, key);
|
|
66
|
-
const selector = variant !== undefined
|
|
67
|
-
? { ...callSelector, variant }
|
|
68
|
-
: callSelector;
|
|
69
|
-
const explicitLocale = isSelector
|
|
70
|
-
? localeOrSelector.locale
|
|
71
|
-
: localeOrSelector;
|
|
72
|
-
const localeStore = derived(intlayerStore, ($store) => explicitLocale ?? context?.locale ?? $store.locale);
|
|
73
|
-
// Once content has resolved, a locale switch keeps it on screen (flagged as
|
|
74
|
-
// loading) until the new chunk lands. Falling back to `loadingProxy()` would
|
|
75
|
-
// blank every text node for a frame before rendering the target locale.
|
|
42
|
+
const readContent = dynamicContentStore.createReader(dictionaryPromise, String(key));
|
|
43
|
+
const contextLocaleStore = derived(intlayerStore, ($store) => context?.locale ?? $store.locale);
|
|
76
44
|
let resolvedContent;
|
|
77
|
-
return derived(
|
|
45
|
+
return derived(contextLocaleStore, ($contextLocale, set) => {
|
|
78
46
|
const publish = (content) => {
|
|
79
47
|
resolvedContent = content;
|
|
80
48
|
set(withState(content));
|
|
81
49
|
};
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
50
|
+
// Resolved per locale, so a locale pinned at the call site stays pinned
|
|
51
|
+
// while an unpinned one follows the store.
|
|
52
|
+
const settled = readContent({
|
|
53
|
+
localeOrSelector,
|
|
54
|
+
contextLocale: $contextLocale,
|
|
55
|
+
contextVariant: context?.variant,
|
|
56
|
+
}, publish, (error) => {
|
|
57
|
+
console.error(error);
|
|
58
|
+
set({ isLoading: false, error: error });
|
|
59
|
+
});
|
|
60
|
+
if (settled) {
|
|
61
|
+
publish(settled.content);
|
|
90
62
|
return;
|
|
91
63
|
}
|
|
64
|
+
// Keeps the previous locale's content on screen while the next loads
|
|
92
65
|
set(resolvedContent === undefined
|
|
93
66
|
? loadingProxy()
|
|
94
67
|
: withState(resolvedContent, true));
|
|
95
|
-
|
|
96
|
-
const load = async () => {
|
|
97
|
-
try {
|
|
98
|
-
let resolved;
|
|
99
|
-
if (process.env.INTLAYER_DICTIONARY_SELECTOR !== 'false' &&
|
|
100
|
-
isQualifiedDynamicLoaderMap(dictionaryPromise)) {
|
|
101
|
-
resolved = await resolveQualifiedDynamicContentAsync({
|
|
102
|
-
loaderMap: dictionaryPromise,
|
|
103
|
-
key: String(key),
|
|
104
|
-
locale: $locale,
|
|
105
|
-
selector,
|
|
106
|
-
transform: (dictionary) => getDictionary(dictionary, $locale),
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
const loader = dictionaryPromise[$locale];
|
|
111
|
-
if (!loader)
|
|
112
|
-
return;
|
|
113
|
-
const dict = await loader();
|
|
114
|
-
resolved = getDictionary(dict, $locale);
|
|
115
|
-
}
|
|
116
|
-
if (isCancelled)
|
|
117
|
-
return;
|
|
118
|
-
publish(resolved);
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
if (isCancelled)
|
|
122
|
-
return;
|
|
123
|
-
console.error(error);
|
|
124
|
-
set({
|
|
125
|
-
isLoading: false,
|
|
126
|
-
error: error,
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
load();
|
|
131
|
-
return () => {
|
|
132
|
-
isCancelled = true;
|
|
133
|
-
};
|
|
134
|
-
},
|
|
135
|
-
// Initial value
|
|
136
|
-
loadingProxy());
|
|
68
|
+
}, loadingProxy());
|
|
137
69
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { KeyPath } from '@intlayer/types/keyPath';
|
|
2
|
+
import { type Readable } from 'svelte/store';
|
|
3
|
+
/**
|
|
4
|
+
* Store of the source string edited in the visual editor for a markdown / html
|
|
5
|
+
* node. Holds `undefined` when the node is not edited (or the editor is off).
|
|
6
|
+
*/
|
|
7
|
+
export declare const getEditedContentSource: (dictionaryKey: string | undefined, keyPath: KeyPath[] | undefined) => Readable<string | undefined>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isEnabled } from '@intlayer/editor/isEnabled';
|
|
2
|
+
import { readable } from 'svelte/store';
|
|
3
|
+
/**
|
|
4
|
+
* Store of the source string edited in the visual editor for a markdown / html
|
|
5
|
+
* node. Holds `undefined` when the node is not edited (or the editor is off).
|
|
6
|
+
*/
|
|
7
|
+
export const getEditedContentSource = (dictionaryKey, keyPath) => readable(undefined, (set) => {
|
|
8
|
+
if (process.env.INTLAYER_EDITOR_ENABLED === 'false' || !isEnabled)
|
|
9
|
+
return;
|
|
10
|
+
if (!dictionaryKey)
|
|
11
|
+
return;
|
|
12
|
+
let isStopped = false;
|
|
13
|
+
let unsubscribe = null;
|
|
14
|
+
import('@intlayer/editor')
|
|
15
|
+
.then(({ subscribeToEditedContentSource }) => {
|
|
16
|
+
if (isStopped)
|
|
17
|
+
return;
|
|
18
|
+
unsubscribe = subscribeToEditedContentSource(dictionaryKey, keyPath ?? [], set);
|
|
19
|
+
})
|
|
20
|
+
.catch(() => { });
|
|
21
|
+
return () => {
|
|
22
|
+
isStopped = true;
|
|
23
|
+
unsubscribe?.();
|
|
24
|
+
};
|
|
25
|
+
});
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import type { KeyPath } from '@intlayer/types/keyPath';
|
|
3
3
|
import ContentSelector from '../editor/ContentSelector.svelte';
|
|
4
|
+
import { getEditedContentSource } from '../editor/editedContentSource';
|
|
4
5
|
import HTMLRenderer from './HTMLRenderer.svelte';
|
|
5
6
|
|
|
6
7
|
export let dictionaryKey: string;
|
|
7
8
|
export let keyPath: KeyPath[];
|
|
8
9
|
export let value: string;
|
|
9
10
|
export let components: any = {};
|
|
11
|
+
|
|
12
|
+
// Live preview of the source edited in the visual editor
|
|
13
|
+
$: editedSource = getEditedContentSource(dictionaryKey, keyPath);
|
|
10
14
|
</script>
|
|
11
15
|
|
|
12
16
|
<ContentSelector {dictionaryKey} {keyPath}>
|
|
13
|
-
<HTMLRenderer {value} {components} />
|
|
17
|
+
<HTMLRenderer value={$editedSource ?? value} {components} />
|
|
14
18
|
</ContentSelector>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import type { KeyPath } from '@intlayer/types/keyPath';
|
|
3
3
|
import ContentSelector from '../editor/ContentSelector.svelte';
|
|
4
|
+
import { getEditedContentSource } from '../editor/editedContentSource';
|
|
4
5
|
import type { ParsedMarkdown } from './compiler';
|
|
5
6
|
import MarkdownRenderer from './MarkdownRenderer.svelte';
|
|
6
7
|
|
|
@@ -9,10 +10,10 @@ export let keyPath: KeyPath[];
|
|
|
9
10
|
export let value: string | ParsedMarkdown;
|
|
10
11
|
|
|
11
12
|
$: overrides = { ...$$restProps };
|
|
13
|
+
// Live preview of the source edited in the visual editor
|
|
14
|
+
$: editedSource = getEditedContentSource(dictionaryKey, keyPath);
|
|
12
15
|
</script>
|
|
13
16
|
|
|
14
17
|
<ContentSelector {dictionaryKey} {keyPath}>
|
|
15
|
-
<MarkdownRenderer {value} {overrides} />
|
|
18
|
+
<MarkdownRenderer value={$editedSource ?? value} {overrides} />
|
|
16
19
|
</ContentSelector>
|
|
17
|
-
|
|
18
|
-
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-intlayer",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.9.0",
|
|
4
4
|
"description": "Easily internationalize i18n your Svelte applications with type-safe multilingual content management.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data",
|
|
@@ -83,11 +83,11 @@
|
|
|
83
83
|
"typecheck": "tsc --noEmit --project tsconfig.types.json"
|
|
84
84
|
},
|
|
85
85
|
"dependencies": {
|
|
86
|
-
"@intlayer/api": "9.
|
|
87
|
-
"@intlayer/config": "9.
|
|
88
|
-
"@intlayer/core": "9.
|
|
89
|
-
"@intlayer/editor": "9.
|
|
90
|
-
"@intlayer/types": "9.
|
|
86
|
+
"@intlayer/api": "9.9.0",
|
|
87
|
+
"@intlayer/config": "9.9.0",
|
|
88
|
+
"@intlayer/core": "9.9.0",
|
|
89
|
+
"@intlayer/editor": "9.9.0",
|
|
90
|
+
"@intlayer/types": "9.9.0"
|
|
91
91
|
},
|
|
92
92
|
"devDependencies": {
|
|
93
93
|
"@sveltejs/adapter-auto": "9.0.0",
|
|
@@ -109,7 +109,7 @@
|
|
|
109
109
|
"svelte": ">=5.0.0"
|
|
110
110
|
},
|
|
111
111
|
"optionalDependencies": {
|
|
112
|
-
"@intlayer/analytics": "9.
|
|
112
|
+
"@intlayer/analytics": "9.9.0"
|
|
113
113
|
},
|
|
114
114
|
"engines": {
|
|
115
115
|
"node": ">=14.18"
|