UNPKG

@sanity/form-builder

Version:
84 lines (81 loc) 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useHotkeys = useHotkeys; var _react = require("react"); var _portableTextEditor = require("@sanity/portable-text-editor"); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } // This hook will create final hotkeys for the editor from on those from props. function useHotkeys(hotkeys) { var editor = (0, _portableTextEditor.usePortableTextEditor)(); // Guard that hotkeys from props will be a stable object. // If this props is defined inline and is always a new object, there will be issues with key handling and cursor! var initialHotkeys = (0, _react.useMemo)(() => hotkeys, []); if (initialHotkeys !== hotkeys) { console.warn('Make sure that hotkeys are a stable object across renders, or there will be issues with key handling in the Portable Text Editor.'); } return (0, _react.useMemo)(() => { var defaultHotkeys = { marks: {} }; var ptFeatures = _portableTextEditor.PortableTextEditor.getPortableTextFeatures(editor); ptFeatures.decorators.forEach(dec => { switch (dec.value) { case 'strong': defaultHotkeys.marks['mod+b'] = dec.value; break; case 'em': defaultHotkeys.marks['mod+i'] = dec.value; break; case 'underline': defaultHotkeys.marks['mod+u'] = dec.value; break; case 'code': defaultHotkeys.marks["mod+'"] = dec.value; break; default: } }); return { marks: _objectSpread(_objectSpread({}, defaultHotkeys.marks), (initialHotkeys || {}).marks), custom: initialHotkeys.custom }; }, [editor, initialHotkeys]); } // If we want to have a hotkey to open up a focused object, we can use this: // // const handleOpenObjectHotkey = ( // event: React.BaseSyntheticEvent, // ptEditor: PortableTextEditor // ) => { // const selection = PortableTextEditor.getSelection(ptEditor) // if (selection) { // event.preventDefault() // event.stopPropagation() // const {focus} = selection // const activeAnnotations = PortableTextEditor.activeAnnotations(ptEditor) // const focusBlock = PortableTextEditor.focusBlock(ptEditor) // const focusChild = PortableTextEditor.focusChild(ptEditor) // if (activeAnnotations.length > 0) { // onFocus([ // ...focus.path.slice(0, 1), // 'markDefs', // {_key: activeAnnotations[0]._key}, // FOCUS_TERMINATOR, // ]) // return // } // if (focusChild && PortableTextEditor.isVoid(ptEditor, focusChild)) { // onFocus([...focus.path, FOCUS_TERMINATOR]) // return // } // if (focusBlock && PortableTextEditor.isVoid(ptEditor, focusBlock)) { // onFocus([...focus.path.slice(0, 1), FOCUS_TERMINATOR]) // } // } // }