Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit ffc9daf

Browse files
hlomzikMikhail Maluyk
andauthored
base64unicode encoding (#95)
Co-authored-by: Mikhail Maluyk <[email protected]>
1 parent 1028ba8 commit ffc9daf

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

src/tags/object/HyperText.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { Component } from "react";
33
import { observer, inject } from "mobx-react";
44
import { types, getType, getRoot } from "mobx-state-tree";
55

6+
import Utils from "../../utils";
67
import ObjectBase from "./Base";
78
import ObjectTag from "../../components/Tags/Object";
89
import RegionsMixin from "../../mixins/Regions";
@@ -22,7 +23,7 @@ import InfoModal from "../../components/Infomodal/Infomodal";
2223
* @param {string} name - name of the element
2324
* @param {string} value - value of the element
2425
* @param {boolean} [showLabels=false] - show labels next to the region
25-
* @param {string} [encoding=string|base64] - provide the html as an escaped string or base64 encoded string
26+
* @param {string} [encoding=none|base64|base64unicode] - decode value from encoded string
2627
*/
2728
const TagAttrs = types.model("HyperTextModel", {
2829
name: types.maybeNull(types.string),
@@ -31,7 +32,7 @@ const TagAttrs = types.model("HyperTextModel", {
3132
highlightcolor: types.maybeNull(types.string),
3233
showlabels: types.optional(types.boolean, false),
3334

34-
encoding: types.optional(types.string, "string"),
35+
encoding: types.optional(types.enumeration(["none", "base64", "base64unicode"]), "none"),
3536
});
3637

3738
const Model = types
@@ -134,13 +135,7 @@ const Model = types
134135
},
135136
}));
136137

137-
const HyperTextModel = types.compose(
138-
"HyperTextModel",
139-
RegionsMixin,
140-
TagAttrs,
141-
Model,
142-
ObjectBase,
143-
);
138+
const HyperTextModel = types.compose("HyperTextModel", RegionsMixin, TagAttrs, Model, ObjectBase);
144139

145140
class HtxHyperTextView extends Component {
146141
render() {
@@ -253,6 +248,7 @@ class HyperTextPieceView extends Component {
253248

254249
let val = runTemplate(item.value, store.task.dataObj);
255250
if (item.encoding === "base64") val = atob(val);
251+
if (item.encoding === "base64unicode") val = Utils.Checkers.atobUnicode(val);
256252

257253
return (
258254
<ObjectTag item={item}>

src/tags/object/Text.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import InfoModal from "../../components/Infomodal/Infomodal";
2727
* @param {string} [highlightColor] - hex string with highlight color, if not provided uses the labels color
2828
* @param {symbol|word} [granularity=symbol] - control per symbol or word selection
2929
* @param {boolean} [showLabels=true] - show labels next to the region
30-
* @param {string} [encoding=string|base64] - decode value from a plain or base64 encoded string
30+
* @param {string} [encoding=none|base64|base64unicode] - decode value from encoded string
3131
*/
3232
const TagAttrs = types.model("TextModel", {
3333
name: types.maybeNull(types.string),
@@ -46,7 +46,8 @@ const TagAttrs = types.model("TextModel", {
4646
showlabels: types.optional(types.boolean, true),
4747

4848
granularity: types.optional(types.enumeration(["symbol", "word", "sentence", "paragraph"]), "symbol"),
49-
encoding: types.optional(types.string, "string"),
49+
50+
encoding: types.optional(types.enumeration(["none", "base64", "base64unicode"]), "none"),
5051
});
5152

5253
const Model = types
@@ -114,6 +115,8 @@ const Model = types
114115
loadedValue(val) {
115116
self.loaded = true;
116117
if (self.encoding === "base64") val = atob(val);
118+
if (self.encoding === "base64unicode") val = Utils.Checkers.atobUnicode(val);
119+
117120
self._value = val;
118121

119122
self._regionsCache.forEach(({ region, completion }) => {
@@ -219,13 +222,7 @@ const Model = types
219222
},
220223
}));
221224

222-
const TextModel = types.compose(
223-
"TextModel",
224-
RegionsMixin,
225-
TagAttrs,
226-
Model,
227-
ObjectBase,
228-
);
225+
const TextModel = types.compose("TextModel", RegionsMixin, TagAttrs, Model, ObjectBase);
229226

230227
class HtxTextView extends Component {
231228
render() {
@@ -248,6 +245,7 @@ class TextPieceView extends Component {
248245

249246
let val = runTemplate(item.value, store.task.dataObj);
250247
if (item.encoding === "base64") val = atob(val);
248+
if (item.encoding === "base64unicode") val = Utils.Checkers.atobUnicode(val);
251249

252250
return val;
253251
}

src/utils/utilities.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ export function hashCode(str) {
8282
}
8383
return hash + "";
8484
}
85+
86+
export function atobUnicode(str) {
87+
// Going backwards: from bytestream, to percent-encoding, to original string.
88+
return decodeURIComponent(
89+
atob(str)
90+
.split("")
91+
.map(function(c) {
92+
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
93+
})
94+
.join(""),
95+
);
96+
}

0 commit comments

Comments
 (0)