This repository was archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmyscript-text-exports.js
219 lines (194 loc) · 7.83 KB
/
myscript-text-exports.js
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
/* eslint-disable class-methods-use-this,no-undef,no-param-reassign,no-underscore-dangle */
import { html, PolymerElement } from '@polymer/polymer';
/**
`myscript-text-exports` is a component to display `myscript-text-web` exports candidates.
<myscript-text-exports
exports="myscript-text-web.exports">
</myscript-text-exports>
Custom property | Description | Default
----------------|-------------|----------
`--myscript-text-exports-color` | Text candidates color | #1580CD
`--myscript-text-exports-background-color` | Text candidates background color | #EDF0F2
`--myscript-text-exports-selected-color` | Selected candidate color | #FFFFFF
`--myscript-text-exports-selected-background-color` | Selected candidate background color | #1580CD
`--myscript-text-exports-predicted-color` | Candidate predicted part color | #73818C
`--myscript-text-exports-completed-color` | Candidate completed part color | #1A9FFF
*/
class MyScriptTextExports extends PolymerElement {
static get template() {
return html`
<style>
:host {
--myscript-text-exports-color: #1580CD;
--myscript-text-exports-background-color: #EDF0F2;
--myscript-text-exports-selected-color: #FFFFFF;
--myscript-text-exports-selected-background-color: #1580CD;
--myscript-text-exports-predicted-color: #73818C;
--myscript-text-exports-completed-color: #1A9FFF;
--myscript-text-candidate: {
padding: .5rem .75rem;
margin: 0 .125rem;
border-radius: 3px;
cursor: pointer;
flex: 0 0 auto;
};
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: stretch;
color: var(--myscript-text-exports-color);
min-height: 60px;
}
#plaintext {
flex-grow: 2;
text-align: center;
overflow: auto;
padding: 5px 0;
}
#candidates {
display: flex;
align-items: center;
overflow: auto;
padding: 5px 0;
}
.text {
color: var(--myscript-text-exports-color);
background-color: var(--myscript-text-exports-background-color);
@apply --myscript-text-candidate;
}
.text[selected] {
color: var(--myscript-text-exports-selected-color);
background: var(--myscript-text-exports-selected-background-color);
}
.candidate[selected] * {
color: inherit;
background-color: inherit;
}
.char.predicted {
color: var(--myscript-text-exports-predicted-color, #73818C);
}
.char.completed {
color: var(--myscript-text-exports-completed-color, #1A9FFF);
}
</style>
<div id="plaintext">{{ _getPlainText(exports) }}</div>
<div id="candidates">
<template is="dom-if" if="[[ exports.CANDIDATES ]]">
<!--WARN: templates needs to be inline to avoid carriage return after span-->
<template is="dom-repeat" id="textCandidateList" items="[[ exports.CANDIDATES.textSegmentResult.candidates ]]" as="textCandidate" index-as="textIndex">
<span class\$="text {{ _getCandidateFlags(candidate) }}" selected\$="[[ _isSelected(textIndex, exports.CANDIDATES.textSegmentResult.selectedCandidateIdx) ]]" on-tap="_select">
<template is="dom-if" if="[[ !textCandidate.children ]]">
{{ textCandidate.label }}
</template>
<template is="dom-if" if="[[ textCandidate.children ]]"><!--WARN: templates needs to be inline to avoid carriage return after span-->
<template is="dom-repeat" id="wordCandidateList" items="[[ _getChildCandidates(textCandidate, exports, 'text') ]]" as="wordCandidate">
<span class\$="word {{ _getCandidateFlags(wordCandidate) }}">
<template is="dom-if" if="[[ !wordCandidate.children ]]">
{{ wordCandidate.label }}
</template>
<template is="dom-if" if="[[ wordCandidate.children ]]">
<template is="dom-repeat" id="charCandidateList" items="[[ _getChildCandidates(wordCandidate, exports, 'word') ]]" as="charCandidate"><span class\$="char {{ _getCandidateFlags(charCandidate) }}">{{ charCandidate.label }}</span></template>
</template>
</span>
</template>
</template>
</span>
</template>
</template>
</div>`;
}
static get is() {
return 'myscript-text-exports';
}
static get properties() {
return {
/**
* Exports result.
* @attribute exports
* @type {Object<String, Object>}.
*/
exports: {
type: Object,
notify: true
}
};
}
_getPlainText(exports) {
if (exports) {
if (exports.TEXT) {
return exports.TEXT;
}
return exports['text/plain'];
}
return undefined;
}
/**
* Select a new candidate
* @param e
*/
_select(e) {
const exports = this.exports;
if (exports.CANDIDATES) {
exports.CANDIDATES.textSegmentResult.selectedCandidateIdx = e.model.textIndex;
exports.TEXT = exports.CANDIDATES.textSegmentResult.candidates[exports.CANDIDATES.textSegmentResult.selectedCandidateIdx].label;
}
this.exports = {};
this.exports = exports;
this.dispatchEvent(new CustomEvent('change', {
detail: {
exports
}
}));
}
_isSelected(index, selectedIndex) {
return index === selectedIndex;
}
_getChildSegments(candidate, exports, type) {
const segments = [];
function addSegment(child, segment) {
if (segment.inkRanges === child.inkRanges) {
segment.selectedCandidateIdx = child.selectedCandidateIdx;
segments.push(segment);
}
}
if (candidate.children) {
candidate.children.forEach((child, index) => {
if (type === 'text' &&
exports &&
exports.CANDIDATES &&
exports.CANDIDATES.wordSegments &&
exports.CANDIDATES.wordSegments.length > -1) {
exports.CANDIDATES.wordSegments.forEach(segment => addSegment(child, segment));
} else if (type === 'word' &&
exports &&
exports.CANDIDATES &&
exports.CANDIDATES.charSegments &&
exports.CANDIDATES.charSegments.length > -1) {
if (!child.inkRanges) {
segments.push({
candidates: [{
label: candidate.label.charAt(index),
flags: candidate.flags
}],
selectedCandidateIdx: 0
});
} else {
exports.CANDIDATES.charSegments.forEach(segment => addSegment(child, segment));
}
}
});
}
return segments;
}
_getChildCandidates(candidate, exports, type) {
return this._getChildSegments(candidate, exports, type)
.map(segment => segment.candidates[segment.selectedCandidateIdx]);
}
_getCandidateFlags(candidate) {
if (candidate && candidate.flags) {
return candidate.flags.join(' ').toLowerCase();
}
return '';
}
}
customElements.define(MyScriptTextExports.is, MyScriptTextExports);