Skip to content
This repository was archived by the owner on Jan 4, 2021. It is now read-only.

Commit b14c731

Browse files
author
Francois-Xavier Gentilhomme
committed
[DEV] Update with last myscript-js version
1 parent 9ebfb65 commit b14c731

File tree

3 files changed

+79
-43
lines changed

3 files changed

+79
-43
lines changed

demo/programmatic-init.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
myscriptTextWebElement.setAttribute('applicationkey', mycustomconfiguration.applicationKey);
3535
myscriptTextWebElement.hmackey = mycustomconfiguration.hmacKey;
3636
// Testing if options are taken into account
37-
myscriptTextWebElement.options = {
37+
myscriptTextWebElement.configuration = {
38+
timestampFloatPrecision: 2,
3839
recognitionParams: {
39-
textParameter: {
40-
language: 'fr_FR'
41-
},
42-
timestampFloatPrecision: 2
40+
v3: {
41+
textParameter: {
42+
language: 'fr_FR'
43+
}
44+
}
4345
}
4446
};
4547
myscriptTextWebElement.language = mycustomconfiguration.language;

demo/stats.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<script>
2929
document.querySelector('myscript-text-web').addEventListener('myscript-text-web-change', function (event) {
3030
const paperToast = document.querySelector('#result');
31-
paperToast.text = 'Stats for this imput are : ' + JSON.stringify(document.querySelector('myscript-text-web').getStats());
31+
paperToast.text = 'Stats for this input are : ' + JSON.stringify(document.querySelector('myscript-text-web').getStats());
3232
paperToast.fitIn = paperToast.show();
3333
});
3434
</script>

myscript-text-web.html

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,26 @@
7171
</style>
7272
<template>
7373
<div class="resultField" hidden="[[ displayResultZone(hideresult, unloaded) ]]">
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">
74+
<template is="dom-repeat" id="textCandidateList" items="[[ candidates ]]" as="textCandidate"
75+
index-as="textIndex">
76+
<span class$="text {{ _getCandidateFlags(candidate) }}"
77+
selected$="[[ _isSelected(textIndex, result.textSegmentResult.selectedCandidateIdx) ]]"
78+
on-tap="_select">
7679
<template is="dom-if" if="[[ !textCandidate.children ]]">
7780
{{ textCandidate.label }}
7881
</template>
7982
<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">
83+
<template is="dom-repeat" id="wordCandidateList"
84+
items="[[ _getChildCandidates(textCandidate, result, 'text') ]]" as="wordCandidate">
8185
<span class$="word {{ _getCandidateFlags(wordCandidate) }}">
8286
<template is="dom-if" if="[[ !wordCandidate.children ]]">
8387
{{ wordCandidate.label }}
8488
</template>
8589
<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>
90+
<template is="dom-repeat" id="charCandidateList"
91+
items="[[ _getChildCandidates(wordCandidate, result, 'word') ]]"
92+
as="charCandidate"><span
93+
class$="char {{ _getCandidateFlags(charCandidate) }}">{{ charCandidate.label }}</span></template>
8794
</template>
8895
</span>
8996
</template>
@@ -96,15 +103,15 @@
96103
applicationkey="{{ applicationkey }}"
97104
hidebuttons="{{ hidebuttons }}"
98105
hmackey="{{ hmackey }}"
99-
options="[[ options ]]"
106+
configuration="[[ configuration ]]"
100107
protocol="{{ protocol }}"
101108
host="{{ host }}"
102109
recognitiontriggerdelay="{{recognitiontriggerdelay}}"
103110
recognitionprocessdelay="{{recognitionprocessdelay}}"
104111
strokecolor="{{ strokecolor }}"
105112
strokewidth="{{ strokewidth }}"
106113
timeout="{{ timeout }}"
107-
unloaded
114+
unloaded="{{ unloaded }}"
108115
type="TEXT"
109116
disableclear="{{ disableclear }}"
110117
disablerecognize="{{ disablerecognize }}"
@@ -136,7 +143,7 @@
136143
*
137144
* @param myscriptTextWebElement
138145
*/
139-
function textBuildOptionsFromTextParam(myscriptTextWebElement) {
146+
function textBuildRecognitionParamsFromTextParam(myscriptTextWebElement) {
140147
const textParameter = {};
141148
textParameter.language = myscriptTextWebElement.language;
142149
textParameter.textInputMode = myscriptTextWebElement.textinputmode;
@@ -158,7 +165,9 @@
158165
textParameter.textProperties.enableTagger = myscriptTextWebElement.enabletagger;
159166
textParameter.textProperties.spellingDistortion = myscriptTextWebElement.spellingdistortion;
160167

161-
return textParameter;
168+
const recognitionParams = {};
169+
recognitionParams[myscriptTextWebElement.apiversion.toLowerCase()] = textParameter;
170+
return recognitionParams;
162171
}
163172

164173
/**
@@ -172,8 +181,8 @@
172181
myscriptTextWebElement.resultlabel = '';
173182
myscriptTextWebElement.candidates = [];
174183

175-
if (eventDetail.recognitionResult && eventDetail.recognitionResult['text/plain']) {
176-
myscriptTextWebElement.resultlabel = eventDetail.recognitionResult['text/plain'];
184+
if (eventDetail.recognitionResult && eventDetail.recognitionResult.TEXT) {
185+
myscriptTextWebElement.resultlabel = eventDetail.recognitionResult.TEXT;
177186
}
178187
if (eventDetail.rawResult &&
179188
eventDetail.rawResult.result &&
@@ -195,31 +204,56 @@
195204
}
196205

197206
function textAttachElementToDom(myscriptTextWebElement) {
198-
if (myscriptTextWebElement.unloaded !== true && myscriptTextWebElement.connected === true) {
207+
myscriptTextWebElement.myscriptcommonelement = textQsWrapper(myscriptTextWebElement).querySelector('myscript-common-element');
208+
myscriptTextWebElement.myscriptcommonelement.configuration = {
209+
recognitionParams: textBuildRecognitionParamsFromTextParam(myscriptTextWebElement)
210+
};
199211

200-
myscriptTextWebElement.myscriptcommonelement = textQsWrapper(myscriptTextWebElement).querySelector('myscript-common-element');
201-
myscriptTextWebElement.myscriptcommonelement.options = { recognitionParams: { textParameter: textBuildOptionsFromTextParam(myscriptTextWebElement) } };
202-
myscriptTextWebElement.myscriptcommonelement.removeAttribute('unloaded');
212+
// Manage clear events
213+
function clearListener(event) {
214+
LOG('text ' + event.type + ' event intercepted', event);
215+
textManageResultEvent(myscriptTextWebElement, event.detail);
216+
myscriptTextWebElement.dispatchEvent(new CustomEvent('myscript-text-web-' + event.type, { detail: event.detail }));
217+
}
203218

204-
myscriptTextWebElement.myscriptcommonelement.addEventListener('myscript-common-element-change', function (event) {
205-
LOG("text change intercepted", event);
206-
myscriptTextWebElement.dispatchEvent(new CustomEvent('myscript-text-web-change', { detail: event.detail }));
207-
});
208-
myscriptTextWebElement.myscriptcommonelement.addEventListener('myscript-common-element-error', function (event) {
209-
LOG("text error intercepted", event);
210-
myscriptTextWebElement.dispatchEvent(new CustomEvent('myscript-text-web-error', { detail: event.detail }));
211-
});
212-
myscriptTextWebElement.myscriptcommonelement.addEventListener('myscript-common-element-result', function (event) {
213-
LOG("text result intercepted", event);
214-
textManageResultEvent(myscriptTextWebElement, event.detail);
215-
myscriptTextWebElement.dispatchEvent(new CustomEvent('myscript-text-web-result', { detail: { result: myscriptTextWebElement.result, srcDetail: event.detail } }));
216-
});
217-
myscriptTextWebElement.myscriptcommonelement.addEventListener('myscript-common-element-clear', function (event) {
218-
LOG("text clear intercepted", event);
219-
textManageClearEvent(myscriptTextWebElement, event.detail);
220-
myscriptTextWebElement.dispatchEvent(new CustomEvent('myscript-text-web-clear', { detail: event.detail }));
221-
});
219+
// Manage result events
220+
function resultListener(event) {
221+
LOG('text ' + event.type + ' event intercepted', event);
222+
// Just forwarding event there.
223+
textManageResultEvent(myscriptTextWebElement, event.detail);
224+
var detail = event.detail;
225+
detail.result = myscriptTextWebElement.result;
226+
myscriptTextWebElement.dispatchEvent(new CustomEvent('myscript-text-web-' + event.type, { detail: detail }));
227+
}
228+
229+
// Manage other events
230+
function defaultListener(event) {
231+
LOG('text ' + event.type + ' event intercepted', event);
232+
// Just forwarding event there.
233+
myscriptTextWebElement.dispatchEvent(new CustomEvent('myscript-text-web-' + event.type, { detail: event.detail }));
222234
}
235+
236+
myscriptTextWebElement.currentListeners = {};
237+
['myscript-common-element-result'].forEach(function (type) {
238+
myscriptTextWebElement.currentListeners[type] = resultListener;
239+
});
240+
['myscript-common-element-clear'].forEach(function (type) {
241+
myscriptTextWebElement.currentListeners[type] = clearListener;
242+
});
243+
['myscript-common-element-undo', 'myscript-common-element-redo', 'myscript-common-element-typeset', 'myscript-common-element-recognize', 'myscript-common-element-change', 'myscript-common-element-error']
244+
.forEach(function (type) {
245+
myscriptTextWebElement.currentListeners[type] = defaultListener;
246+
});
247+
Object.keys(myscriptTextWebElement.currentListeners).forEach(function (type) {
248+
myscriptTextWebElement.myscriptcommonelement.addEventListener(type, myscriptTextWebElement.currentListeners[type])
249+
});
250+
}
251+
252+
/** Remove listeners when common element is unloaded. */
253+
function textDetachElementFromDom(myscriptTextWebElement) {
254+
Object.keys(myscriptTextWebElement.currentListeners).forEach(function (type) {
255+
myscriptTextWebElement.myscriptcommonelement.removeEventListener(type, myscriptTextWebElement.currentListeners[type])
256+
});
223257
}
224258

225259
Polymer({
@@ -346,11 +380,11 @@
346380
observer: 'unloadedChanged'
347381
},
348382
/**
349-
* Set the additional options used to feed MyScript ink Paper (Structure of object as defined in MyScriptjs MyScriptJSOptions.js file)
383+
* Set the additional configuration used to feed MyScript ink Paper (Structure of object as defined in MyScriptjs MyScriptJSOptions.js file)
350384
* Options values are taken into account when myscript-common-element is attach to the dom and when detached is set to false.
351-
* Options values are not reflected to myscript-common-element attributes plus attributes values are always taken into account before options values (see demo/programmatic-init.html for a better understanding).
385+
* Options values are not reflected to myscript-common-element attributes plus attributes values are always taken into account before configuration values (see demo/programmatic-init.html for a better understanding).
352386
*/
353-
options: {
387+
configuration: {
354388
type: Object,
355389
notify: true
356390
},
@@ -524,8 +558,8 @@
524558
* This value must be between `1` and `20`.
525559
*/
526560
textcandidatelistsize: {
527-
type: Number,
528-
value: 1
561+
type: Number,
562+
value: 1
529563
},
530564
/**
531565
* The size of the candidate lists that will be provided at the word level in the recognition result.

0 commit comments

Comments
 (0)