-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,018 additions
and
48 deletions.
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
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
25 changes: 25 additions & 0 deletions
25
...erd-editor/src/components/erd/automatic-table-placement/AutomaticTablePlacement.styles.ts
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,25 @@ | ||
import { css } from '@dineug/r-html'; | ||
|
||
export const root = css` | ||
display: flex; | ||
width: 100%; | ||
height: 100%; | ||
overflow: hidden; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
background-color: var(--canvas-boundary-background); | ||
`; | ||
|
||
export const container = css` | ||
display: flex; | ||
width: 100%; | ||
height: 100%; | ||
overflow: hidden; | ||
position: relative; | ||
pointer-events: none; | ||
.minimap-viewport { | ||
display: none; | ||
} | ||
`; |
150 changes: 150 additions & 0 deletions
150
packages/erd-editor/src/components/erd/automatic-table-placement/AutomaticTablePlacement.ts
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,150 @@ | ||
import { toJson } from '@dineug/erd-editor-schema'; | ||
import { createRef, FC, html, Ref, useProvider } from '@dineug/r-html'; | ||
import { createInRange } from '@dineug/shared'; | ||
import { round } from 'lodash-es'; | ||
|
||
import { | ||
AppContext, | ||
appContext, | ||
createAppContext, | ||
} from '@/components/appContext'; | ||
import Canvas from '@/components/erd/canvas/Canvas'; | ||
import Minimap from '@/components/erd/minimap/Minimap'; | ||
import Button from '@/components/primitives/button/Button'; | ||
import Toast from '@/components/primitives/toast/Toast'; | ||
import { CANVAS_ZOOM_MIN } from '@/constants/schema'; | ||
import { endAutomaticTablePlacementAction } from '@/engine/modules/editor/atom.actions'; | ||
import { initialLoadJsonAction$ } from '@/engine/modules/editor/generator.actions'; | ||
import { | ||
changeZoomLevelAction, | ||
scrollToAction, | ||
} from '@/engine/modules/settings/atom.actions'; | ||
import { query } from '@/utils/collection/query'; | ||
import { openToastAction } from '@/utils/emitter'; | ||
import { closePromise } from '@/utils/promise'; | ||
|
||
import * as styles from './AutomaticTablePlacement.styles'; | ||
import { createAutomaticTablePlacement } from './createAutomaticTablePlacement'; | ||
|
||
export type AutomaticTablePlacementProps = { | ||
app: Ref<AppContext>; | ||
root: Ref<HTMLDivElement>; | ||
onChange: (tables: TablePoint[]) => void; | ||
}; | ||
|
||
export type TablePoint = { | ||
id: string; | ||
x: number; | ||
y: number; | ||
}; | ||
|
||
const AutomaticTablePlacement: FC<AutomaticTablePlacementProps> = ( | ||
props, | ||
ctx | ||
) => { | ||
const canvas = createRef<HTMLDivElement>(); | ||
const prevApp = props.app.value; | ||
const appContextValue = createAppContext({ | ||
toWidth: prevApp.toWidth, | ||
}); | ||
useProvider(ctx, appContext, appContextValue); | ||
|
||
const { | ||
store: { state: prevState }, | ||
emitter, | ||
} = prevApp; | ||
const { store } = appContextValue; | ||
|
||
const zoomInRange = createInRange(CANVAS_ZOOM_MIN, 0.7); | ||
const zoomLevelInRange = (zoom: number) => round(zoomInRange(zoom), 2); | ||
|
||
store.dispatchSync( | ||
initialLoadJsonAction$(toJson(prevState)), | ||
changeZoomLevelAction({ | ||
value: zoomLevelInRange( | ||
prevState.editor.viewport.width / prevState.settings.width | ||
), | ||
}), | ||
scrollToAction({ | ||
scrollLeft: | ||
-1 * | ||
(prevState.settings.width / 2 - prevState.editor.viewport.width / 2), | ||
scrollTop: | ||
-1 * | ||
(prevState.settings.height / 2 - prevState.editor.viewport.height / 2), | ||
}) | ||
); | ||
|
||
const { | ||
doc: { tableIds }, | ||
collections, | ||
} = store.state; | ||
|
||
const tables = query(collections) | ||
.collection('tableEntities') | ||
.selectByIds(tableIds); | ||
|
||
const [close, onClose] = closePromise(); | ||
|
||
const handleCancel = () => { | ||
onClose(); | ||
prevApp.store.dispatch(endAutomaticTablePlacementAction()); | ||
}; | ||
|
||
if (!tables.length) { | ||
handleCancel(); | ||
emitter.emit( | ||
openToastAction({ | ||
message: html`<${Toast} description="Not found tables" />`, | ||
}) | ||
); | ||
return () => null; | ||
} | ||
|
||
try { | ||
const simulation = createAutomaticTablePlacement(store.state); | ||
|
||
const handleStop = () => { | ||
simulation.stop(); | ||
props.onChange( | ||
tables.map(table => ({ | ||
id: table.id, | ||
x: table.ui.x, | ||
y: table.ui.y, | ||
})) | ||
); | ||
handleCancel(); | ||
}; | ||
|
||
emitter.emit( | ||
openToastAction({ | ||
close, | ||
message: html` | ||
<${Toast} | ||
description="Automatic Table Placement..." | ||
action=${html` | ||
<${Button} size="1" text=${'Stop'} .onClick=${handleStop} /> | ||
<${Button} size="1" text=${'Cancel'} .onClick=${handleCancel} /> | ||
`} | ||
/> | ||
`, | ||
}) | ||
); | ||
|
||
simulation.on('end', handleStop); | ||
} catch (e) { | ||
handleCancel(); | ||
} | ||
|
||
return () => | ||
html` | ||
<div class=${styles.root}> | ||
<div class=${styles.container}> | ||
<${Canvas} root=${props.root} canvas=${canvas} /> | ||
<${Minimap} /> | ||
</div> | ||
</div> | ||
`; | ||
}; | ||
|
||
export default AutomaticTablePlacement; |
102 changes: 102 additions & 0 deletions
102
.../erd-editor/src/components/erd/automatic-table-placement/createAutomaticTablePlacement.ts
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,102 @@ | ||
import { | ||
forceCollide, | ||
forceLink, | ||
forceManyBody, | ||
forceSimulation, | ||
forceX, | ||
forceY, | ||
} from 'd3'; | ||
|
||
import { RootState } from '@/engine/state'; | ||
import { Table } from '@/internal-types'; | ||
import { calcTableHeight, calcTableWidths } from '@/utils/calcTable'; | ||
import { query } from '@/utils/collection/query'; | ||
import { relationshipSort } from '@/utils/draw-relationship/sort'; | ||
|
||
type Node = { | ||
id: string; | ||
r: number; | ||
x: number; | ||
y: number; | ||
ref: Table; | ||
}; | ||
|
||
type Link = { | ||
source: string; | ||
target: string; | ||
}; | ||
|
||
function createNodes( | ||
state: RootState, | ||
x: number, | ||
y: number | ||
): [Array<Node>, Array<Link>] { | ||
const { | ||
doc: { tableIds, relationshipIds }, | ||
collections, | ||
} = state; | ||
const tables = query(collections) | ||
.collection('tableEntities') | ||
.selectByIds(tableIds); | ||
const relationships = query(collections) | ||
.collection('relationshipEntities') | ||
.selectByIds(relationshipIds); | ||
|
||
const nodes: Node[] = []; | ||
const links: Link[] = []; | ||
const linkIdSet = new Set<string>(); | ||
|
||
tables.forEach(table => { | ||
const width = calcTableWidths(table, state).width; | ||
const height = calcTableHeight(table); | ||
nodes.push({ | ||
id: table.id, | ||
r: (width + height) / 4, | ||
x, | ||
y, | ||
ref: table, | ||
}); | ||
}); | ||
|
||
relationships.forEach(relationship => { | ||
const { start, end } = relationship; | ||
const linkId = `${start.tableId}-${end.tableId}`; | ||
|
||
if (start.tableId !== end.tableId && !linkIdSet.has(linkId)) { | ||
links.push({ | ||
source: start.tableId, | ||
target: end.tableId, | ||
}); | ||
linkIdSet.add(linkId); | ||
} | ||
}); | ||
|
||
return [nodes, links]; | ||
} | ||
|
||
export function createAutomaticTablePlacement(state: RootState) { | ||
const { settings } = state; | ||
const centerX = settings.width / 2; | ||
const centerY = settings.height / 2; | ||
const [nodes, links] = createNodes(state, centerX, centerY); | ||
|
||
return forceSimulation(nodes) | ||
.force( | ||
'link', | ||
forceLink(links).id((d: any) => d.id) | ||
) | ||
.force( | ||
'collide', | ||
forceCollide().radius((d: any) => 100 + d.r) | ||
) | ||
.force('charge', forceManyBody()) | ||
.force('x', forceX(centerX)) | ||
.force('y', forceY(centerY)) | ||
.on('tick', () => { | ||
nodes.forEach(({ r, x, y, ref }) => { | ||
ref.ui.x = x - r; | ||
ref.ui.y = y - r; | ||
}); | ||
relationshipSort(state); | ||
}); | ||
} |
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
Oops, something went wrong.