@cc98/react-ubb-editor
Version:
A ubb editor component based on react
29 lines (28 loc) • 1.24 kB
JavaScript
import { ConfigType } from './types';
var defaultHandler = function (state, action) {
var start = state.start, end = state.end, value = state.value;
var tagName = action.tagName, _a = action.payload, payload = _a === void 0 ? {} : _a, type = action.type;
var _b = action.shouldEnter, shouldEnter = _b === void 0 ? type === ConfigType.Extend : _b, _c = action.shouldSelect, shouldSelect = _c === void 0 ? type === ConfigType.Button : _c;
var before = value.slice(0, start);
var after = value.slice(end, value.length);
var content = "[" + tagName;
if (payload.mainValue) {
content += "=" + payload.mainValue;
}
if (payload.subValues && payload.subValues.length) {
content += "," + payload.subValues.map(function (_a) {
var key = _a.key, value = _a.value;
return key + "=" + value;
}).join(',');
}
content += "]" + (payload.content || value.slice(start, end)) + "[/" + tagName + "]";
if (shouldEnter) {
content += '\n';
}
return {
value: "" + before + content + after,
start: shouldSelect ? start : before.length + content.length,
end: before.length + content.length,
};
};
export default defaultHandler;