Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
186 lines (160 loc) · 4.86 KB
/
Copy pathapp.js
File metadata and controls
186 lines (160 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { PRESETS } from './presets/index.js';
import { REGIONS } from './regions.js';
import { loadFont } from './fonts.js';
import { buildCustomColumns, setupCustomEvents, customState } from './custom-tab.js';
const grid = document.getElementById('grid');
const input = document.getElementById('glyph-input');
const copyBtn = document.getElementById('copy-btn');
const themeToggle = document.getElementById('theme-toggle');
const sizeInput = document.getElementById('size-input');
const presetBar = document.getElementById('preset-bar');
function applyTheme(light) {
document.body.classList.toggle('light', light);
themeToggle.textContent = light ? '\u263E' : '\u2600';
localStorage.setItem('theme', light ? 'light' : 'dark');
}
const saved = localStorage.getItem('theme');
const prefersLight = saved ? saved === 'light' : matchMedia('(prefers-color-scheme: light)').matches;
applyTheme(prefersLight);
themeToggle.addEventListener('click', () => {
applyTheme(!document.body.classList.contains('light'));
});
let activePreset = 'sans';
function syncState() {
const text = input.value;
const params = new URLSearchParams();
params.set('p', activePreset);
if (activePreset === 'custom') {
params.set('f', customState.fonts.join('|'));
}
let url = location.pathname;
const qs = params.toString();
if (qs) url += '?' + qs;
if (text) url += '#' + encodeURIComponent(text);
history.replaceState(null, '', url);
}
function readHash() {
if (location.hash) {
input.value = decodeURIComponent(location.hash.slice(1));
return true;
}
return false;
}
function readState() {
const params = new URLSearchParams(location.search);
const fontsStr = params.get('f');
if (fontsStr) {
customState.fonts = fontsStr.split('|');
localStorage.setItem('customFonts', JSON.stringify(customState.fonts));
}
return params.get('p') || null;
}
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(location.href).then(() => {
copyBtn.textContent = 'Copied!';
copyBtn.classList.add('copied');
setTimeout(() => {
copyBtn.textContent = 'Copy Link';
copyBtn.classList.remove('copied');
}, 1500);
});
});
function applySize(px) {
document.querySelectorAll('.glyph-display').forEach(el => {
el.style.fontSize = px + 'px';
});
}
const savedSize = localStorage.getItem('size') || 24;
sizeInput.value = savedSize;
applySize(savedSize);
window.addEventListener('hashchange', () => {
readHash();
render();
});
function cssFamily(name) {
return `'${name}'`;
}
function buildRegionColumns() {
grid.innerHTML = '';
REGIONS.forEach(r => {
grid.insertAdjacentHTML('beforeend', `
<div class="column" data-region="${r.key}">
<div class="column-header">
<div class="region">${r.name}</div>
<div class="font-name"></div>
</div>
<div class="glyph-display"></div>
</div>
`);
});
const preset = PRESETS[activePreset];
grid.querySelectorAll('.column').forEach(col => {
const region = col.dataset.region;
const fontName = preset.fonts[region];
col.style.fontFamily = cssFamily(fontName);
col.querySelector('.font-name').textContent = fontName;
});
}
function applyPreset(presetKey) {
activePreset = presetKey;
document.querySelectorAll('.preset-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.preset === presetKey);
});
if (presetKey === 'custom') {
buildCustomColumns(grid);
} else {
buildRegionColumns();
}
applySize(parseInt(sizeInput.value) || 24);
render();
syncState();
}
function render() {
const text = input.value;
grid.querySelectorAll('.glyph-display').forEach(display => {
if (!text) {
display.innerHTML = '<div class="empty-state">Enter characters above</div>';
} else {
display.textContent = text;
}
});
}
Object.keys(PRESETS).forEach(key => {
const btn = document.createElement('button');
btn.className = 'preset-btn' + (key === activePreset ? ' active' : '');
btn.dataset.preset = key;
btn.textContent = PRESETS[key].label;
btn.addEventListener('click', async () => {
btn.disabled = true;
if (key !== 'custom') {
await loadFont(key, PRESETS[key]);
}
btn.disabled = false;
applyPreset(key);
});
presetBar.appendChild(btn);
});
input.addEventListener('input', () => {
syncState();
render();
});
sizeInput.addEventListener('input', () => {
const v = parseInt(sizeInput.value) || 24;
applySize(v);
localStorage.setItem('size', v);
});
const DEFAULT_CHARS = '直骨神平';
async function init() {
const presetKey = readState();
readHash() || (input.value = DEFAULT_CHARS);
if (presetKey && presetKey !== 'sans') {
if (presetKey !== 'custom') {
await loadFont(presetKey, PRESETS[presetKey]);
}
applyPreset(presetKey);
} else {
applyPreset('sans');
}
}
setupCustomEvents(grid, { render, syncState, input });
init();