@oddmash/juliya 0.5.0 → 0.5.1
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 +5 -9
- package/dist/dom/render.js +11 -3
- package/dist/dom/scope.js +57 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ The provider shares its locale with child components and hooks. An explicit loca
|
|
|
41
41
|
|
|
42
42
|
## Try in a browser
|
|
43
43
|
|
|
44
|
-
Save this Korean example as `index.html` and open it in a browser with an internet connection.
|
|
44
|
+
Save this Korean example as `index.html` and open it in a browser with an internet connection.
|
|
45
45
|
|
|
46
46
|
```html
|
|
47
47
|
<!doctype html>
|
|
@@ -50,10 +50,10 @@ Save this Korean example as `index.html` and open it in a browser with an intern
|
|
|
50
50
|
<title>Juliya 예제</title>
|
|
51
51
|
<p id="title" style="max-width: 220px; font-size: 24px">팀의 모든 문서를 한곳에서 관리하세요</p>
|
|
52
52
|
<script type="module">
|
|
53
|
-
import {
|
|
53
|
+
import { mountJuliya } from 'https://cdn.jsdelivr.net/npm/@oddmash/juliya@0.5.0/dist/dom/index.js/+esm';
|
|
54
54
|
|
|
55
55
|
const title = document.getElementById('title');
|
|
56
|
-
|
|
56
|
+
mountJuliya(title, { text: title.textContent });
|
|
57
57
|
</script>
|
|
58
58
|
</html>
|
|
59
59
|
```
|
|
@@ -66,10 +66,6 @@ Save this Korean example as `index.html` and open it in a browser with an intern
|
|
|
66
66
|
- React, Next.js, Vue, Svelte, and plain HTML support.
|
|
67
67
|
- Heuristic language rules, not comprehensive natural-language analysis. Best suited to headings and short passages.
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
[Usage guide](https://oddmash.github.io/juliya/en/docs/) | [Contributing](CONTRIBUTING.md) | [MIT License](LICENSE)
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
[Migration and usage](https://oddmash.github.io/juliya/en/docs/) | [Contributing](CONTRIBUTING.md) | [MIT License](LICENSE)
|
|
74
|
-
|
|
75
|
-
The canonical DOM and framework names are `mountJuliya`, `observeJuliya`, `juliyaDirective`, and `juliya`. The legacy names remain equivalent aliases.
|
|
71
|
+
Use `mountJuliya`, `observeJuliya`, `juliyaDirective`, and `juliya` for DOM, Vue, and Svelte integrations.
|
package/dist/dom/render.js
CHANGED
|
@@ -8,18 +8,26 @@ const wrapStyles = {
|
|
|
8
8
|
overflowWrap: "anywhere",
|
|
9
9
|
};
|
|
10
10
|
const captureStyles = (element) => {
|
|
11
|
-
const
|
|
11
|
+
const originalStyle = element.getAttribute("style");
|
|
12
|
+
const originalCssText = element.style.cssText;
|
|
12
13
|
const names = ["white-space", "word-break", "overflow-wrap"];
|
|
13
14
|
const entries = names.map(name => ({ name, value: element.style.getPropertyValue(name), priority: element.style.getPropertyPriority(name) }));
|
|
14
15
|
return () => {
|
|
16
|
+
// The caller may already have replaced all wrapping styles with its own attribute.
|
|
17
|
+
if (element.style.cssText === originalCssText)
|
|
18
|
+
return;
|
|
15
19
|
for (const { name, value, priority } of entries) {
|
|
16
20
|
if (value)
|
|
17
21
|
element.style.setProperty(name, value, priority);
|
|
18
22
|
else
|
|
19
23
|
element.style.removeProperty(name);
|
|
20
24
|
}
|
|
21
|
-
if (
|
|
22
|
-
|
|
25
|
+
if (element.style.cssText === originalCssText) {
|
|
26
|
+
// Flush pending CSSOM serialization before removing an originally absent attribute.
|
|
27
|
+
element.setAttribute("style", originalStyle ?? "");
|
|
28
|
+
if (originalStyle === null)
|
|
29
|
+
element.removeAttribute("style");
|
|
30
|
+
}
|
|
23
31
|
};
|
|
24
32
|
};
|
|
25
33
|
const renderLines = (element, lines) => {
|
package/dist/dom/scope.js
CHANGED
|
@@ -46,6 +46,8 @@ export const mountJuliyaScope = (root, initial = {}, diagnostics) => {
|
|
|
46
46
|
let active = true;
|
|
47
47
|
let refreshing = false;
|
|
48
48
|
let pending = false;
|
|
49
|
+
let pendingFrame;
|
|
50
|
+
let generation = 0;
|
|
49
51
|
let observer;
|
|
50
52
|
const parent = closestScope(root.parentElement);
|
|
51
53
|
const view = root.ownerDocument.defaultView;
|
|
@@ -125,14 +127,58 @@ export const mountJuliyaScope = (root, initial = {}, diagnostics) => {
|
|
|
125
127
|
refreshing = false;
|
|
126
128
|
}
|
|
127
129
|
};
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
+
const cancelPending = () => {
|
|
131
|
+
generation++;
|
|
132
|
+
if (pendingFrame !== undefined)
|
|
133
|
+
view?.cancelAnimationFrame(pendingFrame);
|
|
134
|
+
pendingFrame = undefined;
|
|
135
|
+
pending = false;
|
|
136
|
+
};
|
|
137
|
+
const schedule = (immediate = false) => {
|
|
138
|
+
if (!active)
|
|
130
139
|
return;
|
|
140
|
+
const useFrame = !immediate && !!view?.requestAnimationFrame && root.ownerDocument.visibilityState !== "hidden";
|
|
141
|
+
if (pending) {
|
|
142
|
+
if (pendingFrame === undefined || useFrame)
|
|
143
|
+
return;
|
|
144
|
+
cancelPending();
|
|
145
|
+
}
|
|
131
146
|
pending = true;
|
|
132
|
-
|
|
147
|
+
const scheduled = ++generation;
|
|
148
|
+
const flush = () => {
|
|
149
|
+
if (scheduled !== generation)
|
|
150
|
+
return;
|
|
151
|
+
pendingFrame = undefined;
|
|
152
|
+
pending = false;
|
|
153
|
+
refresh();
|
|
154
|
+
};
|
|
155
|
+
// Coalesce mutations from independently scheduled paragraph layouts into one scan.
|
|
156
|
+
if (useFrame && view)
|
|
157
|
+
pendingFrame = view.requestAnimationFrame(flush);
|
|
158
|
+
else
|
|
159
|
+
queueMicrotask(flush);
|
|
160
|
+
};
|
|
161
|
+
const belongsToMounted = (target) => {
|
|
162
|
+
for (let element = target.nodeType === 1 ? target : target.parentElement; element && root.contains(element); element = element.parentElement) {
|
|
163
|
+
if (mounted.has(element))
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
};
|
|
168
|
+
const isLayoutMutation = (record) => {
|
|
169
|
+
if (record.type === "characterData" && !root.contains(record.target))
|
|
170
|
+
return true;
|
|
171
|
+
if (!belongsToMounted(record.target) || record.type === "attributes")
|
|
172
|
+
return false;
|
|
173
|
+
return record.type === "characterData" || [...record.addedNodes, ...record.removedNodes].every(node => node.nodeType === 3 || (node.nodeType === 1 && node.matches("br[data-kowrap-break]")));
|
|
174
|
+
};
|
|
175
|
+
const onResize = () => schedule();
|
|
176
|
+
const onVisibilityChange = () => {
|
|
177
|
+
if (root.ownerDocument.visibilityState === "hidden" && pendingFrame !== undefined)
|
|
178
|
+
schedule(true);
|
|
133
179
|
};
|
|
134
180
|
const controller = {
|
|
135
|
-
refresh: () => { refresh(); for (const item of mounted.values())
|
|
181
|
+
refresh: () => { cancelPending(); refresh(); for (const item of mounted.values())
|
|
136
182
|
item.controller.refresh(); },
|
|
137
183
|
update: next => {
|
|
138
184
|
if (!active)
|
|
@@ -140,14 +186,17 @@ export const mountJuliyaScope = (root, initial = {}, diagnostics) => {
|
|
|
140
186
|
const validated = parseConfig(next);
|
|
141
187
|
validateSelectors(root, validated);
|
|
142
188
|
config = validated;
|
|
189
|
+
cancelPending();
|
|
143
190
|
refresh();
|
|
144
191
|
},
|
|
145
192
|
destroy: () => {
|
|
146
193
|
if (!active)
|
|
147
194
|
return;
|
|
148
195
|
active = false;
|
|
196
|
+
cancelPending();
|
|
149
197
|
observer?.disconnect();
|
|
150
|
-
view?.removeEventListener("resize",
|
|
198
|
+
view?.removeEventListener("resize", onResize);
|
|
199
|
+
root.ownerDocument.removeEventListener("visibilitychange", onVisibilityChange);
|
|
151
200
|
for (const element of mounted.keys())
|
|
152
201
|
detach(element, "destroyed");
|
|
153
202
|
scopes.delete(root);
|
|
@@ -158,10 +207,11 @@ export const mountJuliyaScope = (root, initial = {}, diagnostics) => {
|
|
|
158
207
|
try {
|
|
159
208
|
parent?.refresh();
|
|
160
209
|
refresh();
|
|
161
|
-
view?.addEventListener("resize",
|
|
210
|
+
view?.addEventListener("resize", onResize);
|
|
211
|
+
root.ownerDocument.addEventListener("visibilitychange", onVisibilityChange);
|
|
162
212
|
const Mutation = view?.MutationObserver;
|
|
163
213
|
if (Mutation) {
|
|
164
|
-
observer = new Mutation(schedule);
|
|
214
|
+
observer = new Mutation(records => schedule(records.some(record => !isLayoutMutation(record))));
|
|
165
215
|
observer.observe(root, { subtree: true, attributes: true, childList: true, characterData: true });
|
|
166
216
|
}
|
|
167
217
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oddmash/juliya",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.1",
|
|
5
5
|
"description": "Locale-aware text wrapping for Korean, English, Japanese and Simplified Chinese",
|
|
6
6
|
"homepage": "https://oddmash.github.io/juliya/",
|
|
7
7
|
"keywords": ["text-wrapping", "line-breaking", "typography", "internationalization", "korean", "japanese", "chinese", "react", "vue", "svelte"],
|