Skip to content

Commit 9774d8d

Browse files
committed
initial changes for mobile mode.
1 parent 413afe1 commit 9774d8d

File tree

8 files changed

+440
-48
lines changed

8 files changed

+440
-48
lines changed

package-lock.json

Lines changed: 334 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"test:single": "karma start --single-run",
2222
"eslint": "eslint ./src --ext .ts",
2323
"prettier": "prettier --check ./src",
24-
"prettier:fix": "prettier --write ./src"
24+
"prettier:fix": "prettier --write ./src",
25+
"serve": "http-server ./"
2526
},
2627
"author": "b4rtaz",
2728
"license": "MIT",
@@ -30,16 +31,17 @@
3031
"@typescript-eslint/eslint-plugin": "^5.27.0",
3132
"@typescript-eslint/parser": "^5.27.0",
3233
"eslint": "^8.17.0",
34+
"http-server": "^14.1.1",
3335
"karma": "^6.3.20",
3436
"karma-chrome-launcher": "^3.1.1",
3537
"karma-jasmine": "^5.0.1",
3638
"karma-spec-reporter": "^0.0.34",
3739
"karma-typescript": "^5.5.3",
40+
"prettier": "^2.6.2",
3841
"rollup": "^2.75.3",
3942
"rollup-plugin-dts": "^4.2.2",
4043
"rollup-plugin-typescript2": "^0.31.2",
41-
"typescript": "^4.6.4",
42-
"prettier": "^2.6.2"
44+
"typescript": "^4.6.4"
4345
},
4446
"keywords": [
4547
"workflow",

src/designer-view.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Dom } from './core/dom';
2+
import { DesignerView } from './designer-view';
3+
import { createDesignerConfigurationStub, createDesignerContextStub } from './test-tools/stubs';
4+
5+
describe('DesignerView', () => {
6+
it('create() creates view', () => {
7+
const parent = Dom.element('div');
8+
const configuration = createDesignerConfigurationStub();
9+
const context = createDesignerContextStub();
10+
11+
const view = DesignerView.create(parent, context, configuration);
12+
13+
expect(view).toBeDefined();
14+
});
15+
});

src/designer-view.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ControlBar } from './control-bar/control-bar';
2+
import { Dom } from './core/dom';
3+
import { DesignerConfiguration } from './designer-configuration';
4+
import { DesignerContext } from './designer-context';
5+
import { SmartEditor } from './smart-editor/smart-editor';
6+
import { Toolbox } from './toolbox/toolbox';
7+
import { Workspace } from './workspace/workspace';
8+
9+
export class DesignerView {
10+
public static create(parent: HTMLElement, context: DesignerContext, configuration: DesignerConfiguration): DesignerView {
11+
const theme = configuration.theme || 'light';
12+
13+
const root = Dom.element('div', {
14+
class: `sqd-designer sqd-theme-${theme}`
15+
});
16+
parent.appendChild(root);
17+
18+
const workspace = Workspace.create(root, context);
19+
if (!configuration.toolbox.isHidden) {
20+
Toolbox.create(root, context);
21+
}
22+
ControlBar.create(root, context);
23+
if (!configuration.editors.isHidden) {
24+
SmartEditor.create(root, context);
25+
}
26+
const view = new DesignerView(root, workspace);
27+
view.reloadMobileMode();
28+
window.addEventListener('resize', view.onResizeHandler);
29+
return view;
30+
}
31+
32+
private onResizeHandler = () => this.onResize();
33+
34+
public constructor(private readonly root: HTMLElement, public readonly workspace: Workspace) {}
35+
36+
public destroy() {
37+
this.workspace.destroy();
38+
39+
window.removeEventListener('resize', this.onResizeHandler);
40+
this.root.parentElement?.removeChild(this.root);
41+
}
42+
43+
private onResize() {
44+
this.reloadMobileMode();
45+
}
46+
47+
private reloadMobileMode() {
48+
const isMobile = window.innerWidth < 500; // TODO
49+
Dom.toggleClass(this.root, isMobile, 'sqd-mobile');
50+
}
51+
}

src/designer.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,25 @@
11
import { BehaviorController } from './behaviors/behavior-controller';
2-
import { ControlBar } from './control-bar/control-bar';
3-
import { Dom } from './core/dom';
42
import { ObjectCloner } from './core/object-cloner';
53
import { SimpleEvent } from './core/simple-event';
64
import { Definition } from './definition';
75
import { DesignerConfiguration } from './designer-configuration';
86
import { DesignerContext } from './designer-context';
9-
import { SmartEditor } from './smart-editor/smart-editor';
10-
import { Toolbox } from './toolbox/toolbox';
11-
import { Workspace } from './workspace/workspace';
7+
import { DesignerView } from './designer-view';
128

139
export default class Designer {
1410
public static create(parent: HTMLElement, startDefinition: Definition, configuration: DesignerConfiguration): Designer {
15-
const theme = configuration.theme || 'light';
1611
const definition = ObjectCloner.deepClone(startDefinition);
1712

18-
const root = Dom.element('div', {
19-
class: `sqd-designer sqd-theme-${theme}`
20-
});
21-
22-
parent.appendChild(root);
23-
2413
const behaviorController = new BehaviorController();
2514
const context = new DesignerContext(definition, behaviorController, configuration);
2615

27-
const workspace = Workspace.create(root, context);
28-
if (!configuration.toolbox.isHidden) {
29-
Toolbox.create(root, context);
30-
}
31-
ControlBar.create(root, context);
32-
if (!configuration.editors.isHidden) {
33-
SmartEditor.create(root, context);
34-
}
35-
36-
const designer = new Designer(root, context, workspace);
16+
const view = DesignerView.create(parent, context, configuration);
17+
const designer = new Designer(view, context);
3718
context.onDefinitionChanged.subscribe(() => designer.onDefinitionChanged.forward(context.definition));
3819
return designer;
3920
}
4021

41-
private constructor(
42-
private readonly root: HTMLElement,
43-
private readonly context: DesignerContext,
44-
private readonly workspace: Workspace
45-
) {}
22+
private constructor(private readonly view: DesignerView, private readonly context: DesignerContext) {}
4623

4724
public readonly onDefinitionChanged = new SimpleEvent<Definition>();
4825

@@ -51,11 +28,11 @@ export default class Designer {
5128
}
5229

5330
public revalidate() {
54-
this.workspace.revalidate();
31+
this.view.workspace.revalidate();
5532
}
5633

5734
public isValid(): boolean {
58-
return this.workspace.isValid;
35+
return this.view.workspace.isValid;
5936
}
6037

6138
public isReadonly(): boolean {
@@ -87,6 +64,6 @@ export default class Designer {
8764
}
8865

8966
public destroy() {
90-
this.root.parentElement?.removeChild(this.root);
67+
this.view.destroy();
9168
}
9269
}

src/workspace/common-views/region-view.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export class RegionView {
99
class: 'sqd-region',
1010
width: totalWidth,
1111
height,
12-
fill: 'transparent'
12+
fill: 'transparent',
13+
rx: 5,
14+
ry: 5
1315
});
1416
const regions: SVGElement[] = [mainRegion];
1517
parent.insertBefore(mainRegion, parent.firstChild);

src/workspace/workspace-view.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ export class WorkspaceView {
4343
canvas.appendChild(foreground);
4444
workspace.appendChild(canvas);
4545
parent.appendChild(workspace);
46-
return new WorkspaceView(workspace, canvas, gridPattern, gridPatternPath, foreground, configuration);
46+
47+
const view = new WorkspaceView(workspace, canvas, gridPattern, gridPatternPath, foreground, configuration);
48+
window.addEventListener('resize', view.onResizeHandler);
49+
return view;
4750
}
4851

52+
private onResizeHandler = () => this.onResize();
4953
public rootComponent?: StartStopComponent;
5054

5155
private constructor(
@@ -62,6 +66,7 @@ export class WorkspaceView {
6266
this.rootComponent.view.destroy();
6367
}
6468
this.rootComponent = StartStopComponent.create(this.foreground, sequence, this.configuration);
69+
this.refreshSize();
6570
}
6671

6772
public setPositionAndScale(position: Vector, scale: number) {
@@ -80,13 +85,6 @@ export class WorkspaceView {
8085
});
8186
}
8287

83-
public refreshSize() {
84-
Dom.attrs(this.canvas, {
85-
width: this.workspace.offsetWidth,
86-
height: this.workspace.offsetHeight
87-
});
88-
}
89-
9088
public getClientPosition(): Vector {
9189
const rect = this.canvas.getBoundingClientRect();
9290
return new Vector(rect.x, rect.y);
@@ -96,10 +94,6 @@ export class WorkspaceView {
9694
return new Vector(this.canvas.clientWidth, this.canvas.clientHeight);
9795
}
9896

99-
public bindResize(handler: () => void) {
100-
window.addEventListener('resize', handler);
101-
}
102-
10397
public bindMouseDown(handler: (e: MouseEvent) => void) {
10498
this.canvas.addEventListener('mousedown', handler);
10599
}
@@ -111,4 +105,19 @@ export class WorkspaceView {
111105
public bindWheel(handler: (e: WheelEvent) => void) {
112106
this.canvas.addEventListener('wheel', handler);
113107
}
108+
109+
public destroy() {
110+
window.removeEventListener('resize', this.onResizeHandler);
111+
}
112+
113+
private refreshSize() {
114+
Dom.attrs(this.canvas, {
115+
width: this.workspace.offsetWidth,
116+
height: this.workspace.offsetHeight
117+
});
118+
}
119+
120+
private onResize() {
121+
this.refreshSize();
122+
}
114123
}

src/workspace/workspace.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class Workspace implements DesignerComponentProvider {
1515
const workspace = new Workspace(view, context);
1616
setTimeout(() => {
1717
workspace.render();
18-
workspace.view.refreshSize();
1918
workspace.resetViewPort();
2019
});
2120

@@ -25,7 +24,6 @@ export class Workspace implements DesignerComponentProvider {
2524
context.onSelectedStepChanged.subscribe(s => workspace.onSelectedStepChanged(s));
2625
context.onIsDraggingChanged.subscribe(i => workspace.onIsDraggingChanged(i));
2726

28-
workspace.view.bindResize(() => workspace.view.refreshSize());
2927
workspace.view.bindMouseDown(e => workspace.onMouseDown(e));
3028
workspace.view.bindTouchStart(e => workspace.onTouchStart(e));
3129
workspace.view.bindWheel(e => workspace.onWheel(e));
@@ -85,6 +83,10 @@ export class Workspace implements DesignerComponentProvider {
8583
this.context.animateViewPort(realPos.add(clientSize.divideByScalar(2)).subtract(componentOffset), 1);
8684
}
8785

86+
public destroy() {
87+
this.view.destroy();
88+
}
89+
8890
private onMouseDown(e: MouseEvent) {
8991
e.preventDefault();
9092
const isMiddleButton = e.button === 1;

0 commit comments

Comments
 (0)