Skip to content

Commit

Permalink
feat: settings tab
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Dec 3, 2023
1 parent bce542d commit 882f6cf
Show file tree
Hide file tree
Showing 28 changed files with 532 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/erd-editor-schema/src/v3/schema/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const CanvasType = {
visualization: '@dineug/erd-editor/builtin-visualization',
schemaSQL: '@dineug/erd-editor/builtin-schema-sql',
generatorCode: '@dineug/erd-editor/builtin-generator-code',
settings: 'settings',
} as const;
export type CanvasType = ValuesType<typeof CanvasType>;
export const CanvasTypeList: ReadonlyArray<string> = Object.values(CanvasType);
Expand Down
7 changes: 5 additions & 2 deletions packages/erd-editor/src/components/appContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type AppContext = EngineContext & {
actions: Actions;
store: RxStore;
keyBindingMap: KeyBindingMap;
shortcut$: Subject<KeyBindingName>;
shortcut$: Subject<{ type: KeyBindingName; event: KeyboardEvent }>;
keydown$: Subject<KeyboardEvent>;
emitter: Emitter;
};
Expand All @@ -36,7 +36,10 @@ export function createAppContext(ctx: InjectAppContext): AppContext {
const engineContext = createEngineContext(ctx);
const store = createRxStore(engineContext);
const keyBindingMap = observable(createKeyBindingMap(), { shallow: true });
const shortcut$ = new Subject<KeyBindingName>();
const shortcut$ = new Subject<{
type: KeyBindingName;
event: KeyboardEvent;
}>();
const keydown$ = new Subject<KeyboardEvent>();
const emitter = new Emitter();

Expand Down
3 changes: 3 additions & 0 deletions packages/erd-editor/src/components/erd-editor/ErdEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Erd from '@/components/erd/Erd';
import GeneratorCode from '@/components/generator-code/GeneratorCode';
import GlobalStyles from '@/components/global-styles/GlobalStyles';
import SchemaSQL from '@/components/schema-sql/SchemaSQL';
import Settings from '@/components/settings/Settings';
import Theme from '@/components/theme/Theme';
import ToastContainer from '@/components/toast-container/ToastContainer';
import Toolbar from '@/components/toolbar/Toolbar';
Expand Down Expand Up @@ -181,6 +182,8 @@ const ErdEditor: FC<ErdEditorProps, ErdEditorElement> = (props, ctx) => {
? html`<${SchemaSQL} isDarkMode=${isDarkMode} />`
: settings.canvasType === CanvasType.generatorCode
? html`<${GeneratorCode} isDarkMode=${isDarkMode} />`
: settings.canvasType === CanvasType.settings
? html`<${Settings} />`
: null}
</div>
<${ToastContainer} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { get, isEmpty, omit } from 'lodash-es';
import { AppContext } from '@/components/appContext';
import { DatabaseVendorToDatabase } from '@/constants/sql/database';
import { clearAction } from '@/engine/modules/editor/atom.actions';
import { initialLoadJsonAction$ } from '@/engine/modules/editor/generator.actions';
import {
initialLoadJsonAction$,
loadJsonAction$,
} from '@/engine/modules/editor/generator.actions';
import { useDarkMode } from '@/hooks/useDarkMode';
import { useUnmounted } from '@/hooks/useUnmounted';
import {
Expand Down Expand Up @@ -129,12 +132,12 @@ export function useErdEditorAttachElement({
};

ctx.clear = () => {
store.dispatch(clearAction());
store.dispatchSync(clearAction());
};

ctx.setInitialValue = value => {
const safeValue = toSafeString(value);
store.dispatch(
store.dispatchSync(
initialLoadJsonAction$(isEmpty(safeValue) ? '{}' : safeValue)
);
};
Expand Down Expand Up @@ -196,7 +199,12 @@ export function useErdEditorAttachElement({

Object.defineProperty(ctx, 'value', {
get: () => toJson(store.state),
set: (value: string) => ctx.setInitialValue(value),
set: (value: string) => {
const safeValue = toSafeString(value);
store.dispatchSync(
loadJsonAction$(isEmpty(safeValue) ? '{}' : safeValue)
);
},
});

return {
Expand Down
10 changes: 6 additions & 4 deletions packages/erd-editor/src/components/erd/Erd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ const Erd: FC<ErdProps> = (props, ctx) => {
: null}
${runAutomaticTablePlacement
? html`
<${AutomaticTablePlacement}
app=${app}
.onChange=${handleChangeAutomaticTablePlacement}
/>
<div>
<${AutomaticTablePlacement}
app=${app}
.onChange=${handleChangeAutomaticTablePlacement}
/>
</div>
`
: null}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { toJson } from '@dineug/erd-editor-schema';
import { createRef, FC, html, Ref, useProvider } from '@dineug/r-html';
import {
createRef,
FC,
html,
onMounted,
onUnmounted,
Ref,
useProvider,
} from '@dineug/r-html';
import { createInRange } from '@dineug/shared';
import { round } from 'lodash-es';

Expand Down Expand Up @@ -47,7 +55,11 @@ const AutomaticTablePlacement: FC<AutomaticTablePlacementProps> = (
const appContextValue = createAppContext({
toWidth: prevApp.toWidth,
});
useProvider(ctx, appContext, appContextValue);
const provider = useProvider(ctx, appContext, appContextValue);

onUnmounted(() => {
provider.destroy();
});

const {
store: { state: prevState },
Expand Down Expand Up @@ -123,7 +135,12 @@ const AutomaticTablePlacement: FC<AutomaticTablePlacementProps> = (
<${Toast}
description="Automatic Table Placement..."
action=${html`
<${Button} size="1" text=${'Stop'} .onClick=${handleStop} />
<${Button}
variant="soft"
size="1"
text=${'Stop'}
.onClick=${handleStop}
/>
<${Button} size="1" text=${'Cancel'} .onClick=${handleCancel} />
`}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ const ErdContextMenu: FC<ErdContextMenuProps> = (props, ctx) => {
<${ContextMenu.Menu}
icon=${html`<${Icon} name="key" size=${14} />`}
name="Primary Key"
right=${html`
<${Kbd}
shortcut=${keyBindingMap.primaryKey[0]?.shortcut}
/>
`}
/>
`}
/>
Expand All @@ -133,6 +138,11 @@ const ErdContextMenu: FC<ErdContextMenuProps> = (props, ctx) => {
<${Icon} prefix="mdi" name="table-cog" size=${14} />
`}
name="Table Properties"
right=${html`
<${Kbd}
shortcut=${keyBindingMap.tableProperties[0]?.shortcut}
/>
`}
/>
`}
/>
Expand Down
42 changes: 22 additions & 20 deletions packages/erd-editor/src/components/erd/useErdShortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ export function useErdShortcut(ctx: Ctx) {
}
};

const handleShortcut = (action: KeyBindingName) => {
const handleShortcut = ({
type,
}: {
type: KeyBindingName;
event: KeyboardEvent;
}) => {
const { store } = app.value;
const { editor, settings } = store.state;
const showHighLevelTable = isHighLevelTable(settings.zoomLevel);
Expand All @@ -109,19 +114,19 @@ export function useErdShortcut(ctx: Ctx) {
}

if (!editor.focusTable || !editor.focusTable.edit) {
action === KeyBindingName.addTable && store.dispatch(addTableAction$());
action === KeyBindingName.addColumn && store.dispatch(addColumnAction$());
action === KeyBindingName.addMemo && store.dispatch(addMemoAction$());
type === KeyBindingName.addTable && store.dispatch(addTableAction$());
type === KeyBindingName.addColumn && store.dispatch(addColumnAction$());
type === KeyBindingName.addMemo && store.dispatch(addMemoAction$());

action === KeyBindingName.selectAllTable &&
type === KeyBindingName.selectAllTable &&
store.dispatch(selectAllAction());

if (isRelationshipKeyBindingName(action)) {
const relationshipType = keyBindingNameToRelationshipType[action];
if (isRelationshipKeyBindingName(type)) {
const relationshipType = keyBindingNameToRelationshipType[type];
store.dispatch(drawStartRelationshipAction$(relationshipType));
}

if (action === KeyBindingName.removeTable) {
if (type === KeyBindingName.removeTable) {
ctx.host.dispatchEvent(focusEvent());
store.dispatch(removeSelectedAction$());
}
Expand All @@ -130,19 +135,19 @@ export function useErdShortcut(ctx: Ctx) {

// KeyBindingName.find

action === KeyBindingName.zoomIn &&
type === KeyBindingName.zoomIn &&
store.dispatch(streamZoomLevelAction({ value: 0.1 }));
action === KeyBindingName.zoomOut &&
type === KeyBindingName.zoomOut &&
store.dispatch(streamZoomLevelAction({ value: -0.1 }));
}

if (!showHighLevelTable) {
if (editor.focusTable && !editor.focusTable.edit) {
action === KeyBindingName.selectAllColumn &&
type === KeyBindingName.selectAllColumn &&
store.dispatch(selectAllColumnAction());

if (
action === KeyBindingName.removeColumn &&
type === KeyBindingName.removeColumn &&
editor.focusTable.selectColumnIds.length
) {
store.dispatch(
Expand All @@ -156,10 +161,7 @@ export function useErdShortcut(ctx: Ctx) {
// KeyBindingName.copyColumn
// KeyBindingName.pasteColumn

if (
action === KeyBindingName.primaryKey &&
editor.focusTable.columnId
) {
if (type === KeyBindingName.primaryKey && editor.focusTable.columnId) {
store.dispatch(
changeColumnPrimaryKeyAction$(
editor.focusTable.tableId,
Expand All @@ -169,7 +171,7 @@ export function useErdShortcut(ctx: Ctx) {
}
}

if (editor.focusTable && action === KeyBindingName.edit) {
if (editor.focusTable && type === KeyBindingName.edit) {
const focusTable = editor.focusTable;

if (focusTable.edit) {
Expand All @@ -191,13 +193,13 @@ export function useErdShortcut(ctx: Ctx) {
}
}

if (action === KeyBindingName.stop) {
if (type === KeyBindingName.stop) {
ctx.host.dispatchEvent(forceFocusEvent());
store.dispatch(drawEndRelationshipAction(), unselectAllAction$());
}

action === KeyBindingName.undo && store.undo();
action === KeyBindingName.redo && store.redo();
type === KeyBindingName.undo && store.undo();
type === KeyBindingName.redo && store.redo();
};

onMounted(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { css } from '@dineug/r-html';
export const root = css`
position: relative;
height: 100%;
overflow: auto;
overflow: hidden;
background-color: var(--canvas-background);
`;
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const GeneratorCode: FC<GeneratorCodeProps> = (props, ctx) => {
emitter.emit(
openToastAction({
close: delay(2000),
message: html`<${Toast} description="Copied!" />`,
message: html`<${Toast} title="Copied!" />`,
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const meta = {
control: 'radio',
options: ['1', '2', '3'],
},
variant: {
control: 'radio',
options: ['solid', 'soft'],
},
text: {
control: 'text',
},
Expand All @@ -27,9 +31,18 @@ const meta = {
export default meta;
type Story = StoryObj<ButtonProps>;

export const Normal: Story = {
export const Solid: Story = {
args: {
size: '2',
variant: 'solid',
text: 'Button',
},
};

export const Soft: Story = {
args: {
size: '2',
variant: 'soft',
text: 'Button',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const button = css`
export const soft = css`
background-color: var(--accent-color-3);
color: var(--accent-color-11);
fill: var(--accent-color-11);
&:hover {
background-color: var(--accent-color-4);
Expand All @@ -24,6 +25,20 @@ export const soft = css`
}
`;

export const solid = css`
background-color: var(--accent-color-9);
color: #fff;
fill: #fff;
&:hover {
background-color: var(--accent-color-10);
}
&:active {
background-color: var(--accent-color-10);
}
`;

export const size1 = css`
font-weight: var(--font-weight-medium);
padding: 0 8px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { DOMTemplateLiterals, FC, html } from '@dineug/r-html';
import * as styles from './Button.styles';

export type ButtonProps = {
size: '1' | '2' | '3';
size?: '1' | '2' | '3';
variant?: 'solid' | 'soft';
text: string | DOMTemplateLiterals;
onClick?: (event: PointerEvent) => void;
};
Expand All @@ -14,8 +15,8 @@ const Button: FC<ButtonProps> = (props, ctx) => {
<button
class=${[
styles.button,
styles.soft,
Reflect.get(styles, `size${props.size ?? 2}`),
Reflect.get(styles, props.variant ?? 'solid'),
Reflect.get(styles, `size${props.size ?? '2'}`),
]}
type="button"
@click=${props.onClick}
Expand Down
Loading

0 comments on commit 882f6cf

Please sign in to comment.