@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
116 lines • 4.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var prosemirror_1 = require("../../prosemirror");
var utils_1 = require("../../utils");
var commands_1 = require("./commands");
var keymap_1 = require("./keymap");
var input_rule_1 = require("./input-rule");
/**
*
* Plugin State
*
*/
var ListsState = (function () {
function ListsState(state) {
this.changeHandlers = [];
// public state
this.bulletListActive = false;
this.bulletListDisabled = false;
this.bulletListHidden = false;
this.orderedListActive = false;
this.orderedListDisabled = false;
this.orderedListHidden = false;
this.changeHandlers = [];
// Checks what types of lists schema supports.
var _a = state.schema.nodes, bulletList = _a.bulletList, orderedList = _a.orderedList;
this.bulletListHidden = !bulletList;
this.orderedListHidden = !orderedList;
}
ListsState.prototype.subscribe = function (cb) {
this.changeHandlers.push(cb);
cb(this);
};
ListsState.prototype.unsubscribe = function (cb) {
this.changeHandlers = this.changeHandlers.filter(function (ch) { return ch !== cb; });
};
ListsState.prototype.toggleBulletList = function (view) {
return commands_1.toggleList(view.state, view.dispatch, view, 'bulletList');
};
ListsState.prototype.toggleOrderedList = function (view) {
return commands_1.toggleList(view.state, view.dispatch, view, 'orderedList');
};
ListsState.prototype.update = function (newEditorState) {
var doc = newEditorState.doc, selection = newEditorState.selection;
var ancestorPosition = utils_1.findAncestorPosition(doc, selection.$from);
var rootNode = selection instanceof prosemirror_1.NodeSelection
? selection.node
: ancestorPosition.node(ancestorPosition.depth);
var dirty = false;
var newBulletListActive = rootNode.type === newEditorState.schema.nodes.bulletList;
if (newBulletListActive !== this.bulletListActive) {
this.bulletListActive = newBulletListActive;
dirty = true;
}
var newOrderedListActive = rootNode.type === newEditorState.schema.nodes.orderedList;
if (newOrderedListActive !== this.orderedListActive) {
this.orderedListActive = newOrderedListActive;
dirty = true;
}
var newBulletListDisabled = !(newBulletListActive ||
newOrderedListActive ||
this.isWrappingPossible(newEditorState.schema.nodes.bulletList, newEditorState));
if (newBulletListDisabled !== this.bulletListDisabled) {
this.bulletListDisabled = newBulletListDisabled;
dirty = true;
}
var newOrderedListDisabled = !(newBulletListActive ||
newOrderedListActive ||
this.isWrappingPossible(newEditorState.schema.nodes.orderedList, newEditorState));
if (newOrderedListDisabled !== this.orderedListDisabled) {
this.orderedListDisabled = newOrderedListDisabled;
dirty = true;
}
if (dirty) {
this.triggerOnChange();
}
};
ListsState.prototype.triggerOnChange = function () {
var _this = this;
this.changeHandlers.forEach(function (cb) { return cb(_this); });
};
ListsState.prototype.isWrappingPossible = function (nodeType, state) {
var _a = state.selection, $from = _a.$from, $to = _a.$to;
var range = $from.blockRange($to);
if (!range) {
return false;
}
var wrap = prosemirror_1.findWrapping(range, nodeType);
if (!wrap) {
return false;
}
return true;
};
return ListsState;
}());
exports.ListsState = ListsState;
exports.stateKey = new prosemirror_1.PluginKey('listsPlugin');
exports.plugin = new prosemirror_1.Plugin({
state: {
init: function (config, state) {
return new ListsState(state);
},
apply: function (tr, pluginState, oldState, newState) {
pluginState.update(newState);
return pluginState;
}
},
key: exports.stateKey,
view: function (view) {
return {};
}
});
var plugins = function (schema) {
return [exports.plugin, input_rule_1.default(schema), keymap_1.default(schema)].filter(function (plugin) { return !!plugin; });
};
exports.default = plugins;
//# sourceMappingURL=index.js.map