upfront-editable
Version:
Friendly contenteditable API
92 lines (73 loc) • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// 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 = function () {
function WhitespaceHighlighting(markerNode) {
(0, _classCallCheck3.default)(this, WhitespaceHighlighting);
this.marker = markerNode;
}
(0, _createClass3.default)(WhitespaceHighlighting, [{
key: 'findMatches',
value: function findMatches(text) {
var _this = this;
if (!text) return;
var regex = '[' + specialWhitespaceChars + ']';
regex = new RegExp(regex, 'g');
var matches = [];
var match = void 0;
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: description + ' (\\u' + unicode + ')',
marker: this.marker
};
}
}]);
return WhitespaceHighlighting;
}();
exports.default = WhitespaceHighlighting;
function getUnicode(character) {
var code = character.charCodeAt(0);
return '' + code.toString(16).toUpperCase();
}