Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/tags/object/HyperText.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { Component } from "react";
import { observer, inject } from "mobx-react";
import { types, getType, getRoot } from "mobx-state-tree";

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

encoding: types.optional(types.string, "string"),
encoding: types.optional(types.enumeration(["none", "base64", "base64unicode"]), "none"),
});

const Model = types
Expand Down Expand Up @@ -134,13 +135,7 @@ const Model = types
},
}));

const HyperTextModel = types.compose(
"HyperTextModel",
RegionsMixin,
TagAttrs,
Model,
ObjectBase,
);
const HyperTextModel = types.compose("HyperTextModel", RegionsMixin, TagAttrs, Model, ObjectBase);

class HtxHyperTextView extends Component {
render() {
Expand Down Expand Up @@ -253,6 +248,7 @@ class HyperTextPieceView extends Component {

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

return (
<ObjectTag item={item}>
Expand Down
16 changes: 7 additions & 9 deletions src/tags/object/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import InfoModal from "../../components/Infomodal/Infomodal";
* @param {string} [highlightColor] - hex string with highlight color, if not provided uses the labels color
* @param {symbol|word} [granularity=symbol] - control per symbol or word selection
* @param {boolean} [showLabels=true] - show labels next to the region
* @param {string} [encoding=string|base64] - decode value from a plain or base64 encoded string
* @param {string} [encoding=none|base64|base64unicode] - decode value from encoded string
*/
const TagAttrs = types.model("TextModel", {
name: types.maybeNull(types.string),
Expand All @@ -46,7 +46,8 @@ const TagAttrs = types.model("TextModel", {
showlabels: types.optional(types.boolean, true),

granularity: types.optional(types.enumeration(["symbol", "word", "sentence", "paragraph"]), "symbol"),
encoding: types.optional(types.string, "string"),

encoding: types.optional(types.enumeration(["none", "base64", "base64unicode"]), "none"),
});

const Model = types
Expand Down Expand Up @@ -114,6 +115,8 @@ const Model = types
loadedValue(val) {
self.loaded = true;
if (self.encoding === "base64") val = atob(val);
if (self.encoding === "base64unicode") val = Utils.Checkers.atobUnicode(val);

self._value = val;

self._regionsCache.forEach(({ region, completion }) => {
Expand Down Expand Up @@ -219,13 +222,7 @@ const Model = types
},
}));

const TextModel = types.compose(
"TextModel",
RegionsMixin,
TagAttrs,
Model,
ObjectBase,
);
const TextModel = types.compose("TextModel", RegionsMixin, TagAttrs, Model, ObjectBase);

class HtxTextView extends Component {
render() {
Expand All @@ -248,6 +245,7 @@ class TextPieceView extends Component {

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

return val;
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ export function hashCode(str) {
}
return hash + "";
}

export function atobUnicode(str) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(
atob(str)
.split("")
.map(function(c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join(""),
);
}