@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
119 lines • 4.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var prosemirror_1 = require("../../prosemirror");
var utils_1 = require("../utils");
var EditorActions = (function () {
function EditorActions() {
}
// This method needs to be public for context based helper components.
EditorActions.prototype._privateGetEditorView = function () {
return this.editorView;
};
// This method needs to be public for EditorContext component.
EditorActions.prototype._privateRegisterEditor = function (editorView, contentTransformer) {
if (!this.editorView && editorView) {
this.editorView = editorView;
}
else if (this.editorView !== editorView) {
throw new Error('Editor has already been registered! It\'s not allowed to re-register editor with the new Editor instance.');
}
this.contentTransformer = contentTransformer;
};
// This method needs to be public for EditorContext component.
EditorActions.prototype._privateUnregisterEditor = function () {
this.editorView = undefined;
};
EditorActions.prototype.focus = function () {
if (!this.editorView || this.editorView.hasFocus()) {
return false;
}
this.editorView.focus();
return true;
};
EditorActions.prototype.blur = function () {
if (!this.editorView || !this.editorView.hasFocus()) {
return false;
}
this.editorView.dom.blur();
return true;
};
EditorActions.prototype.clear = function () {
if (!this.editorView) {
return false;
}
var editorView = this.editorView;
var state = editorView.state;
var tr = editorView.state.tr
.setSelection(prosemirror_1.TextSelection.create(state.doc, 0, state.doc.nodeSize - 2))
.deleteSelection();
editorView.dispatch(tr);
return true;
};
EditorActions.prototype.getValue = function () {
var _this = this;
return utils_1.getEditorValueWithMedia(this.editorView && this.editorView.state).then(function (doc) {
if (_this.contentTransformer && doc) {
return _this.contentTransformer.encode(doc);
}
return doc;
});
};
EditorActions.prototype.replaceDocument = function (rawValue) {
if (!this.editorView || !rawValue) {
return false;
}
var state = this.editorView.state;
var schema = state.schema;
var value;
if (typeof rawValue === 'string') {
try {
value = JSON.parse(rawValue);
}
catch (e) {
return false;
}
}
else if (rawValue instanceof prosemirror_1.Node) {
// If rawValue is instance of Node we convert it to JSON and re-create children,
// so we get completely new document instead of changing the one that was passed to us
value = rawValue.toJSON();
}
else {
value = rawValue;
}
var content = (value.content || []).map(function (child) { return schema.nodeFromJSON(child); });
if (!content.length) {
return false;
}
var tr = state.tr
.replaceWith(0, state.doc.nodeSize - 2, content)
.scrollIntoView();
this.editorView.dispatch(tr);
return true;
};
EditorActions.prototype.appendText = function (text) {
if (!this.editorView || !text) {
return false;
}
var state = this.editorView.state;
var lastChild = state.doc.lastChild;
if (lastChild && lastChild.type !== state.schema.nodes.paragraph) {
return false;
}
var tr = state.tr
.insertText(text)
.scrollIntoView();
this.editorView.dispatch(tr);
return true;
};
EditorActions.prototype.insertFileFromDataUrl = function (url, filename) {
if (!this.editorView) {
return false;
}
utils_1.insertFileFromDataUrl(this.editorView.state, url, filename);
return true;
};
return EditorActions;
}());
exports.default = EditorActions;
//# sourceMappingURL=index.js.map