@atlaskit/editor-plugin-code-block
Version:
Code block plugin for @atlaskit/editor-core
65 lines (61 loc) • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.codeBlockAutoFullStopTransformPlugin = codeBlockAutoFullStopTransformPlugin;
exports.codeBlockAutoFullStopTransformPluginKey = void 0;
var _browser = require("@atlaskit/editor-common/browser");
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _transforms = require("@atlaskit/editor-common/transforms");
var _state = require("@atlaskit/editor-prosemirror/state");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var codeBlockAutoFullStopTransformPluginKey = exports.codeBlockAutoFullStopTransformPluginKey = new _state.PluginKey('codeBlockAutoFullStopTransformPluginKey');
function codeBlockAutoFullStopTransformPlugin() {
return new _safePlugin.SafePlugin({
key: codeBlockAutoFullStopTransformPluginKey,
appendTransaction: function appendTransaction(_transactions, oldState, newState) {
if (!(0, _platformFeatureFlags.fg)('code_block_auto_insertion_bug_fix')) {
return undefined;
}
// We need to compare the old and new state to isloate auto insertion of fullstop on mac
var trNew = newState.tr;
var trOld = oldState.tr;
var fromOld = trOld.selection.from;
var _trNew$selection = trNew.selection,
fromNew = _trNew$selection.from,
toNew = _trNew$selection.to;
var isCodeBlock = !!(0, _transforms.findCodeBlock)(oldState, trOld.selection) && !!(0, _transforms.findCodeBlock)(newState, trNew.selection);
var browser = (0, _browser.getBrowserInfo)();
/**
* Mac will auto insert a fullstop when the user double taps the space key after some content.
* Line number decorators are assumed content so on new lines the fullstop is inserted.
*
* - When a fulltop is auto inserted the new states selection is the same, the old state selection is one position less
* - The text returned for the old state returns as a space with the selection from - 1
* - The text returned for the new state returns as a fullstop with the selection from - 2 to from -1
*
* This is enough conditional logic to isoloate the auto insertion of the fullstop on mac
*
* There are some solutions to this problem in codemirror which can be read further here
* https://discuss.codemirror.net/t/dot-being-added-when-pressing-space-repeatedly/3899
*/
// both selections must be of code block early exit
if (browser.mac && fromNew === toNew && fromNew === fromOld + 1 && isCodeBlock) {
// detect when '.' is inserted when the previous state was a space ' '
try {
var textBetweenBefore = trOld.doc.textBetween(fromOld - 1, fromOld); // ' '
var textBetweenAfter = trNew.doc.textBetween(fromNew - 2, fromNew - 1); // '.'
if (textBetweenBefore === ' ' && textBetweenAfter === '.') {
trNew.delete(fromNew - 2, fromNew); // remove the fullstop
trNew.insertText(' ', fromNew - 2); // insert double space
return trNew;
}
} catch (error) {
// if for some reason textBetween fails, just return the new transaction as is by defaut.
return undefined;
}
}
return undefined;
}
});
}