-
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/react/playground): add freelayout feature. add custom sourc…
…e icon feature (#184)
- Loading branch information
Showing
12 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Engine, CursorType } from '../models' | ||
import { DragStartEvent, DragMoveEvent, DragStopEvent } from '../events' | ||
import { TreeNode } from '../models' | ||
import { Point } from '@designable/shared' | ||
|
||
type TranslateData = { | ||
element?: HTMLElement | ||
node?: TreeNode | ||
point?: Point | ||
} | ||
|
||
type TranslateStore = { | ||
value?: TranslateData | ||
} | ||
|
||
export const useTranslateEffect = (engine: Engine) => { | ||
const findStartNodeHandler = (target: HTMLElement): TranslateData => { | ||
const handler = target?.closest(`*[${engine.props.nodeTranslateAttrName}]`) | ||
if (handler) { | ||
const type = handler.getAttribute(engine.props.nodeTranslateAttrName) | ||
if (type) { | ||
const element = handler.closest( | ||
`*[${engine.props.nodeSelectionIdAttrName}]` | ||
) as HTMLElement | ||
if (element) { | ||
const nodeId = element.getAttribute( | ||
engine.props.nodeSelectionIdAttrName | ||
) | ||
if (nodeId) { | ||
const node = engine.findNodeById(nodeId) | ||
if (node) { | ||
return { node, element } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
const store: TranslateStore = {} | ||
|
||
engine.subscribeTo(DragStartEvent, (event) => { | ||
if (engine.cursor.type !== CursorType.Move) return | ||
const target = event.data.target as HTMLElement | ||
const data = findStartNodeHandler(target) | ||
if (data) { | ||
const point = new Point(event.data.clientX, event.data.clientY) | ||
store.value = { | ||
...data, | ||
point, | ||
} | ||
} | ||
}) | ||
|
||
engine.subscribeTo(DragMoveEvent, (event) => { | ||
if (engine.cursor.type !== CursorType.Move) return | ||
if (store.value) { | ||
const { node, element, point } = store.value | ||
const allowTranslate = node.allowTranslate() | ||
if (!allowTranslate) return | ||
const translatable = node.designerProps.translatable | ||
const current = new Point(event.data.clientX, event.data.clientY) | ||
const diffX = current.x - point?.x | ||
const diffY = current.y - point?.y | ||
const horizontal = translatable.x?.(node, element, diffX) | ||
const vertical = translatable.y?.(node, element, diffY) | ||
horizontal.translate() | ||
vertical.translate() | ||
store.value.point = current | ||
} | ||
}) | ||
|
||
engine.subscribeTo(DragStopEvent, () => { | ||
if (engine.cursor.type !== CursorType.Move) return | ||
if (store.value) { | ||
store.value = null | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
|
||
export const FreeMove = ( | ||
<svg viewBox="0 0 1024 1024"> | ||
<path | ||
d="M469.333333 938.666667c-17.066667 0-29.866667-8.533333-42.666666-17.066667l-209.066667-179.2 29.866667-34.133333c8.533333-8.533333 21.333333-12.8 29.866666-12.8h8.533334L426.666667 768V341.333333c0-25.6 17.066667-42.666667 42.666666-42.666666s42.666667 17.066667 42.666667 42.666666v192l51.2 4.266667 209.066667 93.866667c21.333333 8.533333 38.4 34.133333 38.4 55.466666v187.733334c0 34.133333-29.866667 64-64 64H469.333333z m128-640V42.666667l128 128-128 128z m0-85.333334H341.333333v85.333334L213.333333 170.666667l128-128v85.333333h256v85.333333z" | ||
p-id="13294" | ||
fill="#ffffff" | ||
></path> | ||
</svg> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/react/src/widgets/AuxToolWidget/TranslateHandler.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react' | ||
import cls from 'classnames' | ||
import { useDesigner, usePrefix } from '../../hooks' | ||
import { TreeNode } from '@designable/core' | ||
import { IconWidget } from '../IconWidget' | ||
|
||
export interface ITranslateHandlerProps { | ||
node: TreeNode | ||
} | ||
|
||
export const TranslateHandler: React.FC<ITranslateHandlerProps> = (props) => { | ||
const designer = useDesigner() | ||
const prefix = usePrefix('aux-node-translate-handler') | ||
const createHandler = (value: string) => { | ||
return { | ||
[designer.props.nodeTranslateAttrName]: value, | ||
className: cls(prefix, value), | ||
} | ||
} | ||
const allowTranslate = props.node.allowTranslate() | ||
if (!allowTranslate) return null | ||
return ( | ||
<> | ||
<div {...createHandler('translate')}> | ||
<IconWidget infer="FreeMove" /> | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters