@oddmash/juliya 0.4.1 → 0.4.3
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/core/scoring.js +2 -0
- package/dist/dom/inline.d.ts +3 -2
- package/dist/dom/inline.js +101 -27
- package/dist/dom/observe-element.d.ts +1 -1
- package/dist/dom/observe-element.js +41 -20
- package/dist/dom/observe.js +1 -1
- package/package.json +3 -1
package/dist/core/scoring.js
CHANGED
|
@@ -7,6 +7,8 @@ const numeralModifier = /^(?:한|두|세|네|다섯|여섯|일곱|여덟|아홉|
|
|
|
7
7
|
const counterHead = /^(?:개|명|건|번|권|회|종|가지|대|장|쪽|줄|단계|시간|분|초|일|개월|년|주)(?:의|이|가|을|를|은|는|에|로|으로|와|과|도|만|부터|까지|에서|에게|이나|나|씩|마다|으로는|로는)?[.,!?。!?…]?$/u;
|
|
8
8
|
const compounds = {
|
|
9
9
|
공통: ["패키지", "코드", "컴포넌트", "모듈"],
|
|
10
|
+
패키지: ["매니저"],
|
|
11
|
+
계약: ["런타임"],
|
|
10
12
|
디자인: ["시스템", "토큰"],
|
|
11
13
|
배포: ["구조", "단위", "경계", "결과"],
|
|
12
14
|
사용: ["범위", "경험", "환경"],
|
package/dist/dom/inline.d.ts
CHANGED
|
@@ -11,8 +11,9 @@ export declare const readInline: (element: HTMLElement) => {
|
|
|
11
11
|
runs: Run[];
|
|
12
12
|
};
|
|
13
13
|
export declare const breakOffsets: (lines: WrappedLine[]) => number[];
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
type ReadAuthorMarkup = <T>(read: () => T) => T;
|
|
15
|
+
export declare const observeInline: (element: HTMLElement, getOptions: () => InlineKowrapOptions, onBreaks: (offsets: number[]) => void, readAuthorMarkup?: ReadAuthorMarkup) => {
|
|
16
|
+
refresh: (force?: boolean) => void;
|
|
16
17
|
destroy: () => void;
|
|
17
18
|
};
|
|
18
19
|
export declare const mountInline: (element: HTMLElement, initial: InlineKowrapOptions) => {
|
package/dist/dom/inline.js
CHANGED
|
@@ -26,48 +26,89 @@ export const readInline = (element) => {
|
|
|
26
26
|
return { text, runs };
|
|
27
27
|
};
|
|
28
28
|
export const breakOffsets = (lines) => lines.slice(0, -1).filter((line) => !line.breakAfter).map((line) => line.end);
|
|
29
|
-
|
|
29
|
+
const readStyledRuns = (element) => {
|
|
30
30
|
const { text, runs } = readInline(element);
|
|
31
|
-
const options = getOptions();
|
|
32
|
-
if (!layout) {
|
|
33
|
-
onBreaks(breakOffsets(manualLines(text, options.forcedBreaks)));
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
31
|
const view = element.ownerDocument.defaultView;
|
|
37
|
-
const
|
|
32
|
+
const styles = new Map();
|
|
38
33
|
const styled = [];
|
|
39
34
|
for (const run of runs) {
|
|
40
35
|
const parent = run.node.parentElement;
|
|
41
36
|
const font = fontOf(view.getComputedStyle(parent));
|
|
42
37
|
const language = languageOf(parent);
|
|
43
38
|
const key = JSON.stringify([font, language]);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
const previous = styled.at(-1);
|
|
50
|
-
if (previous?.key === key && previous.end === run.start)
|
|
51
|
-
previous.end = run.end;
|
|
39
|
+
styles.set(key, { font, language });
|
|
40
|
+
const last = styled.at(-1);
|
|
41
|
+
if (last?.key === key && last.end === run.start)
|
|
42
|
+
last.end = run.end;
|
|
52
43
|
else
|
|
53
|
-
styled.push({ start: run.start, end: run.end, key
|
|
44
|
+
styled.push({ start: run.start, end: run.end, key });
|
|
54
45
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
46
|
+
return { text, styles, styled };
|
|
47
|
+
};
|
|
48
|
+
export const observeInline = (element, getOptions, onBreaks, readAuthorMarkup) => {
|
|
49
|
+
let previous;
|
|
50
|
+
let forceAuthorRead = false;
|
|
51
|
+
const observer = observeElementLayout(element, (layout, fromWindowResize) => {
|
|
52
|
+
const options = getOptions();
|
|
53
|
+
if (!layout) {
|
|
54
|
+
previous = undefined;
|
|
55
|
+
onBreaks(breakOffsets(manualLines(readInline(element).text, options.forcedBreaks)));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const { locale, strategy, breakLongWords, forcedBreaks } = options;
|
|
59
|
+
const signatureOf = ({ text, styled }) => JSON.stringify([text, layout.width, locale, strategy, breakLongWords, forcedBreaks, styled]);
|
|
60
|
+
const rendered = readStyledRuns(element);
|
|
61
|
+
const signature = signatureOf(rendered);
|
|
62
|
+
// Generated breaks can mask structural CSS changes during a viewport resize.
|
|
63
|
+
if (!forceAuthorRead && !fromWindowResize && previous?.signature === signature && previous.measure === layout.measure) {
|
|
64
|
+
onBreaks(previous.offsets);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const author = readAuthorMarkup ? readAuthorMarkup(() => readStyledRuns(element)) : rendered;
|
|
68
|
+
const authorSignature = signatureOf(author);
|
|
69
|
+
const { text, styles, styled } = author;
|
|
70
|
+
if (previous?.authorSignature === authorSignature && previous.measure === layout.measure) {
|
|
71
|
+
onBreaks(previous.offsets);
|
|
72
|
+
previous.signature = readAuthorMarkup ? signatureOf(readStyledRuns(element)) : signature;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const measures = new Map();
|
|
76
|
+
for (const [key, { font, language }] of styles) {
|
|
77
|
+
measures.set(key, createLocalizedCanvasMeasure(font, element.ownerDocument, language));
|
|
78
|
+
}
|
|
79
|
+
const measureRange = (start, end) => styled.reduce((width, run) => {
|
|
80
|
+
const left = Math.max(start, run.start);
|
|
81
|
+
const right = Math.min(end, run.end);
|
|
82
|
+
return left < right ? width + measures.get(run.key)(text.slice(left, right)) : width;
|
|
83
|
+
}, 0);
|
|
84
|
+
const offsets = breakOffsets(wrap(text, { ...options, ...layout, measureRange }));
|
|
85
|
+
onBreaks(offsets);
|
|
86
|
+
previous = { signature: readAuthorMarkup ? signatureOf(readStyledRuns(element)) : signature, authorSignature, measure: layout.measure, offsets };
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
...observer,
|
|
90
|
+
refresh: (force = true) => {
|
|
91
|
+
forceAuthorRead = force;
|
|
92
|
+
try {
|
|
93
|
+
observer.refresh();
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
forceAuthorRead = false;
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
};
|
|
62
101
|
export const mountInline = (element, initial) => {
|
|
63
102
|
let options = initial;
|
|
64
103
|
let undo = [];
|
|
65
104
|
let previous = [];
|
|
105
|
+
const generated = new Set();
|
|
66
106
|
let mutations;
|
|
67
107
|
const restore = () => {
|
|
68
108
|
undo.reverse().forEach((apply) => apply());
|
|
69
109
|
undo = [];
|
|
70
110
|
previous = [];
|
|
111
|
+
generated.clear();
|
|
71
112
|
mutations?.takeRecords();
|
|
72
113
|
};
|
|
73
114
|
const render = (offsets) => {
|
|
@@ -82,6 +123,7 @@ export const mountInline = (element, initial) => {
|
|
|
82
123
|
const { node, start } = run;
|
|
83
124
|
const br = element.ownerDocument.createElement("br");
|
|
84
125
|
br.setAttribute(inlineBreakAttribute, "");
|
|
126
|
+
generated.add(br);
|
|
85
127
|
const position = offset - start;
|
|
86
128
|
if (position === node.length) {
|
|
87
129
|
node.after(br);
|
|
@@ -102,15 +144,47 @@ export const mountInline = (element, initial) => {
|
|
|
102
144
|
previous = offsets;
|
|
103
145
|
mutations?.takeRecords();
|
|
104
146
|
};
|
|
105
|
-
const
|
|
147
|
+
const readAuthorMarkup = (read) => {
|
|
148
|
+
if (!generated.size)
|
|
149
|
+
return read();
|
|
150
|
+
const positions = Array.from(element.querySelectorAll("br")).filter((br) => generated.has(br)).map((br) => ({ br, parent: br.parentNode, next: br.nextSibling }));
|
|
151
|
+
const selection = element.ownerDocument.getSelection();
|
|
152
|
+
const anchor = selection?.anchorNode;
|
|
153
|
+
const focus = selection?.focusNode;
|
|
154
|
+
const anchorOffset = selection?.anchorOffset ?? 0;
|
|
155
|
+
const focusOffset = selection?.focusOffset ?? 0;
|
|
156
|
+
try {
|
|
157
|
+
positions.forEach(({ br }) => br.remove());
|
|
158
|
+
return read();
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
positions.reverse().forEach(({ br, parent, next }) => parent.insertBefore(br, next));
|
|
162
|
+
if (selection && anchor && focus && (element.contains(anchor) || element.contains(focus)) &&
|
|
163
|
+
(selection.anchorNode !== anchor || selection.focusNode !== focus || selection.anchorOffset !== anchorOffset || selection.focusOffset !== focusOffset)) {
|
|
164
|
+
selection.setBaseAndExtent(anchor, anchorOffset, focus, focusOffset);
|
|
165
|
+
}
|
|
166
|
+
mutations?.takeRecords();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
const observer = observeInline(element, () => options, render, readAuthorMarkup);
|
|
106
170
|
const Mutation = element.ownerDocument.defaultView?.MutationObserver;
|
|
107
171
|
if (Mutation) {
|
|
108
|
-
mutations = new Mutation(() => {
|
|
172
|
+
mutations = new Mutation((records) => {
|
|
173
|
+
if (records.some((record) => record.type !== "attributes"))
|
|
174
|
+
restore();
|
|
175
|
+
observer.refresh();
|
|
176
|
+
});
|
|
109
177
|
mutations.observe(element, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ["class", "style", "lang"] });
|
|
110
178
|
}
|
|
179
|
+
const refresh = () => {
|
|
180
|
+
const records = mutations?.takeRecords() ?? [];
|
|
181
|
+
if (records.some((record) => record.type !== "attributes"))
|
|
182
|
+
restore();
|
|
183
|
+
observer.refresh();
|
|
184
|
+
};
|
|
111
185
|
return {
|
|
112
|
-
update: (next) => {
|
|
113
|
-
refresh
|
|
186
|
+
update: (next) => { options = next; refresh(); },
|
|
187
|
+
refresh,
|
|
114
188
|
destroy: () => { observer.destroy(); mutations?.disconnect(); restore(); },
|
|
115
189
|
restore,
|
|
116
190
|
};
|
|
@@ -5,7 +5,7 @@ export interface ElementLayout {
|
|
|
5
5
|
measure: Measure;
|
|
6
6
|
}
|
|
7
7
|
export declare const fontOf: (computed: CSSStyleDeclaration) => CanvasMeasureOptions;
|
|
8
|
-
export declare const observeElementLayout: (element: HTMLElement, onLayout: (layout: ElementLayout | undefined) => void) => {
|
|
8
|
+
export declare const observeElementLayout: (element: HTMLElement, onLayout: (layout: ElementLayout | undefined, fromWindowResize: boolean) => void, skipUnchangedResize?: boolean) => {
|
|
9
9
|
refresh: () => void;
|
|
10
10
|
destroy: () => void;
|
|
11
11
|
};
|
|
@@ -18,19 +18,21 @@ export const fontOf = (computed) => ({
|
|
|
18
18
|
wordSpacing: spacing(computed.wordSpacing),
|
|
19
19
|
fontKerning: computed.fontKerning,
|
|
20
20
|
});
|
|
21
|
-
export const observeElementLayout = (element, onLayout) => {
|
|
21
|
+
export const observeElementLayout = (element, onLayout, skipUnchangedResize = false) => {
|
|
22
22
|
const doc = element.ownerDocument;
|
|
23
23
|
const view = doc.defaultView;
|
|
24
24
|
if (!view) {
|
|
25
|
-
onLayout(undefined);
|
|
26
|
-
return { refresh: () => onLayout(undefined), destroy: () => { } };
|
|
25
|
+
onLayout(undefined, false);
|
|
26
|
+
return { refresh: () => onLayout(undefined, false), destroy: () => { } };
|
|
27
27
|
}
|
|
28
28
|
let active = true;
|
|
29
29
|
let previousFont;
|
|
30
30
|
let measure;
|
|
31
31
|
let observedWidth;
|
|
32
32
|
let frame;
|
|
33
|
-
|
|
33
|
+
let published = false;
|
|
34
|
+
let lastLayout;
|
|
35
|
+
const update = (contentWidth, fromObserver = false, fromWindowResize = false) => {
|
|
34
36
|
if (!active)
|
|
35
37
|
return;
|
|
36
38
|
let layout;
|
|
@@ -63,30 +65,49 @@ export const observeElementLayout = (element, onLayout) => {
|
|
|
63
65
|
catch {
|
|
64
66
|
layout = undefined;
|
|
65
67
|
}
|
|
66
|
-
|
|
68
|
+
if (skipUnchangedResize && fromObserver && published && layout?.width === lastLayout?.width && layout?.measure === lastLayout?.measure)
|
|
69
|
+
return;
|
|
70
|
+
published = true;
|
|
71
|
+
lastLayout = layout;
|
|
72
|
+
onLayout(layout, fromWindowResize);
|
|
73
|
+
};
|
|
74
|
+
let pendingWidth;
|
|
75
|
+
let pendingRefresh = false;
|
|
76
|
+
let pendingWindowResize = false;
|
|
77
|
+
const schedule = (width, cause = "observer") => {
|
|
78
|
+
if (!active)
|
|
79
|
+
return;
|
|
80
|
+
if (width !== undefined)
|
|
81
|
+
pendingWidth = width;
|
|
82
|
+
pendingRefresh ||= cause !== "observer";
|
|
83
|
+
pendingWindowResize ||= cause === "window";
|
|
84
|
+
const flush = () => {
|
|
85
|
+
frame = undefined;
|
|
86
|
+
const nextWidth = pendingWidth;
|
|
87
|
+
const fromObserver = !pendingRefresh;
|
|
88
|
+
const fromWindowResize = pendingWindowResize;
|
|
89
|
+
pendingWidth = undefined;
|
|
90
|
+
pendingRefresh = false;
|
|
91
|
+
pendingWindowResize = false;
|
|
92
|
+
update(nextWidth, fromObserver, fromWindowResize);
|
|
93
|
+
};
|
|
94
|
+
if (!view.requestAnimationFrame)
|
|
95
|
+
flush();
|
|
96
|
+
else if (frame === undefined)
|
|
97
|
+
frame = view.requestAnimationFrame(flush);
|
|
67
98
|
};
|
|
68
99
|
const refresh = () => update();
|
|
100
|
+
const onResize = () => schedule(undefined, "window");
|
|
69
101
|
const onFonts = () => {
|
|
70
102
|
measure = undefined;
|
|
71
|
-
|
|
103
|
+
schedule(undefined, "fonts");
|
|
72
104
|
};
|
|
73
105
|
const Observer = view.ResizeObserver ?? globalThis.ResizeObserver;
|
|
74
106
|
const observer = Observer ? new Observer((entries) => {
|
|
75
|
-
|
|
76
|
-
if (width === undefined || !view.requestAnimationFrame) {
|
|
77
|
-
update(width);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (frame !== undefined)
|
|
81
|
-
view.cancelAnimationFrame(frame);
|
|
82
|
-
// Rendering can change the observed element's height.
|
|
83
|
-
frame = view.requestAnimationFrame(() => {
|
|
84
|
-
frame = undefined;
|
|
85
|
-
update(width);
|
|
86
|
-
});
|
|
107
|
+
schedule(entries?.[0]?.contentRect.width);
|
|
87
108
|
}) : undefined;
|
|
88
109
|
observer?.observe(element);
|
|
89
|
-
view.addEventListener("resize",
|
|
110
|
+
view.addEventListener("resize", onResize);
|
|
90
111
|
doc.fonts?.addEventListener("loadingdone", onFonts);
|
|
91
112
|
void doc.fonts?.ready.then(onFonts);
|
|
92
113
|
update();
|
|
@@ -95,7 +116,7 @@ export const observeElementLayout = (element, onLayout) => {
|
|
|
95
116
|
if (frame !== undefined)
|
|
96
117
|
view.cancelAnimationFrame(frame);
|
|
97
118
|
observer?.disconnect();
|
|
98
|
-
view.removeEventListener("resize",
|
|
119
|
+
view.removeEventListener("resize", onResize);
|
|
99
120
|
doc.fonts?.removeEventListener("loadingdone", onFonts);
|
|
100
121
|
};
|
|
101
122
|
return { refresh, destroy };
|
package/dist/dom/observe.js
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oddmash/juliya",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.3",
|
|
5
5
|
"description": "Locale-aware text wrapping for Korean, English, Japanese and Simplified Chinese",
|
|
6
|
+
"homepage": "https://oddmash.github.io/juliya/",
|
|
7
|
+
"keywords": ["text-wrapping", "line-breaking", "typography", "internationalization", "korean", "japanese", "chinese", "react", "vue", "svelte"],
|
|
6
8
|
"type": "module",
|
|
7
9
|
"packageManager": "pnpm@11.25.0",
|
|
8
10
|
"exports": {
|