Repository navigation
Expand file tree
/
Copy pathsticky.js
More file actions
284 lines (251 loc) · 10 KB
/
Copy pathsticky.js
File metadata and controls
284 lines (251 loc) · 10 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* Nexus Sticky - フロントエンドロジック
* Tauri 2.0 の withGlobalTauri: true 設定のもとで動作
*/
'use strict';
// Tauri 2.0 グローバル API
const { invoke } = window.__TAURI__.core;
const { listen } = window.__TAURI__.event;
// ─────────────────────────────────────────────
// 状態管理
// ─────────────────────────────────────────────
let windowId = null;
let currentColor = '#FFEB3B';
let currentOpacity = 0.95;
let isPinned = false;
let debounceTimer = null;
const DEBOUNCE_MS = 200; // Requirement 2.4
const MAX_CHARS = 10000; // Requirement 11.4
// DOM要素
const stickyEl = document.getElementById('sticky-window');
const contentEl = document.getElementById('content');
const charCountEl = document.getElementById('char-count');
const pinBtn = document.getElementById('pin-btn');
const colorBtn = document.getElementById('color-btn');
const opacityBtn = document.getElementById('opacity-btn');
const minimizeBtn = document.getElementById('minimize-btn');
const closeBtn = document.getElementById('close-btn');
const colorPanel = document.getElementById('color-panel');
const opacityPanel = document.getElementById('opacity-panel');
const opacitySlider = document.getElementById('opacity-slider');
const opacityValue = document.getElementById('opacity-value');
// ─────────────────────────────────────────────
// 初期化 (Requirement 全体)
// ─────────────────────────────────────────────
async function init() {
try {
// 自ウィンドウIDを取得
windowId = await invoke('get_window_id');
// バックエンドから現在状態を取得して UI に適用
const state = await invoke('get_window_state', { windowId });
applyState(state);
// イベントリスナーを設定
setupUiListeners();
await setupEventListeners();
console.log('[NexusSticky] Initialized:', windowId);
} catch (err) {
console.error('[NexusSticky] Init failed:', err);
}
}
/** バックエンドの状態を UI に適用する */
function applyState(state) {
if (state.content) contentEl.value = state.content;
if (state.color) applyColor(state.color);
if (state.opacity != null) applyOpacity(state.opacity);
if (state.pinned != null) applyPin(state.pinned);
updateCharCount();
}
// ─────────────────────────────────────────────
// UI ヘルパー
// ─────────────────────────────────────────────
function applyColor(color) {
currentColor = color;
stickyEl.style.backgroundColor = color;
// 選択中のswatchをハイライト
document.querySelectorAll('.color-swatch').forEach(sw => {
sw.classList.toggle('selected', sw.dataset.color === color);
});
}
function applyOpacity(opacity) {
currentOpacity = opacity;
document.documentElement.style.opacity = opacity;
opacitySlider.value = Math.round(opacity * 100);
opacityValue.textContent = `${Math.round(opacity * 100)}%`;
}
function applyPin(pinned) {
isPinned = pinned;
pinBtn.classList.toggle('active', pinned);
pinBtn.title = pinned ? '常に最前面(ON)' : '常に最前面(OFF)';
}
function updateCharCount() {
const len = contentEl.value.length;
charCountEl.textContent = `${len} / ${MAX_CHARS}`;
charCountEl.classList.toggle('warning', len > MAX_CHARS * 0.9);
}
function closeAllPanels() {
colorPanel.hidden = true;
opacityPanel.hidden = true;
}
// ─────────────────────────────────────────────
// UI イベントリスナー (Requirement 3.3, 4.2, 5.2, 11.2, 12.2)
// ─────────────────────────────────────────────
function setupUiListeners() {
// ピン留めボタン (Requirement 4.2, 4.3)
pinBtn.addEventListener('click', async () => {
closeAllPanels();
try {
const pinned = await invoke('toggle_pin', { windowId });
applyPin(pinned);
} catch (err) {
console.error('[NexusSticky] toggle_pin failed:', err);
}
});
// 色選択ボタン (Requirement 12.2)
colorBtn.addEventListener('click', () => {
opacityPanel.hidden = true;
colorPanel.hidden = !colorPanel.hidden;
});
// 透過率ボタン (Requirement 5.2)
opacityBtn.addEventListener('click', () => {
colorPanel.hidden = true;
opacityPanel.hidden = !opacityPanel.hidden;
});
// 色スウォッチクリック (Requirement 12.2)
document.querySelectorAll('.color-swatch').forEach(swatch => {
swatch.addEventListener('click', async () => {
const color = swatch.dataset.color;
applyColor(color);
closeAllPanels();
try {
await invoke('update_color', { windowId, color });
} catch (err) {
console.error('[NexusSticky] update_color failed:', err);
}
});
});
// 透過率スライダー (Requirement 5.2)
opacitySlider.addEventListener('input', () => {
const opacity = parseInt(opacitySlider.value) / 100;
applyOpacity(opacity);
});
opacitySlider.addEventListener('change', async () => {
const opacity = parseInt(opacitySlider.value) / 100;
try {
await invoke('update_opacity', { windowId, opacity });
} catch (err) {
console.error('[NexusSticky] update_opacity failed:', err);
}
});
// 最小化ボタン (Requirement 3.3)
minimizeBtn.addEventListener('click', async () => {
closeAllPanels();
try {
// Tauri 2.0: getCurrentWindow()
const { getCurrentWindow } = window.__TAURI__.window;
await getCurrentWindow().minimize();
} catch (err) {
console.error('[NexusSticky] minimize failed:', err);
}
});
// 閉じるボタン (Requirement 3.3)
closeBtn.addEventListener('click', async () => {
try {
await invoke('close_window', { windowId });
} catch (err) {
console.error('[NexusSticky] close_window failed:', err);
}
});
// テキスト入力(デバウンス 200ms)(Requirement 11.2, 11.3, 2.4)
contentEl.addEventListener('input', () => {
const len = contentEl.value.length;
updateCharCount();
// 10,000文字制限に達した場合の通知 (Requirement 11.5)
if (len >= MAX_CHARS) {
charCountEl.classList.remove('limit-reached');
void charCountEl.offsetWidth; // reflow for restart animation
charCountEl.classList.add('limit-reached');
}
// バックエンドへのデバウンス送信 (Requirement 2.4)
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
try {
await invoke('update_content', { windowId, content: contentEl.value });
} catch (err) {
console.error('[NexusSticky] update_content failed:', err);
}
}, DEBOUNCE_MS);
});
// パネル外クリックで閉じる
document.addEventListener('click', (e) => {
if (!colorBtn.contains(e.target) && !colorPanel.contains(e.target)) {
colorPanel.hidden = true;
}
if (!opacityBtn.contains(e.target) && !opacityPanel.contains(e.target)) {
opacityPanel.hidden = true;
}
});
// ウィンドウリサイズ時にサイズをバックエンドに通知
let resizeTimer = null;
window.addEventListener('resize', () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(async () => {
try {
await invoke('update_size', {
windowId,
width: window.innerWidth,
height: window.innerHeight,
});
} catch (_) {}
}, 500);
});
}
// ─────────────────────────────────────────────
// Tauri イベントリスナー(クロスウィンドウ同期)
// Requirement 2.1, 2.2, 2.4
// ─────────────────────────────────────────────
async function setupEventListeners() {
// コンテンツ変更(他ウィンドウからの同期)(Requirement 2.4)
await listen('content-changed', (event) => {
const { window_id, content } = event.payload.data;
if (window_id !== windowId) {
// 他ウィンドウからの変更を反映(カーソル位置を保持)
const sel = contentEl.selectionStart;
contentEl.value = content;
try { contentEl.setSelectionRange(sel, sel); } catch (_) {}
updateCharCount();
}
});
// 色変更(Requirement 2.1)
await listen('color-changed', (event) => {
const { window_id, color } = event.payload.data;
if (window_id === windowId) {
applyColor(color);
}
});
// 透過率変更(Requirement 5.4)
await listen('opacity-changed', (event) => {
const { window_id, opacity } = event.payload.data;
if (window_id === windowId) {
applyOpacity(opacity);
}
});
// ピン留め変更(Requirement 4.4, 2.2)
await listen('pinned-changed', (event) => {
const { window_id, pinned } = event.payload.data;
if (window_id === windowId) {
applyPin(pinned);
}
});
// ウィンドウ削除通知
await listen('window-closed', (event) => {
const { window_id } = event.payload.data;
if (window_id === windowId) {
// 自分が削除された場合(通常は close_window コマンドで処理済み)
window.close();
}
});
}
// ─────────────────────────────────────────────
// エントリーポイント
// ─────────────────────────────────────────────
document.addEventListener('DOMContentLoaded', init);