@pie-lib/editable-html 7.22.4 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,271 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = CharactersPlugin;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _reactDom = _interopRequireDefault(require("react-dom"));
11
+
12
+ var _debug = _interopRequireDefault(require("debug"));
13
+
14
+ var _get = _interopRequireDefault(require("lodash/get"));
15
+
16
+ var _mathToolbar = require("@pie-lib/math-toolbar");
17
+
18
+ var _customPopover = _interopRequireDefault(require("./custom-popover"));
19
+
20
+ var _utils = require("../respArea/utils");
21
+
22
+ var _utils2 = require("./utils");
23
+
24
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
29
+
30
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
31
+
32
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
33
+
34
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
35
+
36
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
37
+
38
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
39
+
40
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
41
+
42
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
43
+
44
+ var log = (0, _debug["default"])('@pie-lib:editable-html:plugins:characters');
45
+
46
+ var removeDialogs = function removeDialogs() {
47
+ var prevDialogs = document.querySelectorAll('.insert-character-dialog');
48
+ log('[characters:removeDialogs]');
49
+ prevDialogs.forEach(function (s) {
50
+ return s.remove();
51
+ });
52
+ };
53
+
54
+ var removePopOvers = function removePopOvers() {
55
+ var prevPopOvers = document.querySelectorAll('#mouse-over-popover');
56
+ log('[characters:removePopOvers]');
57
+ prevPopOvers.forEach(function (s) {
58
+ return s.remove();
59
+ });
60
+ };
61
+
62
+ var insertDialog = function insertDialog(_ref) {
63
+ var value = _ref.value,
64
+ callback = _ref.callback,
65
+ opts = _ref.opts;
66
+ var newEl = document.createElement('div');
67
+ var initialBodyOverflow = document.body.style.overflow;
68
+ log('[characters:insertDialog]');
69
+ removeDialogs();
70
+ newEl.className = 'insert-character-dialog';
71
+ document.body.style.overflow = 'hidden';
72
+ var configToUse;
73
+
74
+ switch (true) {
75
+ case opts.language === 'spanish':
76
+ configToUse = _utils2.spanishConfig;
77
+ break;
78
+
79
+ case opts.language === 'special':
80
+ configToUse = _utils2.specialConfig;
81
+ break;
82
+
83
+ default:
84
+ configToUse = opts;
85
+ }
86
+
87
+ if (!configToUse.characters) {
88
+ (0, _utils.insertSnackBar)('No characters provided or language not recognized');
89
+ return;
90
+ }
91
+
92
+ var layoutForCharacters = configToUse.characters.reduce(function (obj, arr) {
93
+ if (arr.length >= obj.columns) {
94
+ obj.columns = arr.length;
95
+ }
96
+
97
+ return obj;
98
+ }, {
99
+ rows: configToUse.characters.length,
100
+ columns: 0
101
+ });
102
+ var popoverEl;
103
+
104
+ var closePopOver = function closePopOver() {
105
+ if (popoverEl) {
106
+ popoverEl.remove();
107
+ }
108
+
109
+ removePopOvers();
110
+ };
111
+
112
+ var renderPopOver = function renderPopOver(event, el) {
113
+ if (!event) {
114
+ return;
115
+ }
116
+
117
+ var infoStyle = {
118
+ fontSize: '20px',
119
+ lineHeight: '20px'
120
+ };
121
+ closePopOver();
122
+ popoverEl = document.createElement('div');
123
+
124
+ _reactDom["default"].render( /*#__PURE__*/_react["default"].createElement(_customPopover["default"], {
125
+ onClose: closePopOver,
126
+ anchorEl: event.currentTarget
127
+ }, /*#__PURE__*/_react["default"].createElement("div", null, el.label), /*#__PURE__*/_react["default"].createElement("div", {
128
+ style: infoStyle
129
+ }, el.description), /*#__PURE__*/_react["default"].createElement("div", {
130
+ style: infoStyle
131
+ }, el.unicode)), popoverEl);
132
+
133
+ document.body.appendChild(newEl);
134
+ };
135
+
136
+ var handleClose = function handleClose() {
137
+ newEl.remove();
138
+ closePopOver();
139
+ document.body.style.overflow = initialBodyOverflow;
140
+ callback(undefined, true);
141
+ };
142
+
143
+ var handleChange = function handleChange(val) {
144
+ if (typeof val === 'string') {
145
+ callback(val);
146
+ }
147
+
148
+ if (configToUse.autoClose) {
149
+ handleClose();
150
+ }
151
+ };
152
+
153
+ var el = /*#__PURE__*/_react["default"].createElement(_mathToolbar.PureToolbar, {
154
+ autoFocus: true,
155
+ noDecimal: true,
156
+ hideInput: true,
157
+ noLatexHandling: true,
158
+ layoutForKeyPad: layoutForCharacters,
159
+ additionalKeys: configToUse.characters.reduce(function (arr, n) {
160
+ arr = [].concat(_toConsumableArray(arr), _toConsumableArray(n.map(function (k) {
161
+ return _objectSpread({
162
+ name: (0, _get["default"])(k, 'name') || k,
163
+ write: (0, _get["default"])(k, 'write') || k,
164
+ label: (0, _get["default"])(k, 'label') || k,
165
+ category: 'character',
166
+ extraClass: 'character'
167
+ }, configToUse.hasPreview ? {
168
+ actions: {
169
+ onMouseEnter: function onMouseEnter(ev) {
170
+ return renderPopOver(ev, k);
171
+ },
172
+ onMouseLeave: closePopOver
173
+ }
174
+ } : {});
175
+ })));
176
+ return arr;
177
+ }, []),
178
+ keypadMode: "language",
179
+ onChange: handleChange,
180
+ onDone: handleClose
181
+ });
182
+
183
+ _reactDom["default"].render(el, newEl, function () {
184
+ var cursorItem = document.querySelector("[data-key=\"".concat(value.anchorKey, "\"]"));
185
+
186
+ if (cursorItem) {
187
+ var boundRect = cursorItem.getBoundingClientRect();
188
+ document.body.appendChild(newEl);
189
+ newEl.style.position = 'fixed';
190
+ newEl.style.top = "".concat(boundRect.top - newEl.offsetHeight - 10, "px");
191
+ newEl.style.left = "".concat(boundRect.left + cursorItem.offsetWidth + 10, "px");
192
+ newEl.style.zIndex = 99999;
193
+ var firstCallMade = false;
194
+
195
+ var listener = function listener() {
196
+ // this will be triggered right after setting it because
197
+ // this toolbar is added on the mousedown event
198
+ // so right after mouseup, the click will be triggered
199
+ if (firstCallMade) {
200
+ document.body.removeEventListener('click', listener);
201
+ handleClose();
202
+ } else {
203
+ firstCallMade = true;
204
+ }
205
+ };
206
+
207
+ if (configToUse.autoClose) {
208
+ document.body.addEventListener('click', listener);
209
+ }
210
+ }
211
+ });
212
+ };
213
+
214
+ var CharacterIcon = function CharacterIcon(_ref2) {
215
+ var letter = _ref2.letter;
216
+ return /*#__PURE__*/_react["default"].createElement("div", {
217
+ style: {
218
+ fontSize: '25px',
219
+ lineHeight: '15px'
220
+ }
221
+ }, letter);
222
+ };
223
+
224
+ function CharactersPlugin(opts) {
225
+ removeDialogs();
226
+ return {
227
+ name: 'math',
228
+ toolbar: {
229
+ icon: /*#__PURE__*/_react["default"].createElement(CharacterIcon, {
230
+ letter: opts.characterIcon || _utils2.characterIcons[opts.language] || 'ñ'
231
+ }),
232
+ onClick: function onClick(value, onChange) {
233
+ var valueToUse = value;
234
+
235
+ var callback = function callback(_char, focus) {
236
+ if (_char) {
237
+ var change = valueToUse.change().insertTextByKey(valueToUse.anchorKey, valueToUse.anchorOffset, _char);
238
+ valueToUse = change.value;
239
+ log('[characters:insert]: ', value);
240
+ onChange(change);
241
+ }
242
+
243
+ log('[characters:click]');
244
+
245
+ if (focus) {
246
+ var editorDOM = document.querySelector("[data-key=\"".concat(valueToUse.document.key, "\"]"));
247
+
248
+ if (editorDOM) {
249
+ editorDOM.focus();
250
+ }
251
+ }
252
+ };
253
+
254
+ insertDialog({
255
+ value: valueToUse,
256
+ callback: callback,
257
+ opts: opts
258
+ });
259
+ }
260
+ },
261
+ pluginStyles: function pluginStyles(node, parentNode, p) {
262
+ if (p) {
263
+ return {
264
+ position: 'absolute',
265
+ top: 'initial'
266
+ };
267
+ }
268
+ }
269
+ };
270
+ }
271
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/plugins/characters/index.jsx"],"names":["log","removeDialogs","prevDialogs","document","querySelectorAll","forEach","s","remove","removePopOvers","prevPopOvers","insertDialog","value","callback","opts","newEl","createElement","initialBodyOverflow","body","style","overflow","className","configToUse","language","spanishConfig","specialConfig","characters","layoutForCharacters","reduce","obj","arr","length","columns","rows","popoverEl","closePopOver","renderPopOver","event","el","infoStyle","fontSize","lineHeight","ReactDOM","render","currentTarget","label","description","unicode","appendChild","handleClose","undefined","handleChange","val","autoClose","n","map","k","name","write","category","extraClass","hasPreview","actions","onMouseEnter","ev","onMouseLeave","cursorItem","querySelector","anchorKey","boundRect","getBoundingClientRect","position","top","offsetHeight","left","offsetWidth","zIndex","firstCallMade","listener","removeEventListener","addEventListener","CharacterIcon","letter","CharactersPlugin","toolbar","icon","characterIcon","characterIcons","onClick","onChange","valueToUse","char","focus","change","insertTextByKey","anchorOffset","editorDOM","key","pluginStyles","node","parentNode","p"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AAEA;;AAEA;;AACA;;AACA;;;;;;;;;;;;;;;;;;;;;;AACA,IAAMA,GAAG,GAAG,uBAAM,2CAAN,CAAZ;;AAEA,IAAMC,aAAa,GAAG,SAAhBA,aAAgB,GAAM;AAC1B,MAAMC,WAAW,GAAGC,QAAQ,CAACC,gBAAT,CAA0B,0BAA1B,CAApB;AAEAJ,EAAAA,GAAG,CAAC,4BAAD,CAAH;AACAE,EAAAA,WAAW,CAACG,OAAZ,CAAoB,UAAAC,CAAC;AAAA,WAAIA,CAAC,CAACC,MAAF,EAAJ;AAAA,GAArB;AACD,CALD;;AAOA,IAAMC,cAAc,GAAG,SAAjBA,cAAiB,GAAM;AAC3B,MAAMC,YAAY,GAAGN,QAAQ,CAACC,gBAAT,CAA0B,qBAA1B,CAArB;AAEAJ,EAAAA,GAAG,CAAC,6BAAD,CAAH;AACAS,EAAAA,YAAY,CAACJ,OAAb,CAAqB,UAAAC,CAAC;AAAA,WAAIA,CAAC,CAACC,MAAF,EAAJ;AAAA,GAAtB;AACD,CALD;;AAOA,IAAMG,YAAY,GAAG,SAAfA,YAAe,OAA+B;AAAA,MAA5BC,KAA4B,QAA5BA,KAA4B;AAAA,MAArBC,QAAqB,QAArBA,QAAqB;AAAA,MAAXC,IAAW,QAAXA,IAAW;AAClD,MAAMC,KAAK,GAAGX,QAAQ,CAACY,aAAT,CAAuB,KAAvB,CAAd;AACA,MAAMC,mBAAmB,GAAGb,QAAQ,CAACc,IAAT,CAAcC,KAAd,CAAoBC,QAAhD;AAEAnB,EAAAA,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AAR,EAAqB;AAC5B,YAAIC,UAAU,GAAG9E,KAAjB;;AACA,YAAMC,QAAQ,GAAG,SAAXA,QAAW,CAAC8E,KAAD,EAAOC,KAAP,EAAiB;AAChC,cAAID,KAAJ,EAAU;AACR,gBAAME,MAAM,GAAGH,UAAU,CACtBG,MADY,GAEZC,eAFY,CAEIJ,UAAU,CAACtB,SAFf,EAE0BsB,UAAU,CAACK,YAFrC,EAEmDJ,KAFnD,CAAf;AAIAD,YAAAA,UAAU,GAAGG,MAAM,CAACjF,KAApB;AACAX,YAAAA,GAAG,CAAC,uBAAD,EAA0BW,KAA1B,CAAH;AACA6E,YAAAA,QAAQ,CAACI,MAAD,CAAR;AACD;;AAED5F,UAAAA,GAAG,CAAC,oBAAD,CAAH;;AAEA,cAAI2F,KAAJ,EAAW;AACT,gBAAMI,SAAS,GAAG5F,QAAQ,CAAC+D,aAAT,uBAAqCuB,UAAU,CAACtF,QAAX,CAAoB6F,GAAzD,SAAlB;;AAEA,gBAAID,SAAJ,EAAe;AACbA,cAAAA,SAAS,CAACJ,KAAV;AACD;AACF;AACF,SApBD;;AAsBAjF,QAAAA,YAAY,CAAC;AAAEC,UAAAA,KAAK,EAAE8E,UAAT;AAAqB7E,UAAAA,QAAQ,EAARA,QAArB;AAA+BC,UAAAA,IAAI,EAAJA;AAA/B,SAAD,CAAZ;AACD;AA3BM,KAFJ;AAgCLoF,IAAAA,YAAY,EAAE,sBAACC,IAAD,EAAOC,UAAP,EAAmBC,CAAnB,EAAyB;AACrC,UAAIA,CAAJ,EAAO;AACL,eAAO;AACL9B,UAAAA,QAAQ,EAAE,UADL;AAELC,UAAAA,GAAG,EAAE;AAFA,SAAP;AAID;AACF;AAvCI,GAAP;AAyCD","sourcesContent":["import React from 'react';\nimport ReactDOM from 'react-dom';\nimport debug from 'debug';\nimport get from 'lodash/get';\n\nimport { PureToolbar } from '@pie-lib/math-toolbar';\n\nimport CustomPopOver from './custom-popover';\nimport { insertSnackBar } from '../respArea/utils';\nimport { characterIcons, spanishConfig, specialConfig } from './utils';\nconst log = debug('@pie-lib:editable-html:plugins:characters');\n\nconst removeDialogs = () => {\n const prevDialogs = document.querySelectorAll('.insert-character-dialog');\n\n log('[characters:removeDialogs]');\n prevDialogs.forEach(s => s.remove());\n};\n\nconst removePopOvers = () => {\n const prevPopOvers = document.querySelectorAll('#mouse-over-popover');\n\n log('[characters:removePopOvers]');\n prevPopOvers.forEach(s => s.remove());\n};\n\nconst insertDialog = ({ value, callback, opts }) => {\n const newEl = document.createElement('div');\n const initialBodyOverflow = document.body.style.overflow;\n\n log('[characters:insertDialog]');\n\n removeDialogs();\n\n newEl.className = 'insert-character-dialog';\n document.body.style.overflow = 'hidden';\n\n let configToUse;\n\n switch (true) {\n case opts.language === 'spanish':\n configToUse = spanishConfig;\n break;\n case opts.language === 'special':\n configToUse = specialConfig;\n break;\n default:\n configToUse = opts;\n }\n\n if (!configToUse.characters) {\n insertSnackBar('No characters provided or language not recognized');\n return;\n }\n\n const layoutForCharacters = configToUse.characters.reduce(\n (obj, arr) => {\n if (arr.length >= obj.columns) {\n obj.columns = arr.length;\n }\n\n return obj;\n },\n { rows: configToUse.characters.length, columns: 0 }\n );\n\n let popoverEl;\n\n const closePopOver = () => {\n if (popoverEl) {\n popoverEl.remove();\n }\n\n removePopOvers();\n };\n\n const renderPopOver = (event, el) => {\n if (!event) {\n return;\n }\n\n const infoStyle = { fontSize: '20px', lineHeight: '20px' };\n\n closePopOver();\n\n popoverEl = document.createElement('div');\n ReactDOM.render(\n <CustomPopOver onClose={closePopOver} anchorEl={event.currentTarget}>\n <div>{el.label}</div>\n\n <div style={infoStyle}>{el.description}</div>\n\n <div style={infoStyle}>{el.unicode}</div>\n </CustomPopOver>,\n popoverEl\n );\n\n document.body.appendChild(newEl);\n };\n\n const handleClose = () => {\n newEl.remove();\n closePopOver();\n document.body.style.overflow = initialBodyOverflow;\n callback(undefined, true);\n };\n\n const handleChange = val => {\n if (typeof val === 'string') {\n callback(val);\n }\n\n if (configToUse.autoClose) {\n handleClose();\n }\n };\n\n const el = (\n <PureToolbar\n autoFocus\n noDecimal\n hideInput\n noLatexHandling\n layoutForKeyPad={layoutForCharacters}\n additionalKeys={configToUse.characters.reduce((arr, n) => {\n arr = [\n ...arr,\n ...n.map(k => ({\n name: get(k, 'name') || k,\n write: get(k, 'write') || k,\n label: get(k, 'label') || k,\n category: 'character',\n extraClass: 'character',\n ...(configToUse.hasPreview\n ? {\n actions: { onMouseEnter: ev => renderPopOver(ev, k), onMouseLeave: closePopOver }\n }\n : {})\n }))\n ];\n\n return arr;\n }, [])}\n keypadMode=\"language\"\n onChange={handleChange}\n onDone={handleClose}\n />\n );\n\n ReactDOM.render(el, newEl, () => {\n const cursorItem = document.querySelector(`[data-key=\"${value.anchorKey}\"]`);\n\n if (cursorItem) {\n const boundRect = cursorItem.getBoundingClientRect();\n\n document.body.appendChild(newEl);\n newEl.style.position = 'fixed';\n newEl.style.top = `${boundRect.top - newEl.offsetHeight - 10}px`;\n newEl.style.left = `${boundRect.left + cursorItem.offsetWidth + 10}px`;\n newEl.style.zIndex = 99999;\n\n let firstCallMade = false;\n\n const listener = () => {\n // this will be triggered right after setting it because\n // this toolbar is added on the mousedown event\n // so right after mouseup, the click will be triggered\n if (firstCallMade) {\n document.body.removeEventListener('click', listener);\n handleClose();\n } else {\n firstCallMade = true;\n }\n };\n\n if (configToUse.autoClose) {\n document.body.addEventListener('click', listener);\n }\n }\n });\n};\n\nconst CharacterIcon = ({ letter }) => (\n <div\n style={{\n fontSize: '25px',\n lineHeight: '15px'\n }}\n >\n {letter}\n </div>\n);\n\nexport default function CharactersPlugin(opts) {\n removeDialogs();\n return {\n name: 'math',\n toolbar: {\n icon: <CharacterIcon letter={opts.characterIcon || characterIcons[opts.language] || 'ñ'} />,\n onClick: (value, onChange) => {\n let valueToUse = value;\n const callback = (char, focus) => {\n if (char) {\n const change = valueToUse\n .change()\n .insertTextByKey(valueToUse.anchorKey, valueToUse.anchorOffset, char);\n\n valueToUse = change.value;\n log('[characters:insert]: ', value);\n onChange(change);\n }\n\n log('[characters:click]');\n\n if (focus) {\n const editorDOM = document.querySelector(`[data-key=\"${valueToUse.document.key}\"]`);\n\n if (editorDOM) {\n editorDOM.focus();\n }\n }\n };\n\n insertDialog({ value: valueToUse, callback, opts });\n }\n },\n\n pluginStyles: (node, parentNode, p) => {\n if (p) {\n return {\n position: 'absolute',\n top: 'initial'\n };\n }\n }\n };\n}\n"],"file":"index.js"}
@@ -0,0 +1,362 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.specialConfig = exports.spanishConfig = exports.characterIcons = void 0;
7
+ var spanishConfig = {
8
+ characters: [['á', 'é', 'í', 'ó', 'ú'], ['Á', 'É', 'Í', 'Ó', 'Ú'], ['—', '«', '»', 'ñ', 'ü'], ['-', '¿', '¡', 'Ñ', 'Ü']],
9
+ autoClose: true
10
+ };
11
+ exports.spanishConfig = spanishConfig;
12
+ var specialConfig = {
13
+ hasPreview: true,
14
+ characters: [[{
15
+ unicode: 'U+00A2',
16
+ description: 'CENT SIGN',
17
+ write: '¢',
18
+ label: '¢'
19
+ }, {
20
+ unicode: 'U+00BF',
21
+ description: 'INVERTED QUESTION MARK',
22
+ write: '¿',
23
+ label: '¿'
24
+ }, {
25
+ unicode: 'U+00B4',
26
+ description: 'ACUTE ACCENT',
27
+ write: '´',
28
+ label: '´'
29
+ }, {
30
+ unicode: 'U+00E1',
31
+ description: 'LATIN SMALL LETTER A WITH ACUTE',
32
+ write: 'á',
33
+ label: 'á'
34
+ }, {
35
+ unicode: 'U+00E9',
36
+ description: 'LATIN SMALL LETTER E WITH ACUTE',
37
+ write: 'é',
38
+ label: 'é'
39
+ }, {
40
+ unicode: 'U+00ED',
41
+ description: 'LATIN SMALL LETTER I WITH ACUTE',
42
+ write: 'í',
43
+ label: 'í'
44
+ }, {
45
+ unicode: 'U+00F3',
46
+ description: 'LATIN SMALL LETTER O WITH ACUTE',
47
+ write: 'ó',
48
+ label: 'ó'
49
+ }, {
50
+ unicode: 'U+00FA',
51
+ description: 'LATIN SMALL LETTER U WITH ACUTE',
52
+ write: 'ú',
53
+ label: 'ú'
54
+ }, {
55
+ unicode: 'U+00F1',
56
+ description: 'LATIN SMALL LETTER N WITH TILDE',
57
+ write: 'ñ',
58
+ label: 'ñ'
59
+ }], [{
60
+ unicode: 'U+20AC',
61
+ description: 'EURO SIGN',
62
+ write: '€',
63
+ label: '€'
64
+ }, {
65
+ unicode: 'U+00A1',
66
+ description: 'INVERTED EXCLAMATION MARK',
67
+ write: '¡',
68
+ label: '¡'
69
+ }, {
70
+ unicode: 'U+00C1',
71
+ description: 'LATIN CAPITAL LETTER A WITH ACUTE',
72
+ write: 'Á',
73
+ label: 'Á'
74
+ }, {
75
+ unicode: 'U+00C9',
76
+ description: 'LATIN CAPITAL LETTER E WITH ACUTE',
77
+ write: 'É',
78
+ label: 'É'
79
+ }, {
80
+ unicode: 'U+00CD',
81
+ description: 'LATIN CAPITAL LETTER I WITH ACUTE',
82
+ write: 'Í',
83
+ label: 'Í'
84
+ }, {
85
+ unicode: 'U+00D3',
86
+ description: 'LATIN CAPITAL LETTER O WITH ACUTE',
87
+ write: 'Ó',
88
+ label: 'Ó'
89
+ }, {
90
+ unicode: 'U+00DA',
91
+ description: 'LATIN CAPITAL LETTER U WITH ACUTE',
92
+ write: 'Ú',
93
+ label: 'Ú'
94
+ }, {
95
+ unicode: 'U+00D1',
96
+ description: 'LATIN CAPITAL LETTER N WITH TILDE',
97
+ write: 'Ñ',
98
+ label: 'Ñ'
99
+ }], [{
100
+ unicode: 'U+00A3',
101
+ description: 'POUND SIGN',
102
+ write: '£',
103
+ label: '£'
104
+ }, {
105
+ unicode: 'U+00AB',
106
+ description: 'LEFT-POINTING DOUBLE ANGLE QUOTATION MARK',
107
+ write: '«',
108
+ label: '«'
109
+ }, {
110
+ unicode: 'U+005E',
111
+ description: 'CIRCUMFLEX ACCENT',
112
+ write: '^',
113
+ label: '^'
114
+ }, {
115
+ unicode: 'U+00E2',
116
+ description: 'LATIN SMALL LETTER A WITH CIRCUMFLEX',
117
+ write: 'â',
118
+ label: 'â'
119
+ }, {
120
+ unicode: 'U+00EA',
121
+ description: 'LATIN SMALL LETTER E WITH CIRCUMFLEX',
122
+ write: 'ê',
123
+ label: 'ê'
124
+ }, {
125
+ unicode: 'U+00EE',
126
+ description: 'LATIN SMALL LETTER I WITH CIRCUMFLEX',
127
+ write: 'î',
128
+ label: 'î'
129
+ }, {
130
+ unicode: 'U+00F4',
131
+ description: 'LATIN SMALL LETTER O WITH CIRCUMFLEX',
132
+ write: 'ô',
133
+ label: 'ô'
134
+ }, {
135
+ unicode: 'U+00FB',
136
+ description: 'LATIN SMALL LETTER U WITH CIRCUMFLEX',
137
+ write: 'û',
138
+ label: 'û'
139
+ }, {
140
+ unicode: 'U+00E7',
141
+ description: 'LATIN SMALL LETTER C WITH CEDILLA',
142
+ write: 'ç',
143
+ label: 'ç'
144
+ }], [{
145
+ unicode: 'U+00A5',
146
+ description: 'YEN SIGN',
147
+ write: '¥',
148
+ label: '¥'
149
+ }, {
150
+ unicode: 'U+00BB',
151
+ description: 'RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK',
152
+ write: '»',
153
+ label: '»'
154
+ }, {
155
+ unicode: 'U+00C2',
156
+ description: 'LATIN CAPITAL LETTER A WITH CIRCUMFLEX',
157
+ write: 'Â',
158
+ label: 'Â'
159
+ }, {
160
+ unicode: 'U+00CA',
161
+ description: 'LATIN CAPITAL LETTER E WITH CIRCUMFLEX',
162
+ write: 'Ê',
163
+ label: 'Ê'
164
+ }, {
165
+ unicode: 'U+00CE',
166
+ description: 'LATIN CAPITAL LETTER I WITH CIRCUMFLEX',
167
+ write: 'Î',
168
+ label: 'Î'
169
+ }, {
170
+ unicode: 'U+00D4',
171
+ description: 'LATIN CAPITAL LETTER O WITH CIRCUMFLEX',
172
+ write: 'Ô',
173
+ label: 'Ô'
174
+ }, {
175
+ unicode: 'U+00DB',
176
+ description: 'LATIN CAPITAL LETTER U WITH CIRCUMFLEX',
177
+ write: 'Û',
178
+ label: 'Û'
179
+ }, {
180
+ unicode: 'U+00C7',
181
+ description: 'LATIN CAPITAL LETTER C WITH CEDILLA',
182
+ write: 'Ç',
183
+ label: 'Ç'
184
+ }], [{
185
+ unicode: 'U+00A0',
186
+ description: 'NO-BREAK SPACE',
187
+ write: String.fromCodePoint('0x00A0'),
188
+ label: '&nbsp;'
189
+ }, {
190
+ unicode: 'U+00A7',
191
+ description: 'SECTION SIGN',
192
+ write: '§',
193
+ label: '§'
194
+ }, {
195
+ unicode: 'U+00A8',
196
+ description: 'DIAERESIS',
197
+ write: '¨',
198
+ label: '¨'
199
+ }, {
200
+ unicode: 'U+00E4',
201
+ description: 'LATIN SMALL LETTER A WITH DIAERESIS',
202
+ write: 'ä',
203
+ label: 'ä'
204
+ }, {
205
+ unicode: 'U+00EB',
206
+ description: 'LATIN SMALL LETTER E WITH DIAERESIS',
207
+ write: 'ë',
208
+ label: 'ë'
209
+ }, {
210
+ unicode: 'U+00EF',
211
+ description: 'LATIN SMALL LETTER I WITH DIAERESIS',
212
+ write: 'ï',
213
+ label: 'ï'
214
+ }, {
215
+ unicode: 'U+00F6',
216
+ description: 'LATIN SMALL LETTER O WITH DIAERESIS',
217
+ write: 'ö',
218
+ label: 'ö'
219
+ }, {
220
+ unicode: 'U+00FC',
221
+ description: 'LATIN SMALL LETTER U WITH DIAERESIS',
222
+ write: 'ü',
223
+ label: 'ü'
224
+ }, {
225
+ unicode: 'U+00DF',
226
+ description: 'LATIN SMALL LETTER SHARP S',
227
+ write: 'ß',
228
+ label: 'ß'
229
+ }], [{
230
+ unicode: 'U+2009',
231
+ description: 'THIN SPACE',
232
+ write: String.fromCodePoint('0x2009'),
233
+ label: '&thinsp;'
234
+ }, {
235
+ unicode: 'U+2026',
236
+ description: 'HORIZONTAL ELLIPSIS',
237
+ write: '…',
238
+ label: '…'
239
+ }, {
240
+ unicode: 'U+00C4',
241
+ description: 'LATIN CAPITAL LETTER A WITH DIAERESIS',
242
+ write: 'Ä',
243
+ label: 'Ä'
244
+ }, {
245
+ unicode: 'U+00CB',
246
+ description: 'LATIN CAPITAL LETTER E WITH DIAERESIS',
247
+ write: 'Ë',
248
+ label: 'Ë'
249
+ }, {
250
+ unicode: 'U+00CF',
251
+ description: 'LATIN CAPITAL LETTER I WITH DIAERESIS',
252
+ write: 'Ï',
253
+ label: 'Ï'
254
+ }, {
255
+ unicode: 'U+00D6',
256
+ description: 'LATIN CAPITAL LETTER O WITH DIAERESIS',
257
+ write: 'Ö',
258
+ label: 'Ö'
259
+ }, {
260
+ unicode: 'U+00DC',
261
+ description: 'LATIN CAPITAL LETTER U WITH DIAERESIS',
262
+ write: 'Ü',
263
+ label: 'Ü'
264
+ }, {
265
+ unicode: 'U+2212',
266
+ description: 'MINUS SIGN',
267
+ write: '−',
268
+ label: '−'
269
+ }], [{
270
+ unicode: 'U+200A',
271
+ description: 'HAIR SPACE',
272
+ write: String.fromCodePoint('0x200A'),
273
+ label: '&hairsp;'
274
+ }, {
275
+ unicode: 'U+2022',
276
+ description: 'BULLET',
277
+ write: '•',
278
+ label: '•'
279
+ }, {
280
+ unicode: 'U+0060',
281
+ description: 'GRAVE ACCENT',
282
+ write: '`',
283
+ label: '`'
284
+ }, {
285
+ unicode: 'U+00E0',
286
+ description: 'LATIN SMALL LETTER A WITH GRAVE',
287
+ write: 'à',
288
+ label: 'à'
289
+ }, {
290
+ unicode: 'U+00E8',
291
+ description: 'LATIN SMALL LETTER E WITH GRAVE',
292
+ write: 'è',
293
+ label: 'è'
294
+ }, {
295
+ unicode: 'U+00EC',
296
+ description: 'LATIN SMALL LETTER I WITH GRAVE',
297
+ write: 'ì',
298
+ label: 'ì'
299
+ }, {
300
+ unicode: 'U+00F2',
301
+ description: 'LATIN SMALL LETTER O WITH GRAVE',
302
+ write: 'ò',
303
+ label: 'ò'
304
+ }, {
305
+ unicode: 'U+00F9',
306
+ description: 'LATIN SMALL LETTER U WITH GRAVE',
307
+ write: 'ù',
308
+ label: 'ù'
309
+ }, {
310
+ unicode: 'U+2013',
311
+ description: 'EN DASH',
312
+ write: '–',
313
+ label: '–'
314
+ }], [{
315
+ unicode: 'U+0009',
316
+ description: 'TAB',
317
+ write: String.fromCodePoint('0x0009'),
318
+ label: 'TAB'
319
+ }, {
320
+ unicode: 'U+25E6',
321
+ description: 'WHITE BULLET',
322
+ write: '◦',
323
+ label: '◦'
324
+ }, {
325
+ unicode: 'U+00C0',
326
+ description: 'LATIN CAPITAL LETTER A WITH GRAVE',
327
+ write: 'À',
328
+ label: 'À'
329
+ }, {
330
+ unicode: 'U+00C8',
331
+ description: 'LATIN CAPITAL LETTER E WITH GRAVE',
332
+ write: 'È',
333
+ label: 'È'
334
+ }, {
335
+ unicode: 'U+00CC',
336
+ description: 'LATIN CAPITAL LETTER I WITH GRAVE',
337
+ write: 'Ì',
338
+ label: 'Ì'
339
+ }, {
340
+ unicode: 'U+00D2',
341
+ description: 'LATIN CAPITAL LETTER O WITH GRAVE',
342
+ write: 'Ò',
343
+ label: 'Ò'
344
+ }, {
345
+ unicode: 'U+00D9',
346
+ description: 'LATIN CAPITAL LETTER U WITH GRAVE',
347
+ write: 'Ù',
348
+ label: 'Ù'
349
+ }, {
350
+ unicode: 'U+2014',
351
+ description: 'EM DASH',
352
+ write: '—',
353
+ label: '—'
354
+ }]]
355
+ };
356
+ exports.specialConfig = specialConfig;
357
+ var characterIcons = {
358
+ spanish: 'ñ',
359
+ special: '€'
360
+ };
361
+ exports.characterIcons = characterIcons;
362
+ //# sourceMappingURL=utils.js.map