UNPKG

@atlaskit/editor-plugin-code-block

Version:

Code block plugin for @atlaskit/editor-core

64 lines 3.35 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import React, { useCallback, useState } from 'react'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { changeLanguage, detectLanguage } from '../editor-commands'; import { DETECT_LANGUAGE_VALUE } from './language-picker-options'; import { LanguagePicker } from './LanguagePicker'; import { getRecentLanguages, saveRecentLanguage } from './recent-languages'; export var CodeBlockLanguagePicker = function CodeBlockLanguagePicker(_ref) { var _api$analytics2; var api = _ref.api, defaultValue = _ref.defaultValue, editorView = _ref.editorView, filterOption = _ref.filterOption, formatMessage = _ref.formatMessage, languagePickerOptions = _ref.languagePickerOptions, triggerSpacing = _ref.triggerSpacing; var _useState = useState(function () { return getRecentLanguages(); }), _useState2 = _slicedToArray(_useState, 2), recentLanguageValues = _useState2[0], setRecentLanguageValues = _useState2[1]; var refreshRecentLanguages = useCallback(function () { setRecentLanguageValues(getRecentLanguages()); }, []); var handleSelection = useCallback(function (option, selectionSource, interactionMethod) { var _api$analytics; var isDetectLanguageSelected = option.value === DETECT_LANGUAGE_VALUE; var command = isDetectLanguageSelected && (fg('platform_editor_code_block_ga_patch_1') || fg('platform_editor_code_block_language_detection_flow')) ? detectLanguage() : changeLanguage(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(option.value, selectionSource); var commandSucceeded = command(editorView.state, editorView.dispatch); if (fg('platform_editor_code_block_ga_patch_1')) { if (interactionMethod === 'mouse') { requestAnimationFrame(function () { // Mouse-opened picker should return editing focus to the code block. Keyboard-opened // picker keeps focus on the trigger to avoid CodeMirror DOM focus without cm.hasFocus. api === null || api === void 0 || api.core.actions.focus({ scrollIntoView: false }); }); } } else { requestAnimationFrame(function () { // Let PopupSelect/FocusLock finish returning focus to the trigger, then restore the editor. api === null || api === void 0 || api.core.actions.focus({ scrollIntoView: false }); }); } if (commandSucceeded && (option.value !== DETECT_LANGUAGE_VALUE || fg('platform_editor_code_block_ga_patch_1'))) { saveRecentLanguage(option.value); setRecentLanguageValues(getRecentLanguages()); } }, [api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions, api === null || api === void 0 ? void 0 : api.core.actions, editorView]); return /*#__PURE__*/React.createElement(LanguagePicker, { defaultValue: defaultValue, filterOption: filterOption, formatMessage: formatMessage, languagePickerOptions: languagePickerOptions, recentLanguageValues: recentLanguageValues, onMenuOpen: refreshRecentLanguages, onSelection: handleSelection, triggerSpacing: triggerSpacing }); };