usxeditor
Version:
USX editor react component.
161 lines (160 loc) • 8.25 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var react_autobind_1 = __importDefault(require("react-autobind"));
var Utils = __importStar(require("../Utils"));
var EditableText_1 = __importDefault(require("./EditableText"));
var styles_1 = __importDefault(require("../styles"));
var Para = /** @class */ (function (_super) {
__extends(Para, _super);
function Para(props) {
var _this = _super.call(this, props) || this;
(0, react_autobind_1.default)(_this);
return _this;
}
Object.defineProperty(Para, "TagName", {
get: function () { return 'para'; },
enumerable: false,
configurable: true
});
Para.prototype.shouldComponentUpdate = function (nextProps) {
if (nextProps.raw !== this.props.raw)
return true;
if (nextProps.allowFocus !== this.props.allowFocus)
return true;
if (nextProps.selectionStart !== this.props.selectionStart)
return this.isSelectionInComponent(nextProps.selectionStart, nextProps.startOffset);
if (nextProps.selectionEnd !== this.props.selectionEnd)
return this.isSelectionInComponent(nextProps.selectionEnd, nextProps.startOffset);
return false;
};
Para.prototype.isSelectionInComponent = function (selection, startOffset) {
if (selection >= startOffset)
return true;
return false;
};
Para.prototype.handleInsertMarker = function (isPara, style, newValue) {
var _a;
if (isPara) {
var effectiveRawUsx = (_a = (newValue && Utils.toOuterUsx(Para.TagName, this.props.attrs, newValue))) !== null && _a !== void 0 ? _a : this.props.raw;
var updatedUsx = effectiveRawUsx.replace("\\".concat(style), "</".concat(Para.TagName, "><").concat(Para.TagName, " style=\"").concat(style, "\"> "));
this.props.onChanged(updatedUsx);
}
// Insert (end) Char marker.
else if (style.endsWith('*')) {
var usx = Utils.serialize(this.props.children);
// If there doesn't appear to be a matching start char.
// ENHANCE: TODO: check the end tag matches the start tag first. (ie. deal with this error case\\nd followed by \wj*)
if (usx.indexOf('</char>') === -1) {
usx = usx.replace("\\".concat(style.slice(0, -1)), "<unmatched marker=\"".concat(style, "\" />"));
var updatedUsx = Utils.toOuterUsx(Para.TagName, this.props.attrs, usx);
this.props.onChanged(updatedUsx);
}
else {
var markerWithoutEndAsterisk = style.slice(0, -1);
var startOfMarker = usx.indexOf("\\".concat(markerWithoutEndAsterisk));
if (startOfMarker === -1)
return;
var part = usx.substring(startOfMarker).replace('</char>', '');
usx = usx.substring(0, startOfMarker) + part;
usx = usx.replace("\\".concat(markerWithoutEndAsterisk), '</char>');
var updatedUsx = Utils.toOuterUsx(Para.TagName, this.props.attrs, usx);
this.props.onChanged(updatedUsx);
}
}
// Insert (start) Char marker.
else {
var usx = Utils.serialize(this.props.children);
var startOfMarker = usx.indexOf("\\".concat(style));
var nextCharPos = usx.indexOf('<char ', startOfMarker);
if (nextCharPos !== -1)
usx = usx.substring(0, nextCharPos) + '</char>' + usx.substring(nextCharPos);
usx = usx.replace("\\".concat(style), "<char style=\"".concat(style, "\"> "));
if (nextCharPos === -1)
usx += '</char>';
var updatedUsx = Utils.toOuterUsx(Para.TagName, this.props.attrs, usx);
this.props.onChanged(updatedUsx);
}
};
Para.prototype.render = function () {
var _this = this;
var _a = this.props, attrs = _a.attrs, otherProps = __rest(_a, ["attrs"]);
// Paragraph markers without children should at the very least show a space.
// TODO: REVIEW should this be instead of ' '?
var defaultChild = react_1.default.createElement(EditableText_1.default
// Match the same key as xml-to-react uses.
// This should prevent remounting.
, __assign({
// Match the same key as xml-to-react uses.
// This should prevent remounting.
key: 0, value: ' ', onInsertMarker: this.handleInsertMarker }, otherProps, { raw: ' ' }));
return react_1.default.createElement("div", null,
react_1.default.createElement("span", { style: styles_1.default.marker }, "\\".concat(attrs.style, " ")),
Utils.addHandlersToChildren(__assign(__assign({ children: defaultChild }, otherProps), { onChanged: function (newValue) { return _this.props.onChanged(Utils.toOuterUsx(Para.TagName, attrs, newValue)); }, onInsertMarker: this.handleInsertMarker, onDeleteBackward: function (index) { return Utils.defaultDeleteBackward(_this.props, index, Para.TagName, attrs); } })));
};
return Para;
}(react_1.default.Component));
exports.default = Para;