upfront-editable
Version:
Friendly contenteditable API
103 lines (88 loc) • 2.8 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
// See: https://en.wikipedia.org/wiki/Whitespace_character
var characters = {
'A0': 'no-break space',
// \\u00A0
'2000': 'en quad',
// \\u2000
'2001': 'em quad',
// \\u2001
'2002': 'en space',
// \\u2002
'2003': 'em space',
// \\u2003
'2004': 'three-per-em space',
// \\u2004
'2005': 'four-per-em space',
// \\u2005
'2006': 'six-per-em space',
// \\u2006
'2007': 'figure space',
// \\u2007
'2008': 'punctuation space',
// \\u2008
'2009': 'thin space',
// \\u2009
'200A': 'hair space',
// \\u200A
'202F': 'narrow no-break space',
// \\u202F
'205F': 'medium mathematical space',
// \\u205F
'3000': 'ideographic space' // \\u3000
}; // The no-break space is not highlighted as this can cause problems.
// Browser can insert no-break spaces when typing at the end of
// a paragraph and the highlighting prevents browsers from converting
// the no-break space back to a normal space when the user keeps typing.
var specialWhitespaceChars = "\\u2000-\\u200A\\u202F\\u205F\\u3000";
var WhitespaceHighlighting = /*#__PURE__*/function () {
function WhitespaceHighlighting(markerNode) {
(0, _classCallCheck2["default"])(this, WhitespaceHighlighting);
this.marker = markerNode;
}
(0, _createClass2["default"])(WhitespaceHighlighting, [{
key: "findMatches",
value: function findMatches(text) {
var _this = this;
if (!text) return;
var regex = "[".concat(specialWhitespaceChars, "]");
regex = new RegExp(regex, 'g');
var matches = [];
var match;
while (match = regex.exec(text)) {
matches.push(match);
}
return matches.map(function (entry) {
return _this.prepareMatch(entry);
});
}
}, {
key: "prepareMatch",
value: function prepareMatch(match) {
var startIndex = match.index;
var unicode = getUnicode(match[0]);
var description = characters[unicode];
return {
startIndex: startIndex,
endIndex: startIndex + match.length,
match: match[0],
title: "".concat(description, " (\\u").concat(unicode, ")"),
marker: this.marker
};
}
}]);
return WhitespaceHighlighting;
}();
exports["default"] = WhitespaceHighlighting;
function getUnicode(character) {
var code = character.charCodeAt(0);
return "".concat(code.toString(16).toUpperCase());
}
module.exports = exports.default;