@atlaskit/editor-plugin-code-block
Version:
Code block plugin for @atlaskit/editor-core
48 lines • 2.38 kB
JavaScript
import React, { useCallback, useState } from 'react';
import { fg } from '@atlaskit/platform-feature-flags';
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 const CodeBlockLanguagePicker = ({
api,
defaultValue,
editorView,
filterOption,
formatMessage,
languagePickerOptions,
triggerSpacing
}) => {
var _api$analytics2;
const [recentLanguageValues, setRecentLanguageValues] = useState(() => getRecentLanguages());
const refreshRecentLanguages = useCallback(() => {
setRecentLanguageValues(getRecentLanguages());
}, []);
const handleSelection = useCallback((option, selectionSource) => {
var _api$analytics;
const command = option.value === DETECT_LANGUAGE_VALUE && fg('platform_editor_code_block_language_detection_flow') ? detectLanguage() : changeLanguage(api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(option.value, selectionSource);
const commandSucceeded = command(editorView.state, editorView.dispatch);
if (fg('platform_editor_code_block_dogfooding_patch')) {
requestAnimationFrame(() => {
// Let PopupSelect/FocusLock finish returning focus to the trigger, then restore the editor.
api === null || api === void 0 ? void 0 : api.core.actions.focus({
scrollIntoView: false
});
});
}
if (commandSucceeded && option.value !== DETECT_LANGUAGE_VALUE) {
saveRecentLanguage(option.value);
setRecentLanguageValues(getRecentLanguages());
}
}, [api === null || api === void 0 ? 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
});
};