UNPKG

@atlaskit/editor-plugin-code-block

Version:

Code block plugin for @atlaskit/editor-core

47 lines 1.6 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { removeAutoDetection, updateAutoDetectState } from '../utils/auto-detect-state'; import { syncPendingDetectionTimers } from '../utils/auto-detect-view'; import { ACTIONS } from './actions'; import { autoDetectPluginKey } from './auto-detect-state'; export const createAutoDetectPlugin = api => new SafePlugin({ key: autoDetectPluginKey, state: { init() { return { languageDetectionMap: {} }; }, apply(tr, pluginState) { const meta = tr.getMeta(autoDetectPluginKey); let languageDetectionMap = tr.docChanged ? updateAutoDetectState(tr, pluginState) : pluginState.languageDetectionMap; if ((meta === null || meta === void 0 ? void 0 : meta.type) === ACTIONS.SET_AUTO_DETECT_ENTRY) { languageDetectionMap = { ...languageDetectionMap, [meta.data.localId]: meta.data.entry }; } else if ((meta === null || meta === void 0 ? void 0 : meta.type) === ACTIONS.REMOVE_AUTO_DETECT_ENTRY) { languageDetectionMap = removeAutoDetection(languageDetectionMap, meta.data.localId); } if (languageDetectionMap === pluginState.languageDetectionMap) { return pluginState; } return { languageDetectionMap }; } }, view(view) { const timers = new Map(); return { update() { syncPendingDetectionTimers(view, timers, api); }, destroy() { timers.forEach(({ timer }) => clearTimeout(timer)); timers.clear(); } }; } });