@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
8 lines (7 loc) • 6.63 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/resizable-editor/index.js"],
"sourcesContent": ["import clsx from 'clsx';\nimport { useDispatch } from '@wordpress/data';\nimport { useRef, useCallback, useState } from '@wordpress/element';\nimport {\n\tResizableBox,\n\t__unstableMotion as motion,\n} from '@wordpress/components';\nimport { useReducedMotion } from '@wordpress/compose';\nimport ResizeHandle from './resize-handle';\nimport { store as editorStore } from '../../store';\nimport { unlock } from '../../lock-unlock';\n\n// Removes the inline styles in the drag handles.\nconst HANDLE_STYLES_OVERRIDE = {\n\tposition: undefined,\n\tuserSelect: undefined,\n\tcursor: undefined,\n\twidth: undefined,\n\theight: undefined,\n\ttop: undefined,\n\tright: undefined,\n\tbottom: undefined,\n\tleft: undefined,\n};\n\n/**\n * Checks if the current width is at the max width.\n *\n * @param {number} currentWidth - The current width of the editor.\n * @param {number} containerWidth - The width of the container.\n * @param {number} tolerance - The tolerance for the max width in pixels.\n * @return {boolean} - True if the current width is at the max width, false otherwise.\n */\nfunction isAtMaxWidth( currentWidth, containerWidth, tolerance = 0 ) {\n\treturn containerWidth > 0 && currentWidth >= containerWidth - tolerance;\n}\n\nfunction ResizableEditor( {\n\tclassName,\n\tenableResizing,\n\twidth = '100%',\n\theight = '100%',\n\tchildren,\n} ) {\n\tconst [ isResizing, setIsResizing ] = useState( false );\n\tconst disableMotion = useReducedMotion();\n\tconst { setCanvasWidth } = unlock( useDispatch( editorStore ) );\n\n\tconst resizableRef = useRef();\n\tconst resizeWidthBy = useCallback(\n\t\t( deltaPixels ) => {\n\t\t\tif ( resizableRef.current ) {\n\t\t\t\tconst _isAtMaxWidth = isAtMaxWidth(\n\t\t\t\t\tresizableRef.current.offsetWidth + deltaPixels,\n\t\t\t\t\tresizableRef.current.parentElement?.offsetWidth ?? 0,\n\t\t\t\t\t80\n\t\t\t\t);\n\t\t\t\tsetCanvasWidth(\n\t\t\t\t\t_isAtMaxWidth\n\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t: resizableRef.current.offsetWidth + deltaPixels\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[ setCanvasWidth ]\n\t);\n\n\tconst updateCanvasWidth = useCallback(\n\t\t( element, isResizeEnd ) => {\n\t\t\tconst currentWidth = element.offsetWidth;\n\t\t\tconst containerWidth = element.parentElement?.offsetWidth ?? 0;\n\t\t\tsetCanvasWidth(\n\t\t\t\tisResizeEnd && isAtMaxWidth( currentWidth, containerWidth, 80 )\n\t\t\t\t\t? undefined\n\t\t\t\t\t: currentWidth\n\t\t\t);\n\t\t},\n\t\t[ setCanvasWidth ]\n\t);\n\n\treturn (\n\t\t<ResizableBox\n\t\t\tas={ motion.div }\n\t\t\tinitial={ false }\n\t\t\tanimate={ { height } }\n\t\t\ttransition={ {\n\t\t\t\ttype: 'tween',\n\t\t\t\tduration: isResizing || disableMotion ? 0 : 0.2,\n\t\t\t\tease: 'easeOut',\n\t\t\t} }\n\t\t\tclassName={ clsx( 'editor-resizable-editor', className, {\n\t\t\t\t'is-resizable': enableResizing,\n\t\t\t\t'is-resizing': isResizing,\n\t\t\t} ) }\n\t\t\tref={ ( api ) => {\n\t\t\t\tresizableRef.current = api?.resizable;\n\t\t\t} }\n\t\t\tsize={ {\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t} }\n\t\t\tonResizeStart={ () => {\n\t\t\t\tsetIsResizing( true );\n\t\t\t} }\n\t\t\tonResize={ ( event, direction, element ) => {\n\t\t\t\tupdateCanvasWidth( element );\n\t\t\t} }\n\t\t\tonResizeStop={ ( event, direction, element ) => {\n\t\t\t\tupdateCanvasWidth( element, true );\n\t\t\t\tsetIsResizing( false );\n\t\t\t} }\n\t\t\tminWidth={ 300 }\n\t\t\tmaxWidth=\"100%\"\n\t\t\tmaxHeight=\"100%\"\n\t\t\tenable={ {\n\t\t\t\tleft: enableResizing,\n\t\t\t\tright: enableResizing,\n\t\t\t} }\n\t\t\tshowHandle={ enableResizing }\n\t\t\t// The editor is centered horizontally, resizing it only\n\t\t\t// moves half the distance. Hence double the ratio to correctly\n\t\t\t// align the cursor to the resizer handle.\n\t\t\tresizeRatio={ 2 }\n\t\t\thandleComponent={ {\n\t\t\t\tleft: (\n\t\t\t\t\t<ResizeHandle\n\t\t\t\t\t\tdirection=\"left\"\n\t\t\t\t\t\tresizeWidthBy={ resizeWidthBy }\n\t\t\t\t\t/>\n\t\t\t\t),\n\t\t\t\tright: (\n\t\t\t\t\t<ResizeHandle\n\t\t\t\t\t\tdirection=\"right\"\n\t\t\t\t\t\tresizeWidthBy={ resizeWidthBy }\n\t\t\t\t\t/>\n\t\t\t\t),\n\t\t\t} }\n\t\t\thandleClasses={ undefined }\n\t\t\thandleStyles={ {\n\t\t\t\tleft: HANDLE_STYLES_OVERRIDE,\n\t\t\t\tright: HANDLE_STYLES_OVERRIDE,\n\t\t\t} }\n\t\t>\n\t\t\t{ children }\n\t\t</ResizableBox>\n\t);\n}\n\nexport default ResizableEditor;\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAiB;AACjB,kBAA4B;AAC5B,qBAA8C;AAC9C,wBAGO;AACP,qBAAiC;AACjC,2BAAyB;AACzB,mBAAqC;AACrC,yBAAuB;AAmHlB;AAhHL,IAAM,yBAAyB;AAAA,EAC9B,UAAU;AAAA,EACV,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,KAAK;AAAA,EACL,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,MAAM;AACP;AAUA,SAAS,aAAc,cAAc,gBAAgB,YAAY,GAAI;AACpE,SAAO,iBAAiB,KAAK,gBAAgB,iBAAiB;AAC/D;AAEA,SAAS,gBAAiB;AAAA,EACzB;AAAA,EACA;AAAA,EACA,QAAQ;AAAA,EACR,SAAS;AAAA,EACT;AACD,GAAI;AACH,QAAM,CA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