UNPKG

hadron-document

Version:
85 lines 2.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const hadron_type_checker_1 = __importDefault(require("hadron-type-checker")); const element_events_1 = require("../element-events"); const utils_1 = require("../utils"); /** * Regex to match an array or object string. */ const ARRAY_OR_OBJECT = /^(\[|\{)(.+)(\]|\})$/; /** * CRUD editor for standard values. */ class StandardEditor { /** * Create the editor with the element. * * @param {Element} element - The hadron document element. */ constructor(element) { this.element = element; this.type = element.currentType; this.editing = false; } /** * Edit the element with the provided value. * * @param {Object} value - The new value. */ edit(value) { const currentType = this.element.currentType; try { const newValue = hadron_type_checker_1.default.cast(value, currentType); this.element.edit(newValue); } catch (e) { this.element.setInvalid(value, currentType, e.message); } } /** * Edit the element via a paste. * * @param {String} value - The value. */ paste(value) { if (ARRAY_OR_OBJECT.exec(value)) { this.edit(JSON.parse(value)); this.element._bubbleUp(element_events_1.ElementEvents.Converted, this.element); } else { this.edit(value); } } /** * Get the number of characters the value should display. * * @param {Boolean} editMode - If the element is being edited. * * @returns {Number} The number of characters. */ size() { return (0, utils_1.fieldStringLen)(this.element.currentValue); } /** * Get the value being edited. Always returns a string because this value will * always be used by browser input elements that operate on nothing but * strings * * @returns {string} The value. */ value() { return String(this.element.currentValue); } // Standard editing requires no special start/complete behaviour. start() { this.editing = true; } complete() { this.editing = false; } } exports.default = StandardEditor; //# sourceMappingURL=standard.js.map