@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
78 lines • 3.41 kB
JavaScript
import { getUrlForDomainInContext } from '@atlaskit/atlassian-context';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import { linkPreferencesPath } from './constants';
var STAGING = 'staging';
var PRODUCTION = 'prod';
var DEV = 'dev';
export function isTextAtPos(pos) {
return function (_ref) {
var tr = _ref.tr;
var node = tr.doc.nodeAt(pos);
return !!node && node.isText;
};
}
export function isLinkAtPos(pos) {
return function (state) {
var node = state.doc.nodeAt(pos);
return !!node && !!state.schema.marks.link.isInSet(node.marks);
};
}
export var getLinkPreferencesURLFromENV = function getLinkPreferencesURLFromENV() {
var envType = process.env.CLOUD_ENV === 'staging' ? STAGING : PRODUCTION;
return getUrlForDomainInContext('id', envType) + linkPreferencesPath;
};
var isSelectionInsideLink = function isSelectionInsideLink(state) {
return !!state.doc.type.schema.marks.link.isInSet(state.selection.$from.marks());
};
var isEmptySelectionBeforeLink = function isEmptySelectionBeforeLink(state) {
var _$from$nodeAfter;
var _state$selection = state.selection,
$from = _state$selection.$from,
$to = _state$selection.$to;
return $from === $to && !!state.doc.type.schema.marks.link.isInSet(((_$from$nodeAfter = $from.nodeAfter) === null || _$from$nodeAfter === void 0 ? void 0 : _$from$nodeAfter.marks) || []);
};
var isEmptySelectionAfterLink = function isEmptySelectionAfterLink(state) {
var _$from$nodeBefore;
var _state$selection2 = state.selection,
$from = _state$selection2.$from,
$to = _state$selection2.$to;
return $from === $to && !!state.doc.type.schema.marks.link.isInSet(((_$from$nodeBefore = $from.nodeBefore) === null || _$from$nodeBefore === void 0 ? void 0 : _$from$nodeBefore.marks) || []);
};
var isSelectionAroundLink = function isSelectionAroundLink(state) {
var _state$selection3 = state.selection,
$from = _state$selection3.$from,
$to = _state$selection3.$to;
var node = $from.nodeAfter;
return !!node && $from.textOffset === 0 && $to.pos - $from.pos === node.nodeSize && !!state.doc.type.schema.marks.link.isInSet(node.marks);
};
export var getActiveLinkMark = function getActiveLinkMark(state) {
var $from = state.selection.$from;
if (isSelectionInsideLink(state) || isSelectionAroundLink(state)) {
var pos = $from.pos - $from.textOffset;
var node = state.doc.nodeAt(pos);
return node && node.isText ? {
node: node,
pos: pos
} : undefined;
}
if (isEmptySelectionBeforeLink(state) && editorExperiment('platform_editor_controls', 'variant1')) {
// if user clicks right before the link, we want to show the toolbar for the link
// but only if the link is a single character
var _node = state.doc.nodeAt($from.pos);
return _node && _node.isText && _node.nodeSize === 1 ? {
node: _node,
pos: $from.pos
} : undefined;
}
if (isEmptySelectionAfterLink(state) && editorExperiment('platform_editor_controls', 'variant1')) {
// if user clicks right after the link, we want to show the toolbar for the link
// but only if the link is a single character
// and we return the position of the link
var _node2 = state.doc.nodeAt($from.pos - 1);
return _node2 && _node2.isText && _node2.nodeSize === 1 ? {
node: _node2,
pos: $from.pos - 1
} : undefined;
}
return undefined;
};