UNPKG

@atlaskit/editor-plugin-placeholder-text

Version:

placeholder text plugin for @atlaskit/editor-core

101 lines 4 kB
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import { Slice } from '@atlaskit/editor-prosemirror/model'; import { Selection, TextSelection } from '@atlaskit/editor-prosemirror/state'; import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view'; export var FakeTextCursorBookmark = /*#__PURE__*/function () { function FakeTextCursorBookmark(pos) { _classCallCheck(this, FakeTextCursorBookmark); _defineProperty(this, "pos", undefined); _defineProperty(this, "visible", false); this.pos = pos; } return _createClass(FakeTextCursorBookmark, [{ key: "map", value: function map(mapping) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return new FakeTextCursorBookmark(mapping.map(this.pos)); } }, { key: "resolve", value: function resolve(doc) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion var $pos = doc.resolve(this.pos); return Selection.near($pos); } }]); }(); export var FakeTextCursorSelection = /*#__PURE__*/function (_Selection) { function FakeTextCursorSelection($pos) { _classCallCheck(this, FakeTextCursorSelection); return _callSuper(this, FakeTextCursorSelection, [$pos, $pos]); } _inherits(FakeTextCursorSelection, _Selection); return _createClass(FakeTextCursorSelection, [{ key: "map", value: function map(doc, mapping) { var $pos = doc.resolve(mapping.map(this.$head.pos)); return new FakeTextCursorSelection($pos); } }, { key: "eq", value: function eq(other) { return other instanceof FakeTextCursorSelection && other.head === this.head; } }, { key: "toJSON", value: function toJSON() { return { type: 'Cursor', pos: this.head }; } }, { key: "getBookmark", value: function getBookmark() { return new FakeTextCursorBookmark(this.anchor); } }], [{ key: "content", value: function content() { return Slice.empty; } }, { key: "fromJSON", value: function fromJSON(doc, json) { return new FakeTextCursorSelection(doc.resolve(json.pos)); } }]); }(Selection); Selection.jsonID('fake-text-cursor', FakeTextCursorSelection); export var addFakeTextCursor = function addFakeTextCursor(state, dispatch) { var selection = state.selection; if (selection.empty) { var $from = state.selection.$from; dispatch(state.tr.setSelection(new FakeTextCursorSelection($from))); } }; export var removeFakeTextCursor = function removeFakeTextCursor(state, dispatch) { if (state.selection instanceof FakeTextCursorSelection) { var $from = state.selection.$from; dispatch(state.tr.setSelection(new TextSelection($from))); } }; export var drawFakeTextCursor = function drawFakeTextCursor(state) { if (!(state.selection instanceof FakeTextCursorSelection)) { return null; } var node = document.createElement('div'); node.className = 'ProseMirror-fake-text-cursor'; return DecorationSet.create(state.doc, [Decoration.widget(state.selection.head, node, { key: 'Cursor' })]); };