-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDictionaryManageDialog.vue
668 lines (612 loc) · 18.7 KB
/
DictionaryManageDialog.vue
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
<template>
<QDialog
v-model="dictionaryManageDialogOpenedComputed"
maximized
transitionShow="jump-up"
transitionHide="jump-down"
class="setting-dialog transparent-backdrop"
>
<QLayout container view="hHh Lpr fFf" class="bg-background">
<QPageContainer>
<QHeader class="q-py-sm">
<QToolbar>
<!-- close button -->
<QBtn
round
flat
icon="sym_r_close"
color="display"
@click="discardOrNotDialog(closeDialog)"
/>
<QToolbarTitle class="text-display">
読み方&アクセント辞書
</QToolbarTitle>
<QBtn
outline
icon="sym_r_add"
label="追加"
textColor="display"
class="text-bold"
:disable="uiLocked"
@click="newWord"
/>
</QToolbar>
</QHeader>
<QPage class="row">
<div v-if="loadingDictState" class="loading-dict">
<div>
<QSpinner color="primary" size="2.5rem" />
<div style="margin-top: 12px">
<template v-if="loadingDictState === 'loading'"
>読み込み中...</template
>
<template v-if="loadingDictState === 'synchronizing'"
>同期中...</template
>
</div>
</div>
</div>
<div class="col-4 word-list-col">
<div
v-if="wordEditing && isNewWordEditing"
class="word-list-disable-overlay"
@click="discardOrNotDialog(cancel)"
></div>
<QList class="word-list">
<QItem
v-for="(value, key) in userDict"
:key
v-ripple
tag="label"
clickable
:active="selectedId === key"
class="word-list-item"
activeClass="active-word"
@click="selectWord(key)"
>
<QItemSection>
<QItemLabel class="text-display">{{
value.surface
}}</QItemLabel>
<QItemLabel caption class="row">
<span>{{ value.yomi }} [{{ wordTypeLabels[getWordTypeFromPartOfSpeech(value)] }}]</span>
<span class="q-ml-auto">優先度:{{ value.priority }}</span>
</QItemLabel>
</QItemSection>
</QItem>
</QList>
</div>
<DictionaryEditWordDialog />
</QPage>
</QPageContainer>
</QLayout>
</QDialog>
</template>
<script lang="ts">
import { Ref, ComputedRef } from "vue";
export const dictionaryManageDialogContextKey = "dictionaryManageDialogContext";
export interface DictionaryManageDialogContext {
wordEditing: Ref<boolean>;
surfaceInput: Ref<QInput | undefined>;
selectedId: Ref<string>;
uiLocked: Ref<boolean>;
userDict: Ref<Record<string, UserDictWord>>;
isOnlyHiraOrKana: Ref<boolean>;
accentPhrase: Ref<AccentPhrase | undefined>;
voiceComputed: ComputedRef<{
engineId: EngineId;
speakerId: SpeakerId;
styleId: StyleId;
}>;
surface: Ref<string>;
yomi: Ref<string>;
wordType: Ref<WordTypes>;
wordTypeLabels: Ref<Record<WordTypes, string>>;
wordPriority: Ref<number>;
isWordChanged: ComputedRef<boolean>;
isNewWordEditing: Ref<boolean>;
setYomi: (text: string, changeWord?: boolean) => Promise<void>;
createUILockAction: <T>(action: Promise<T>) => Promise<T>;
loadingDictProcess: () => Promise<void>;
computeRegisteredAccent: () => number;
discardOrNotDialog: (okCallback: () => void) => Promise<void>;
toWordEditingState: () => void;
toWordSelectedState: () => void;
cancel: () => void;
deleteWord: () => Promise<void>;
getWordTypeFromPartOfSpeech: (dictData: UserDictWord | undefined) => WordTypes;
}
</script>
<script setup lang="ts">
import { computed, ref, watch, provide } from "vue";
import { QInput } from "quasar";
import DictionaryEditWordDialog from "./DictionaryEditWordDialog.vue";
import { useStore } from "@/store";
import { AccentPhrase, UserDictWord, WordTypes } from "@/openapi";
import { EngineId, SpeakerId, StyleId } from "@/type/preload";
import {
convertHiraToKana,
convertLongVowel,
createKanaRegex,
} from "@/domain/japanese";
const defaultWordType = WordTypes.ProperNoun;
const defaultDictPriority = 5;
const props = defineProps<{
modelValue: boolean;
}>();
const emit = defineEmits<{
(e: "update:modelValue", v: boolean): void;
}>();
const store = useStore();
const dictionaryManageDialogOpenedComputed = computed({
get: () => props.modelValue,
set: (val) => emit("update:modelValue", val),
});
const uiLocked = ref(false); // ダイアログ内でstore.getters.UI_LOCKEDは常にtrueなので独自に管理
const loadingDictState = ref<null | "loading" | "synchronizing">("loading");
const userDict = ref<Record<string, UserDictWord>>({});
const createUILockAction = function <T>(action: Promise<T>) {
uiLocked.value = true;
return action.finally(() => {
uiLocked.value = false;
});
};
const loadingDictProcess = async () => {
if (store.state.engineIds.length === 0)
throw new Error("assert engineId.length > 0");
loadingDictState.value = "loading";
try {
userDict.value = await createUILockAction(
store.actions.LOAD_ALL_USER_DICT(),
);
} catch {
const result = await store.actions.SHOW_ALERT_DIALOG({
title: "辞書の取得に失敗しました",
message: "音声合成エンジンの再起動をお試しください。",
});
if (result === "OK") {
dictionaryManageDialogOpenedComputed.value = false;
}
}
loadingDictState.value = "synchronizing";
try {
await createUILockAction(store.actions.SYNC_ALL_USER_DICT());
} catch {
await store.actions.SHOW_ALERT_DIALOG({
title: "辞書の同期に失敗しました",
message: "音声合成エンジンの再起動をお試しください。",
});
}
loadingDictState.value = null;
};
watch(dictionaryManageDialogOpenedComputed, async (newValue) => {
if (newValue) {
await loadingDictProcess();
toInitialState();
}
});
const wordEditing = ref(false);
const surfaceInput = ref<QInput>();
const selectedId = ref("");
const lastSelectedId = ref("");
const surface = ref("");
const yomi = ref("");
const voiceComputed = computed(() => {
const userOrderedCharacterInfos =
store.getters.USER_ORDERED_CHARACTER_INFOS("talk");
if (userOrderedCharacterInfos == undefined)
throw new Error("assert USER_ORDERED_CHARACTER_INFOS");
if (store.state.engineIds.length === 0)
throw new Error("assert engineId.length > 0");
const characterInfo = userOrderedCharacterInfos[0].metas;
const speakerId = characterInfo.speakerUuid;
const { engineId, styleId } = characterInfo.styles[0];
return { engineId, speakerId, styleId };
});
const kanaRegex = createKanaRegex();
const isOnlyHiraOrKana = ref(true);
const accentPhrase = ref<AccentPhrase | undefined>();
const setYomi = async (text: string, changeWord?: boolean) => {
const { engineId, styleId } = voiceComputed.value;
// テキスト長が0の時にエラー表示にならないように、テキスト長を考慮する
isOnlyHiraOrKana.value = !text.length || kanaRegex.test(text);
// 読みが変更されていない場合は、アクセントフレーズに変更を加えない
// ただし、読みが同じで違う単語が存在する場合が考えられるので、changeWordフラグを考慮する
// 「ガ」が自動挿入されるので、それを考慮してsliceしている
if (
text ==
accentPhrase.value?.moras
.map((v) => v.text)
.join("")
.slice(0, -1) &&
!changeWord
) {
return;
}
if (isOnlyHiraOrKana.value && text.length) {
text = convertHiraToKana(text);
text = convertLongVowel(text);
accentPhrase.value = (
await createUILockAction(
store.actions.FETCH_ACCENT_PHRASES({
text: text + "ガ'",
engineId,
styleId,
isKana: true,
}),
)
)[0];
if (selectedId.value && userDict.value[selectedId.value].yomi === text) {
accentPhrase.value.accent = computeDisplayAccent();
}
} else {
accentPhrase.value = undefined;
}
yomi.value = text;
};
// accent phraseにあるaccentと実際に登録するアクセントには差が生まれる
// アクセントが自動追加される「ガ」に指定されている場合、
// 実際に登録するaccentの値は0となるので、そうなるように処理する
const computeRegisteredAccent = () => {
if (!accentPhrase.value) return 0; // エラーにさせないために0を返す
let accent = accentPhrase.value.accent;
accent = accent === accentPhrase.value.moras.length ? 0 : accent;
return accent;
};
// computeの逆
// 辞書から得たaccentが0の場合に、自動で追加される「ガ」の位置にアクセントを表示させるように処理する
const computeDisplayAccent = () => {
if (!accentPhrase.value || !selectedId.value) return 0; // エラーにさせないために0を返す
let accent = userDict.value[selectedId.value].accentType;
accent = accent === 0 ? accentPhrase.value.moras.length : accent;
return accent;
};
const wordType = ref<WordTypes>(defaultWordType);
const wordTypeLabels = ref({
[WordTypes.ProperNoun]: "固有名詞",
[WordTypes.LocationName]: "地名",
[WordTypes.OrganizationName]: "組織・施設名",
[WordTypes.PersonName]: "人名",
[WordTypes.PersonFamilyName]: "人名 - 姓",
[WordTypes.PersonGivenName]: "人名 - 名",
[WordTypes.CommonNoun]: "一般名詞",
[WordTypes.Verb]: "動詞",
[WordTypes.Adjective]: "形容詞",
[WordTypes.Suffix]: "接尾辞",
});
// 品詞フィールドから WordTypes を推定する関数
const getWordTypeFromPartOfSpeech = (dictData: UserDictWord | undefined): WordTypes => {
// 基本ないが、もし dictData が undefined の場合は固有名詞として扱う
if (!dictData) return WordTypes.ProperNoun;
const { partOfSpeech, partOfSpeechDetail1, partOfSpeechDetail2, partOfSpeechDetail3 } = dictData;
if (partOfSpeech === "名詞") {
if (partOfSpeechDetail1 === "固有名詞") {
if (partOfSpeechDetail2 === "地域" && partOfSpeechDetail3 === "一般") {
return WordTypes.LocationName;
}
if (partOfSpeechDetail2 === "組織") {
return WordTypes.OrganizationName;
}
if (partOfSpeechDetail2 === "人名") {
if (partOfSpeechDetail3 === "一般") {
return WordTypes.PersonName;
}
if (partOfSpeechDetail3 === "姓") {
return WordTypes.PersonFamilyName;
}
if (partOfSpeechDetail3 === "名") {
return WordTypes.PersonGivenName;
}
}
return WordTypes.ProperNoun;
}
if (partOfSpeechDetail1 === "接尾") return WordTypes.Suffix;
return WordTypes.CommonNoun;
}
if (partOfSpeech === "動詞") return WordTypes.Verb;
if (partOfSpeech === "形容詞") return WordTypes.Adjective;
// デフォルトは固有名詞
return WordTypes.ProperNoun;
};
const wordPriority = ref(defaultDictPriority);
// 操作(ステートの移動)
const isWordChanged = computed(() => {
if (selectedId.value === "") {
return (
surface.value != "" && yomi.value != "" && accentPhrase.value != undefined
);
}
// 一旦代入することで、userDictそのものが更新された時もcomputedするようにする
const dict = userDict.value;
const dictData = dict[selectedId.value];
const currentWordType = getWordTypeFromPartOfSpeech(dictData);
return (
dictData &&
(dictData.surface !== surface.value ||
dictData.yomi !== yomi.value ||
dictData.accentType !== computeRegisteredAccent() ||
currentWordType !== wordType.value ||
dictData.priority !== wordPriority.value)
);
});
const deleteWord = async () => {
const result = await store.actions.SHOW_WARNING_DIALOG({
title: "単語を削除しますか?",
message: `単語「${userDict.value[selectedId.value].surface}」を削除します。`,
actionName: "削除する",
isWarningColorButton: true,
cancel: "削除しない",
});
if (result === "OK") {
try {
void store.actions.SHOW_LOADING_SCREEN({
message: "単語を辞書から削除しています...",
});
await createUILockAction(
store.actions.DELETE_WORD({
wordUuid: selectedId.value,
}),
);
} catch {
void store.actions.SHOW_ALERT_DIALOG({
title: "単語の削除に失敗しました",
message: "音声合成エンジンの再起動をお試しください。",
});
return;
} finally {
await store.actions.HIDE_ALL_LOADING_SCREEN();
}
await loadingDictProcess();
toInitialState();
}
};
const discardOrNotDialog = async (okCallback: () => void) => {
if (isWordChanged.value) {
const result = await store.actions.SHOW_WARNING_DIALOG({
title: "単語の追加・変更を破棄しますか?",
message: "保存されていない変更内容は失われます。",
actionName: "破棄する",
isWarningColorButton: true,
});
if (result === "OK") {
okCallback();
}
} else {
okCallback();
}
};
const isNewWordEditing = ref(false);
const newWord = () => {
const newWordImpl = () => {
isNewWordEditing.value = true;
selectedId.value = "";
surface.value = "";
void setYomi("");
wordType.value = defaultWordType;
wordPriority.value = defaultDictPriority;
editWord();
};
if (wordEditing.value && isWordChanged.value) {
void discardOrNotDialog(newWordImpl);
} else {
newWordImpl();
}
};
const editWord = () => {
toWordEditingState();
};
const selectWord = (id: string) => {
const selectWordImpl = () => {
selectedId.value = id;
surface.value = userDict.value[id].surface;
void setYomi(userDict.value[id].yomi, true);
wordType.value = getWordTypeFromPartOfSpeech(userDict.value[id]);
wordPriority.value = userDict.value[id].priority;
toWordSelectedState();
editWord();
};
if (wordEditing.value && isWordChanged.value) {
void discardOrNotDialog(selectWordImpl);
} else {
selectWordImpl();
}
};
const cancel = () => {
toInitialState();
};
const closeDialog = () => {
toDialogClosedState();
};
// ステートの移動
// 初期状態
const toInitialState = () => {
isNewWordEditing.value = false;
wordEditing.value = false;
selectedId.value = "";
surface.value = "";
void setYomi("");
wordType.value = defaultWordType;
wordPriority.value = defaultDictPriority;
// 辞書の最初の項目を選択する
if (Object.keys(userDict.value).length > 0) {
// 前回選択していた項目があればそれを選択、なければ最初の項目を選択
const targetKey = lastSelectedId.value && userDict.value[lastSelectedId.value]
? lastSelectedId.value
: Object.keys(userDict.value)[0];
selectWord(targetKey);
}
};
// 単語が選択されているだけの状態
const toWordSelectedState = () => {
wordEditing.value = false;
// 選択された項目を記憶
lastSelectedId.value = selectedId.value;
};
// 単語が編集されている状態
const toWordEditingState = () => {
wordEditing.value = true;
surfaceInput.value?.focus();
};
// ダイアログが閉じている状態
const toDialogClosedState = () => {
dictionaryManageDialogOpenedComputed.value = false;
};
provide<DictionaryManageDialogContext>(dictionaryManageDialogContextKey, {
wordEditing,
surfaceInput,
selectedId,
uiLocked,
userDict,
isOnlyHiraOrKana,
accentPhrase,
voiceComputed,
surface,
yomi,
wordType,
wordTypeLabels,
wordPriority,
isWordChanged,
isNewWordEditing,
setYomi,
createUILockAction,
loadingDictProcess,
computeRegisteredAccent,
discardOrNotDialog,
toWordEditingState,
toWordSelectedState,
cancel,
deleteWord,
getWordTypeFromPartOfSpeech,
});
</script>
<style lang="scss">
.play-button .material-symbols-rounded {
font-variation-settings: 'FILL' 1, 'wght' 300, 'GRAD' 200, 'opsz' 24 !important;
}
</style>
<style lang="scss" scoped>
@use "@/styles/colors" as colors;
@use "@/styles/variables" as vars;
.word-list-col {
border-right: solid 1px colors.$surface;
position: relative; // オーバーレイのため
overflow-x: hidden;
}
.word-list-header {
margin: 1rem;
gap: 0.5rem;
align-items: center;
justify-content: space-between;
.word-list-title {
flex-grow: 1;
}
}
.word-list {
// menubar-height + toolbar-height + window-border-width
height: calc(
100vh - #{vars.$menubar-height + vars.$toolbar-height +
vars.$window-border-width}
);
width: 100%;
overflow-y: auto;
.word-list-item {
padding-right: 16px;
&.active-word {
background: hsl(206 66% 32% / 1);
padding-right: 12px;
border-right: 4px solid colors.$primary;
}
}
}
.loading-dict {
background-color: rgba(0, 0, 0, 0.3);
position: absolute;
inset: 0;
z-index: 10;
display: flex;
text-align: center;
align-items: center;
justify-content: center;
> div {
color: colors.$display;
background: colors.$background;
border-radius: 6px;
padding: 16px 20px;
}
}
.word-list-disable-overlay {
background-color: rgba($color: #000000, $alpha: 0.4);
width: 100%;
height: 100%;
position: absolute;
z-index: 10;
}
.word-editor {
display: flex;
flex-flow: column;
height: calc(
100vh - #{vars.$menubar-height + vars.$toolbar-height +
vars.$window-border-width}
) !important;
}
.model-detail-content {
flex: 1;
overflow-y: auto;
padding: 0;
height: calc(100% - 66px);
}
.fixed-bottom-buttons {
display: flex;
justify-content: flex-end;
padding: 16px;
padding-top: 14px;
height: 66px;
border-top: 2px solid var(--color-splitter);
}
.word-input {
width: 100%;
}
.desc-row {
color: rgba(colors.$display-rgb, 0.7);
font-size: 12px;
}
.play-button {
margin: auto 0;
padding-right: 16px;
}
.accent-phrase-table {
flex-grow: 1;
align-self: stretch;
display: flex;
height: 130px;
overflow-x: scroll;
width: calc(66vw - 140px);
.mora-table {
display: inline-grid;
align-self: stretch;
grid-template-rows: 1fr 60px 30px;
.text-cell {
padding: 0;
min-width: 20px;
max-width: 20px;
grid-row-start: 3;
text-align: center;
white-space: nowrap;
color: colors.$display;
position: relative;
}
.splitter-cell {
min-width: 20px;
max-width: 20px;
grid-row: 3 / span 1;
z-index: vars.$detail-view-splitter-cell-z-index;
}
}
}
.save-delete-reset-buttons {
padding: 20px;
display: flex;
flex: 1;
align-items: flex-end;
}
</style>