lexxy_math_symbols 0.1.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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +171 -0
- data/app/assets/stylesheets/lexxy_math_symbols/palette.css +472 -0
- data/app/javascript/lexxy_math_symbols/catalog.js +1656 -0
- data/app/javascript/lexxy_math_symbols/i18n.js +108 -0
- data/app/javascript/lexxy_math_symbols/icons.js +11 -0
- data/app/javascript/lexxy_math_symbols/index.js +189 -0
- data/app/javascript/lexxy_math_symbols/locales/en.js +1657 -0
- data/app/javascript/lexxy_math_symbols/locales/pl.js +38 -0
- data/app/javascript/lexxy_math_symbols/palette.js +1069 -0
- data/app/javascript/lexxy_math_symbols/sets.js +148 -0
- data/config/importmap.rb +5 -0
- data/lib/lexxy_math_symbols/engine.rb +17 -0
- data/lib/lexxy_math_symbols/version.rb +3 -0
- data/lib/lexxy_math_symbols.rb +20 -0
- metadata +73 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// The palette's languages. A locale is
|
|
2
|
+
//
|
|
3
|
+
// { strings, categories, labels, keywords }
|
|
4
|
+
//
|
|
5
|
+
// `strings` the interface's texts by key (see locales/pl.js for every key),
|
|
6
|
+
// `categories` the tabs' names by category key ("recent" too), `labels` and
|
|
7
|
+
// `keywords` what an entry is called and the words the search finds it by,
|
|
8
|
+
// by its insert (`\frac{}{}`). A string may hold {name} for a value given to
|
|
9
|
+
// `t`; one that depends on a number is an object of Intl.PluralRules forms,
|
|
10
|
+
// { one: "{count} result", other: "{count} results" }.
|
|
11
|
+
//
|
|
12
|
+
// The catalog's own labels and keywords (catalog.js) are Polish: they are
|
|
13
|
+
// `pl`'s wherever `pl` gives none of its own.
|
|
14
|
+
import { CATEGORIES } from "lexxy_math_symbols/catalog";
|
|
15
|
+
import pl from "lexxy_math_symbols/locales/pl";
|
|
16
|
+
import en from "lexxy_math_symbols/locales/en";
|
|
17
|
+
|
|
18
|
+
// Where no element says its language: Polish, as before there were others.
|
|
19
|
+
export const DEFAULT_LOCALE = "pl";
|
|
20
|
+
// A language with no locale of its own.
|
|
21
|
+
export const FALLBACK_LOCALE = "en";
|
|
22
|
+
// Whose labels and keywords the catalog's entries carry.
|
|
23
|
+
export const CATALOG_LOCALE = "pl";
|
|
24
|
+
|
|
25
|
+
const LOCALES = new Map();
|
|
26
|
+
|
|
27
|
+
// Adds a language, or more words to one already there (a key given again
|
|
28
|
+
// replaces the old text). `code` is a BCP 47 tag ("de", "pt-BR").
|
|
29
|
+
export function registerLocale(code, { strings, categories, labels, keywords } = {}) {
|
|
30
|
+
const key = code.toLowerCase();
|
|
31
|
+
const old = LOCALES.get(key) || {};
|
|
32
|
+
// A new object each time: the search keeps its words per locale object,
|
|
33
|
+
// so new labels or keywords are read again.
|
|
34
|
+
LOCALES.set(key, {
|
|
35
|
+
code: key,
|
|
36
|
+
strings: { ...old.strings, ...strings },
|
|
37
|
+
categories: { ...old.categories, ...categories },
|
|
38
|
+
labels: { ...old.labels, ...labels },
|
|
39
|
+
keywords: { ...old.keywords, ...keywords },
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
registerLocale("pl", {
|
|
44
|
+
...pl,
|
|
45
|
+
categories: {
|
|
46
|
+
...Object.fromEntries(CATEGORIES.map((category) => [category.key, category.label])),
|
|
47
|
+
...pl.categories,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
registerLocale("en", en);
|
|
51
|
+
|
|
52
|
+
// The locale for a language tag: the tag itself, else its language
|
|
53
|
+
// ("en-GB" → "en"), else English; no tag at all, Polish.
|
|
54
|
+
export function resolveLocale(lang) {
|
|
55
|
+
if (!lang) return DEFAULT_LOCALE;
|
|
56
|
+
const code = lang.toLowerCase();
|
|
57
|
+
if (LOCALES.has(code)) return code;
|
|
58
|
+
const base = code.split("-")[0];
|
|
59
|
+
return LOCALES.has(base) ? base : FALLBACK_LOCALE;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// An element's locale, from the nearest `lang` up from it (the editor's or
|
|
63
|
+
// the page's). An element not yet in the page takes the page's.
|
|
64
|
+
export function localeOf(element) {
|
|
65
|
+
let tagged = element?.closest?.("[lang]");
|
|
66
|
+
if (!tagged && !element?.isConnected) tagged = document.documentElement.closest("[lang]");
|
|
67
|
+
return resolveLocale(tagged?.lang);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function localeData(locale) {
|
|
71
|
+
return LOCALES.get(locale) || LOCALES.get(resolveLocale(locale));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The locales a missing text is looked for in, in turn.
|
|
75
|
+
function chain(locale) {
|
|
76
|
+
return [...new Set([locale, locale.split("-")[0], FALLBACK_LOCALE, DEFAULT_LOCALE])]
|
|
77
|
+
.map((code) => LOCALES.get(code))
|
|
78
|
+
.filter(Boolean);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The text for `key` in `locale`, with {name} replaced from `vars`.
|
|
82
|
+
export function t(key, vars = {}, locale = DEFAULT_LOCALE) {
|
|
83
|
+
const found = chain(locale).find((each) => each.strings[key] !== undefined);
|
|
84
|
+
let text = found ? found.strings[key] : key;
|
|
85
|
+
if (typeof text === "object") {
|
|
86
|
+
const form = new Intl.PluralRules(found.code).select(vars.count ?? 0);
|
|
87
|
+
text = text[form] ?? text.other;
|
|
88
|
+
}
|
|
89
|
+
return text.replace(/\{(\w+)\}/g, (match, name) => (name in vars ? String(vars[name]) : match));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function categoryLabel(key, locale = DEFAULT_LOCALE) {
|
|
93
|
+
const found = chain(locale).find((each) => each.categories[key] !== undefined);
|
|
94
|
+
return found ? found.categories[key] : key;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// What an entry is called in `locale`, "" if that locale does not say: never
|
|
98
|
+
// another language's name.
|
|
99
|
+
export function itemLabel(item, locale = DEFAULT_LOCALE) {
|
|
100
|
+
const own = localeData(locale)?.labels[item.insert];
|
|
101
|
+
return own ?? (locale === CATALOG_LOCALE ? item.label || "" : "");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// The words an entry is found by in `locale` (besides its commands).
|
|
105
|
+
export function itemKeywords(item, locale = DEFAULT_LOCALE) {
|
|
106
|
+
const own = localeData(locale)?.keywords[item.insert];
|
|
107
|
+
return own ?? (locale === CATALOG_LOCALE ? item.keywords || "" : "");
|
|
108
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The toolbar's buttons: inline math (𝑥 in parentheses) and block math (the
|
|
2
|
+
// 𝑥 in a frame), drawn as Lexxy draws its own icons: filled shapes in
|
|
3
|
+
// currentColor, 1.5-unit lines.
|
|
4
|
+
const PARENTHESES = `M15.212 2.079a.75.75 0 0 1 1.006.336A16.932 16.932 0 0 1 18 10c0 2.724-.641 5.3-1.782 7.585a.75.75 0 1 1-1.342-.67A15.432 15.432 0 0 0 16.5 10c0-2.486-.585-4.834-1.624-6.915a.75.75 0 0 1 .336-1.006Zm-10.424 0a.75.75 0 0 1 .336 1.006A15.433 15.433 0 0 0 3.5 10c0 2.486.585 4.834 1.624 6.915a.75.75 0 1 1-1.342.67A16.933 16.933 0 0 1 2 10c0-2.724.641-5.3 1.782-7.585a.75.75 0 0 1 1.006-.336Z`;
|
|
5
|
+
const X = `M7.073 5.633a1.5 1.5 0 0 1 2.219.677l.856 2.08 1.146-1.77a2.25 2.25 0 0 1 3.137-.65l.235.156a.75.75 0 1 1-.832 1.248l-.235-.156a.75.75 0 0 0-1.045.216l-1.71 2.644 1.251 3.04.739-.492a.75.75 0 1 1 .832 1.248l-.739.493a1.5 1.5 0 0 1-2.219-.677l-.856-2.08-1.146 1.77a2.25 2.25 0 0 1-3.137.65l-.235-.156a.75.75 0 0 1 .832-1.248l.235.157a.75.75 0 0 0 1.045-.217l1.71-2.644-1.251-3.04-.739.492a.75.75 0 0 1-.832-1.248l.739-.493Z`;
|
|
6
|
+
|
|
7
|
+
export const MATH_INLINE_ICON = `<svg viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" clip-rule="evenodd" d="${PARENTHESES}${X.replace("M7.073 5.633", "m2.285 3.554")}"/></svg>`;
|
|
8
|
+
|
|
9
|
+
// The frame takes the parentheses' place; the 𝑥 is drawn at full size, so
|
|
10
|
+
// its lines stay 1.5 like the frame's, and it is about 0.7 of the frame.
|
|
11
|
+
export const MATH_BLOCK_ICON = `<svg viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><rect x="2.25" y="2.25" width="15.5" height="15.5" rx="2.5" fill="none" stroke="currentColor" stroke-width="1.5"/><path fill-rule="evenodd" clip-rule="evenodd" d="${X}"/></svg>`;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BlockMathNode,
|
|
3
|
+
INSERT_BLOCK_MATH_COMMAND,
|
|
4
|
+
INSERT_INLINE_MATH_COMMAND,
|
|
5
|
+
InlineMathNode,
|
|
6
|
+
MathExtension,
|
|
7
|
+
} from "lexxy-math";
|
|
8
|
+
import { MathSymbolsPalette } from "lexxy_math_symbols/palette";
|
|
9
|
+
import { MATH_BLOCK_ICON, MATH_INLINE_ICON } from "lexxy_math_symbols/icons";
|
|
10
|
+
import { localeOf, t } from "lexxy_math_symbols/i18n";
|
|
11
|
+
|
|
12
|
+
export { CATEGORIES, ITEMS } from "lexxy_math_symbols/catalog";
|
|
13
|
+
export {
|
|
14
|
+
categoryLabel,
|
|
15
|
+
itemKeywords,
|
|
16
|
+
itemLabel,
|
|
17
|
+
localeData,
|
|
18
|
+
localeOf,
|
|
19
|
+
registerLocale,
|
|
20
|
+
resolveLocale,
|
|
21
|
+
t,
|
|
22
|
+
} from "lexxy_math_symbols/i18n";
|
|
23
|
+
export {
|
|
24
|
+
MathSymbolsPalette,
|
|
25
|
+
forgetSearchFields,
|
|
26
|
+
insertLatex,
|
|
27
|
+
labelOf,
|
|
28
|
+
moveToSlot,
|
|
29
|
+
previewLatex,
|
|
30
|
+
search,
|
|
31
|
+
slotsOf,
|
|
32
|
+
} from "lexxy_math_symbols/palette";
|
|
33
|
+
export { FULL_SET, editorSymbolSet, findSymbol, symbolSet } from "lexxy_math_symbols/sets";
|
|
34
|
+
|
|
35
|
+
if (!customElements.get("lexxy-math-symbols")) {
|
|
36
|
+
customElements.define("lexxy-math-symbols", MathSymbolsPalette);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Gives lexxy-math's editor (<lexxy-math-editor>, the LaTeX input that opens
|
|
40
|
+
// over a formula) the palette under its preview. lexxy-math defines the
|
|
41
|
+
// element and keeps one per Lexxy editor; its `show` is wrapped once: the
|
|
42
|
+
// first show adds the palette (connectedCallback cannot be wrapped: the
|
|
43
|
+
// browser takes it from the class when the element is defined), each show
|
|
44
|
+
// marks the mode (block-only entries are dimmed inline), starts the palette
|
|
45
|
+
// afresh and keeps the panel inside the editor, as does one listener on the
|
|
46
|
+
// window's resize while a panel is open.
|
|
47
|
+
export function installPalette() {
|
|
48
|
+
const MathEditor = customElements.get("lexxy-math-editor");
|
|
49
|
+
if (!MathEditor || MathEditor.prototype.hasSymbolsPalette) return;
|
|
50
|
+
|
|
51
|
+
const { show } = MathEditor.prototype;
|
|
52
|
+
Object.assign(MathEditor.prototype, {
|
|
53
|
+
hasSymbolsPalette: true,
|
|
54
|
+
|
|
55
|
+
show(latex, anchor, options = {}) {
|
|
56
|
+
this.dataset.mode = options.displayMode ? "block" : "inline";
|
|
57
|
+
if (!this.querySelector("lexxy-math-symbols")) {
|
|
58
|
+
this.append(document.createElement("lexxy-math-symbols"));
|
|
59
|
+
}
|
|
60
|
+
show.call(this, latex, anchor, options);
|
|
61
|
+
anchors.set(this, anchor);
|
|
62
|
+
this.querySelector("lexxy-math-symbols").reset();
|
|
63
|
+
keepInsideEditor(this);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
window.addEventListener("resize", realign);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const anchors = new WeakMap();
|
|
70
|
+
|
|
71
|
+
// On a resize the formula moves with the text: the panel goes back under it,
|
|
72
|
+
// placed as lexxy-math places it (4px below, left edges aligned), then back
|
|
73
|
+
// inside the editor.
|
|
74
|
+
function realign() {
|
|
75
|
+
for (const panel of document.querySelectorAll("lexxy-math-editor:not([hidden])")) {
|
|
76
|
+
const anchor = anchors.get(panel);
|
|
77
|
+
const editor = panel.closest("lexxy-editor");
|
|
78
|
+
if (anchor?.isConnected && editor) {
|
|
79
|
+
const box = anchor.getBoundingClientRect();
|
|
80
|
+
const frame = editor.getBoundingClientRect();
|
|
81
|
+
panel.style.top = `${box.bottom - frame.top + 4}px`;
|
|
82
|
+
panel.style.left = `${box.left - frame.left}px`;
|
|
83
|
+
}
|
|
84
|
+
keepInsideEditor(panel);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function keepInsideEditor(panel) {
|
|
89
|
+
const editor = panel.closest("lexxy-editor");
|
|
90
|
+
if (!editor) return;
|
|
91
|
+
const overflow = panel.getBoundingClientRect().right - editor.getBoundingClientRect().right;
|
|
92
|
+
if (overflow > 0) {
|
|
93
|
+
panel.style.left = `${Math.max(0, parseFloat(panel.style.left || 0) - overflow)}px`;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
installPalette();
|
|
98
|
+
|
|
99
|
+
// In Polish; the editor shows them in its own language (t("emptyInline"),
|
|
100
|
+
// t("emptyBlock")).
|
|
101
|
+
export const EMPTY_INLINE = t("emptyInline");
|
|
102
|
+
export const EMPTY_BLOCK = t("emptyBlock");
|
|
103
|
+
|
|
104
|
+
// An empty formula in the editor: lexxy-math shows "$\ldots$" in the text and
|
|
105
|
+
// "Click to add math formula" for a block, which say nothing to someone who
|
|
106
|
+
// does not know LaTeX. Only the editor's DOM changes (createDOM/updateDOM),
|
|
107
|
+
// never the saved HTML (exportDOM).
|
|
108
|
+
//
|
|
109
|
+
// In the editor's language: createDOM makes a node's element before it is in
|
|
110
|
+
// the page, so the language comes from the Lexical editor's root (Lexical
|
|
111
|
+
// passes the editor to createDOM), else the page's. updateDOM's element is
|
|
112
|
+
// in the page already.
|
|
113
|
+
export function installPlaceholders() {
|
|
114
|
+
if (InlineMathNode.prototype.hasSymbolsPlaceholder) return;
|
|
115
|
+
for (const [Node, mark] of [
|
|
116
|
+
[InlineMathNode, markEmptyInline],
|
|
117
|
+
[BlockMathNode, markEmptyBlock],
|
|
118
|
+
]) {
|
|
119
|
+
const { createDOM, updateDOM } = Node.prototype;
|
|
120
|
+
Object.assign(Node.prototype, {
|
|
121
|
+
hasSymbolsPlaceholder: true,
|
|
122
|
+
createDOM(...args) {
|
|
123
|
+
const dom = createDOM.apply(this, args);
|
|
124
|
+
const root = args[1]?.getRootElement?.();
|
|
125
|
+
mark(dom, !this.__latex, localeOf(root || document.documentElement));
|
|
126
|
+
return dom;
|
|
127
|
+
},
|
|
128
|
+
updateDOM(previous, dom, ...rest) {
|
|
129
|
+
const result = updateDOM.call(this, previous, dom, ...rest);
|
|
130
|
+
mark(dom, !this.__latex, localeOf(dom));
|
|
131
|
+
return result;
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function markEmptyInline(span, empty, locale) {
|
|
138
|
+
span.classList.toggle("lexxy-math-inline--empty", empty);
|
|
139
|
+
if (empty) span.textContent = t("emptyInline", {}, locale);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function markEmptyBlock(figure, empty, locale) {
|
|
143
|
+
const preview = figure.querySelector(".lexxy-math-block__preview");
|
|
144
|
+
if (empty && preview) preview.textContent = t("emptyBlock", {}, locale);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
installPlaceholders();
|
|
148
|
+
|
|
149
|
+
// lexxy-math's extension with the toolbar's two buttons (inline formula,
|
|
150
|
+
// block formula) before Lexxy's right-hand group. Configure it in place of
|
|
151
|
+
// MathExtension:
|
|
152
|
+
//
|
|
153
|
+
// configure({ global: { extensions: [MathSymbolsExtension] }, default: { math: true } })
|
|
154
|
+
export default class MathSymbolsExtension extends MathExtension {
|
|
155
|
+
initializeToolbar(toolbar) {
|
|
156
|
+
super.initializeToolbar(toolbar);
|
|
157
|
+
const anchor = toolbar.querySelector(".lexxy-editor__toolbar-button--push-right");
|
|
158
|
+
const locale = localeOf(toolbar);
|
|
159
|
+
const inline = toolbarButton(toolbar, {
|
|
160
|
+
name: "lexxy-math-inline",
|
|
161
|
+
icon: MATH_INLINE_ICON,
|
|
162
|
+
title: t("insertInline", {}, locale),
|
|
163
|
+
command: INSERT_INLINE_MATH_COMMAND,
|
|
164
|
+
});
|
|
165
|
+
const block = toolbarButton(toolbar, {
|
|
166
|
+
name: "lexxy-math-block",
|
|
167
|
+
icon: MATH_BLOCK_ICON,
|
|
168
|
+
title: t("insertBlock", {}, locale),
|
|
169
|
+
command: INSERT_BLOCK_MATH_COMMAND,
|
|
170
|
+
});
|
|
171
|
+
block.classList.add("lexxy-editor__toolbar-group-end");
|
|
172
|
+
toolbar.insertBefore(inline, anchor);
|
|
173
|
+
toolbar.insertBefore(block, anchor);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function toolbarButton(toolbar, { name, icon, title, command }) {
|
|
178
|
+
const button = document.createElement("button");
|
|
179
|
+
button.type = "button";
|
|
180
|
+
button.name = name;
|
|
181
|
+
button.className = "lexxy-editor__toolbar-button";
|
|
182
|
+
button.title = title;
|
|
183
|
+
button.innerHTML = icon;
|
|
184
|
+
button.addEventListener("click", () => {
|
|
185
|
+
toolbar.editor.update(() => toolbar.editor.dispatchCommand(command));
|
|
186
|
+
toolbar.editor.focus();
|
|
187
|
+
});
|
|
188
|
+
return button;
|
|
189
|
+
}
|