|
25 | 25 | --myscript-common-element-button-focus-background: var(--myscript-text-web-button-focus-background);
|
26 | 26 | --myscript-common-element-button-disabled-background: var(--myscript-text-web-button-disabled-background);
|
27 | 27 | }
|
| 28 | + |
28 | 29 | myscript-common-element {
|
29 |
| - height: 100%; |
30 |
| - } |
31 |
| - myscript-common-element.result { |
32 | 30 | height: calc(100% - 100px);
|
33 | 31 | }
|
| 32 | + |
34 | 33 | .resultField {
|
35 | 34 | overflow: auto;
|
36 | 35 | text-align: center;
|
37 | 36 | font-size: xx-large;
|
38 | 37 | min-height: 95px;
|
39 | 38 | max-height: 95px;
|
40 |
| - color: #1580CD; |
41 |
| - } |
42 |
| - span[selected] { |
43 |
| - color: #FFFFFF; |
44 |
| - background-color: #1580CD; |
45 | 39 | }
|
46 |
| - span[selected] * { |
47 |
| - color: inherit; |
48 |
| - background-color: inherit; |
| 40 | + |
| 41 | + .resultField[hidden] + myscript-common-element { |
| 42 | + height: 100%; |
49 | 43 | }
|
| 44 | + |
50 | 45 | .text {
|
| 46 | + color: #1580CD; |
51 | 47 | background-color: #EDF0F2;
|
52 | 48 | font-size: 1.75rem;
|
53 | 49 | padding: .5rem .75rem;
|
54 | 50 | border-radius: 3px;
|
55 | 51 | cursor: pointer;
|
56 | 52 | }
|
| 53 | + |
| 54 | + .text[selected] { |
| 55 | + color: #FFFFFF; |
| 56 | + background-color: #1580CD; |
| 57 | + } |
| 58 | + |
| 59 | + .candidate[selected] * { |
| 60 | + color: inherit; |
| 61 | + background-color: inherit; |
| 62 | + } |
| 63 | + |
57 | 64 | .char.predicted {
|
58 | 65 | color: #73818C;
|
59 | 66 | }
|
| 67 | + |
60 | 68 | .char.completed {
|
61 | 69 | color: #1A9FFF;
|
62 | 70 | }
|
63 | 71 | </style>
|
64 | 72 | <template>
|
65 | 73 | <div class="resultField" hidden="[[ displayResultZone(hideresult, unloaded) ]]">
|
66 |
| - <template is="dom-repeat" id="candidateList" items="[[ candidates ]]" as="candidate"> |
67 |
| - <span id="candidate" index="[[ index ]]" selected$="[[ candidate.selected ]]" inner-h-t-m-l="[[ candidate.customlabel ]]" on-tap="_select"></span> |
| 74 | + <template is="dom-repeat" id="textCandidateList" items="[[ candidates ]]" as="textCandidate" index-as="textIndex"> |
| 75 | + <span class$="text {{ _getCandidateFlags(candidate) }}" selected$="[[ _isSelected(textIndex, result.textSegmentResult.selectedCandidateIdx) ]]" on-tap="_select"> |
| 76 | + <template is="dom-if" if="[[ !textCandidate.children ]]"> |
| 77 | + {{ textCandidate.label }} |
| 78 | + </template> |
| 79 | + <template is="dom-if" if="[[ textCandidate.children ]]"><!--WARN: templates needs to be inline to avoid carriage return after span--> |
| 80 | + <template is="dom-repeat" id="wordCandidateList" items="[[ _getChildCandidates(textCandidate, result, 'text') ]]" as="wordCandidate"> |
| 81 | + <span class$="word {{ _getCandidateFlags(wordCandidate) }}"> |
| 82 | + <template is="dom-if" if="[[ !wordCandidate.children ]]"> |
| 83 | + {{ wordCandidate.label }} |
| 84 | + </template> |
| 85 | + <template is="dom-if" if="[[ wordCandidate.children ]]"> |
| 86 | + <template is="dom-repeat" id="charCandidateList" items="[[ _getChildCandidates(wordCandidate, result, 'word') ]]" as="charCandidate"><span class$="char {{ _getCandidateFlags(charCandidate) }}">{{ charCandidate.label }}</span></template> |
| 87 | + </template> |
| 88 | + </span> |
| 89 | + </template> |
| 90 | + </template> |
| 91 | + </span> |
68 | 92 | </template>
|
69 | 93 | </div>
|
70 | 94 | <myscript-common-element
|
|
147 | 171 | myscriptTextWebElement.result = {};
|
148 | 172 | myscriptTextWebElement.resultlabel = '';
|
149 | 173 | myscriptTextWebElement.candidates = [];
|
| 174 | + |
| 175 | + if (eventDetail.recognitionResult && eventDetail.recognitionResult['text/plain']) { |
| 176 | + myscriptTextWebElement.resultlabel = eventDetail.recognitionResult['text/plain']; |
| 177 | + } |
150 | 178 | if (eventDetail.rawResult &&
|
151 | 179 | eventDetail.rawResult.result &&
|
152 | 180 | eventDetail.rawResult.result.textSegmentResult &&
|
153 | 181 | eventDetail.rawResult.result.textSegmentResult.candidates) {
|
154 | 182 |
|
155 | 183 | myscriptTextWebElement.result = eventDetail.rawResult.result;
|
156 |
| - myscriptTextWebElement.resultlabel = eventDetail.rawResult.result.textSegmentResult.candidates[eventDetail.rawResult.result.textSegmentResult.selectedCandidateIdx].label; |
157 |
| - myscriptTextWebElement.candidates = eventDetail.rawResult.result.textSegmentResult.candidates; |
158 |
| - myscriptTextWebElement.candidates[eventDetail.rawResult.result.textSegmentResult.selectedCandidateIdx].selected = true; // FIXME: ugly hack to mark selected candidate |
159 |
| - textUpdateCandidatesCustomLabel(myscriptTextWebElement); |
| 184 | + myscriptTextWebElement.resultlabel = myscriptTextWebElement.result.textSegmentResult.candidates[myscriptTextWebElement.result.textSegmentResult.selectedCandidateIdx].label; |
| 185 | + myscriptTextWebElement.candidates = myscriptTextWebElement.result.textSegmentResult.candidates; |
160 | 186 | }
|
161 | 187 | }
|
162 | 188 |
|
|
168 | 194 | }
|
169 | 195 | }
|
170 | 196 |
|
171 |
| - /** |
172 |
| - * Update custom label of candidates by theirs owns properties |
173 |
| - */ |
174 |
| - function textUpdateCandidatesCustomLabel(myscriptTextWebElement) { |
175 |
| - var result = myscriptTextWebElement.result; |
176 |
| - result.textSegmentResult.candidates.forEach(function (candidate) { |
177 |
| - var textClasses = ['text', 'myscript-text-web']; |
178 |
| - var textFlags = myscriptTextWebElement._getFlagsClasses(candidate); |
179 |
| - if (textFlags) { |
180 |
| - textClasses.push(textFlags); |
181 |
| - } |
182 |
| - var textLabel = '<span class="' + textClasses.join(' ') + '">'; |
183 |
| - if (result.wordSegments && result.wordSegments.length > -1) { |
184 |
| - candidate.children.forEach(function(textChild) { |
185 |
| - var wordCandidateIndex = textChild.selectedCandidateIdx; |
186 |
| - var wordInkRanges = textChild.inkRanges; |
187 |
| - result.wordSegments.forEach(function (wordSegment) { |
188 |
| - if (wordSegment.inkRanges === wordInkRanges) { |
189 |
| - var wordCandidate = wordSegment.candidates[wordCandidateIndex]; |
190 |
| - if (wordCandidate) { |
191 |
| - var wordClasses = ['word', 'myscript-text-web']; |
192 |
| - var wordFlags = myscriptTextWebElement._getFlagsClasses(candidate); |
193 |
| - if (wordFlags) { |
194 |
| - wordClasses.push(wordFlags); |
195 |
| - } |
196 |
| - var wordLabel = '<span class="' + wordClasses.join(' ') + '">'; |
197 |
| - if (!result.charSegments || result.charSegments.length <= 0) { |
198 |
| - wordLabel += wordCandidate.label; |
199 |
| - } else { |
200 |
| - wordCandidate.children.forEach(function(charSegmentItem, index) { |
201 |
| - var charClasses = ['char', 'myscript-text-web']; |
202 |
| - var charFlags = myscriptTextWebElement._getFlagsClasses(candidate); |
203 |
| -// if (charFlags) { |
204 |
| -// charClasses.push(charFlags); |
205 |
| -// } |
206 |
| - var charLabel = '<span class="' + charClasses.join(' ') + '">'; |
207 |
| - if (!(charSegmentItem.inkRanges && charSegmentItem.inkRanges.length > 0)) { |
208 |
| - if (wordCandidate.flags.indexOf('PREDICTED') > -1) { |
209 |
| - charLabel = '<span class="char predicted myscript-text-web">'; |
210 |
| - } else if (wordCandidate.flags.indexOf('COMPLETED') > -1) { |
211 |
| - charLabel = '<span class="char completed myscript-text-web">'; |
212 |
| - } |
213 |
| - } |
214 |
| - charLabel += wordCandidate.label.charAt(index); |
215 |
| - charLabel += '</span>'; |
216 |
| - wordLabel += charLabel; |
217 |
| - }); |
218 |
| - } |
219 |
| - wordLabel += '</span>'; |
220 |
| - textLabel += wordLabel; |
221 |
| - } |
222 |
| - } |
223 |
| - }); |
224 |
| - }); |
225 |
| - } |
226 |
| - candidate.customlabel = textLabel + '</span>'; |
227 |
| - }); |
228 |
| - } |
229 |
| - |
230 | 197 | function textAttachElementToDom(myscriptTextWebElement) {
|
231 | 198 | if (myscriptTextWebElement.unloaded !== true && myscriptTextWebElement.connected === true) {
|
232 | 199 |
|
233 | 200 | myscriptTextWebElement.myscriptcommonelement = textQsWrapper(myscriptTextWebElement).querySelector('myscript-common-element');
|
234 |
| - //myscriptTextWebElement.myscriptcommonelement.unloaded = false; |
235 | 201 | myscriptTextWebElement.myscriptcommonelement.options = { recognitionParams: { textParameter: textBuildOptionsFromTextParam(myscriptTextWebElement) } };
|
236 | 202 | myscriptTextWebElement.myscriptcommonelement.removeAttribute('unloaded');
|
237 |
| - myscriptTextWebElement.hideresultChanged(myscriptTextWebElement.hideresult); |
238 | 203 |
|
239 | 204 | myscriptTextWebElement.myscriptcommonelement.addEventListener('myscript-common-element-change', function (event) {
|
240 | 205 | LOG("text change intercepted", event);
|
|
495 | 460 | */
|
496 | 461 | hideresult: {
|
497 | 462 | type: Boolean,
|
498 |
| - value: false, |
499 |
| - observer: 'hideresultChanged' |
| 463 | + value: false |
500 | 464 | },
|
501 | 465 | /**
|
502 | 466 | * The recognition language used by the recognition process.
|
|
521 | 485 | */
|
522 | 486 | resultdetail: {
|
523 | 487 | type: String,
|
524 |
| - value: 'CHARACTER' |
| 488 | + value: 'WORD' |
525 | 489 | },
|
526 | 490 | /**
|
527 | 491 | * The content types to use for the recognition.
|
|
548 | 512 | value: []
|
549 | 513 | },
|
550 | 514 | /**
|
551 |
| - * List of user resources to use for recognitions. Thoses user ressources have to be attached to the user account where application is declare. |
| 515 | + * List of user resources to use for recognitions. Theses user resources have to be attached to the user account where application is declare. |
552 | 516 | * @type {Array}
|
553 | 517 | */
|
554 | 518 | userresources: {
|
|
561 | 525 | */
|
562 | 526 | textcandidatelistsize: {
|
563 | 527 | type: Number,
|
564 |
| - value: 3 |
| 528 | + value: 1 |
565 | 529 | },
|
566 | 530 | /**
|
567 | 531 | * The size of the candidate lists that will be provided at the word level in the recognition result.
|
|
577 | 541 | */
|
578 | 542 | wordpredictionlistsize: {
|
579 | 543 | type: Number,
|
580 |
| - value: 1 |
| 544 | + value: 0 |
581 | 545 | },
|
582 | 546 | /**
|
583 | 547 | * The size of the candidate completion lists that will be provided at the word level in the recognition result.
|
584 | 548 | * This value must be between `0` and `20`.
|
585 | 549 | */
|
586 | 550 | wordcompletionlistsize: {
|
587 | 551 | type: Number,
|
588 |
| - value: 1 |
| 552 | + value: 0 |
589 | 553 | },
|
590 | 554 | /**
|
591 | 555 | * The size of the candidate lists that will be provided at the character level in the recognition result.
|
|
720 | 684 | //-----------------------------------------------------------------------
|
721 | 685 | //--------- Wrapping END ------------
|
722 | 686 | //-----------------------------------------------------------------------
|
723 |
| - /** |
724 |
| - * |
725 |
| - */ |
726 |
| - hideresultChanged: function (hideresult) { |
727 |
| - if (this.myscriptcommonelement) { |
728 |
| - if (hideresult) { |
729 |
| - this.myscriptcommonelement.classList.remove('result'); |
730 |
| - } else { |
731 |
| - this.myscriptcommonelement.classList.add('result'); |
732 |
| - } |
733 |
| - } |
734 |
| - }, |
735 | 687 | unloadedChanged: function (newUnloadValue) {
|
736 | 688 | LOG('text unloaded changed', newUnloadValue);
|
737 | 689 | this.unloaded = newUnloadValue;
|
|
765 | 717 | this.result = {};
|
766 | 718 | this.result = result;
|
767 | 719 | },
|
768 |
| - /** |
769 |
| - * Determine if the candidate is selected |
770 |
| - * @param candidate |
771 |
| - * @param result |
772 |
| - * @returns {boolean} |
773 |
| - * @private |
774 |
| - */ |
775 |
| - _isSelected: function (candidate, result) { |
776 |
| - if (result && result.textSegmentResult) { |
777 |
| - return (candidate === result.textSegmentResult.candidates[result.textSegmentResult.selectedCandidateIdx]); |
778 |
| - } |
779 |
| - return false; |
| 720 | + _isSelected: function (index, selectedIndex) { |
| 721 | + return index === selectedIndex; |
780 | 722 | },
|
781 |
| - _getWordCandidates: function (candidate, result) { |
782 |
| - var wordCandidates = undefined; |
783 |
| - if (result.wordSegments && result.wordSegments.length > -1) { |
784 |
| - wordCandidates = []; |
785 |
| - candidate.children.forEach(function (textChild) { |
786 |
| - var wordCandidateIndex = textChild.selectedCandidateIdx; |
787 |
| - var wordInkRanges = textChild.inkRanges; |
788 |
| - result.wordSegments.forEach(function (wordSegment) { |
789 |
| - if (wordSegment.inkRanges === wordInkRanges) { |
790 |
| - wordCandidates.push(wordSegment.candidates[wordCandidateIndex]) |
| 723 | + _getChildSegments: function (candidate, result, type) { |
| 724 | + var segments = []; |
| 725 | + |
| 726 | + function addSegment(child, segment) { |
| 727 | + if (segment.inkRanges === child.inkRanges) { |
| 728 | + segment.selectedCandidateIdx = child.selectedCandidateIdx; |
| 729 | + segments.push(segment); |
| 730 | + } |
| 731 | + } |
| 732 | + |
| 733 | + if (candidate.children) { |
| 734 | + candidate.children.forEach(function (child, index) { |
| 735 | + if (type === 'text' && result.wordSegments && result.wordSegments.length > -1) { |
| 736 | + result.wordSegments.forEach(function (segment) { |
| 737 | + addSegment(child, segment); |
| 738 | + }); |
| 739 | + } else if (type === 'word' && result.charSegments && result.charSegments.length > -1) { |
| 740 | + if (!child.inkRanges) { |
| 741 | + var seg = { |
| 742 | + candidates: [{ |
| 743 | + label: candidate.label.charAt(index), |
| 744 | + flags: candidate.flags |
| 745 | + }], |
| 746 | + selectedCandidateIdx: 0 |
| 747 | + }; |
| 748 | + segments.push(seg); |
| 749 | + } else { |
| 750 | + result.charSegments.forEach(function (segment) { |
| 751 | + addSegment(child, segment); |
| 752 | + }); |
791 | 753 | }
|
792 |
| - }); |
| 754 | + } |
793 | 755 | });
|
794 | 756 | }
|
795 |
| - return wordCandidates; |
| 757 | + return segments; |
796 | 758 | },
|
797 |
| - _getCharCandidates: function (candidate, result) { |
798 |
| - var charCandidates = undefined; |
799 |
| - if (result.wordSegments && result.wordSegments.length > -1) { |
800 |
| - charCandidates = []; |
801 |
| - candidate.children.forEach(function (wordChild) { |
802 |
| - var charCandidateIndex = wordChild.selectedCandidateIdx; |
803 |
| - var charInkRanges = wordChild.inkRanges; |
804 |
| - result.charSegments.forEach(function (charSegment) { |
805 |
| - if (charSegment.inkRanges === charInkRanges) { |
806 |
| - charCandidates.push(charSegment.candidates[charCandidateIndex]) |
807 |
| - } |
808 |
| - }); |
| 759 | + _getChildCandidates: function (candidate, result, type) { |
| 760 | + var candidates = []; |
| 761 | + this._getChildSegments(candidate, result, type) |
| 762 | + .forEach(function (segment) { |
| 763 | + candidates.push(segment.candidates[segment.selectedCandidateIdx]); |
809 | 764 | });
|
810 |
| - } |
811 |
| - return charCandidates; |
| 765 | + return candidates; |
812 | 766 | },
|
813 |
| - _getFlagsClasses: function (candidate) { |
| 767 | + _getCandidateFlags: function (candidate) { |
814 | 768 | if (candidate.flags) {
|
815 | 769 | return candidate.flags.join(' ').toLowerCase();
|
816 | 770 | }
|
817 |
| - return false; |
| 771 | + return ''; |
818 | 772 | }
|
819 | 773 | })
|
820 | 774 | ;
|
|
0 commit comments