Skip to content

Commit

Permalink
story(playground): Add gravity playground. Add some fixes and feature…
Browse files Browse the repository at this point in the history
…s for playground
  • Loading branch information
draedful committed Oct 8, 2024
1 parent 0d53e16 commit 6634dea
Show file tree
Hide file tree
Showing 28 changed files with 885 additions and 102 deletions.
42 changes: 41 additions & 1 deletion .storybook/styles/global.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@

@mixin nv-legacy-text {
@font-face {
font-family: "YS Text";
src: url("https://yastatic.net/s3/home/fonts/ys/1/text-light.woff2") format("woff2");
font-weight: 300;
font-style: normal;
font-stretch: normal;
font-display: swap;
}

@font-face {
font-family: "YS Text";
src: url("https://yastatic.net/s3/home/fonts/ys/1/text-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-stretch: normal;
font-display: swap;
}

@font-face {
font-family: "YS Text";
src: url("https://yastatic.net/s3/home/fonts/ys/1/text-medium.woff2") format("woff2");
font-weight: 500;
font-style: normal;
font-stretch: normal;
font-display: swap;
}

@font-face {
font-family: "YS Text";
src: url("https://yastatic.net/s3/home/fonts/ys/1/text-bold.woff2") format("woff2");
font-weight: bold;
font-style: normal;
font-stretch: normal;
font-display: swap;
}
}


.sb-show-main {
padding: 0 !important;
margin: 0;
margin: 0;
font-family: "YS Text";
}

.toolbox {
Expand Down
43 changes: 37 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
}
],
"dependencies": {
"@monaco-editor/react": "^4.6.0",
"@preact/signals-core": "^1.5.1",
"intersects": "^2.7.2",
"lodash-es": "^4.17.21",
Expand Down
7 changes: 7 additions & 0 deletions src/components/canvas/anchors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type TAnchorState = {
};

export class Anchor extends withHitTest(EventedComponent) {

public readonly cursor = 'pointer';

public get zIndex() {
// @ts-ignore this.__comp.parent instanceOf Block
return this.__comp.parent.zIndex + 1;
Expand Down Expand Up @@ -75,6 +78,10 @@ export class Anchor extends withHitTest(EventedComponent) {
this.shift = this.props.size / 2 + props.lineWidth;
}

public getPosition() {
return this.props.getPosition(this.props);
}

protected subscribe() {
return [
this.connectedState.$selected.subscribe((selected) => {
Expand Down
1 change: 1 addition & 0 deletions src/components/canvas/blocks/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export class Block<T extends TBlock = TBlock, Props extends TBlockProps = TBlock
}

protected onDragStart(event: MouseEvent) {
console.log('drag start')
this.context.graph.executеDefaultEventAction(
"block-drag-start",
{
Expand Down
2 changes: 2 additions & 0 deletions src/components/canvas/connections/BlockConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type TConnectionState = TConnection & {
};

export class BlockConnection extends withBatchedConnection(withHitTest(EventedComponent)) {

public readonly cursor = 'pointer';
public declare props: TConnectionProps;

public declare state: TConnectionState;
Expand Down
5 changes: 5 additions & 0 deletions src/components/canvas/layers/graphLayer/GraphLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ export class GraphLayer extends Layer<TGraphLayerProps, TGraphLayerContext> {

private onRootPointerMove(event: MouseEvent) {
if (this.targetComponent !== this.prevTargetComponent) {
if(this.targetComponent?.cursor) {
this.root.style.cursor = this.targetComponent?.cursor;
} else {
this.root.style.removeProperty('cursor');
}
this.applyEventToTargetComponent(
new CustomEvent("mouseleave", {
bubbles: false,
Expand Down
4 changes: 2 additions & 2 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export type LayerConfig<T extends Constructor<Layer> = Constructor<Layer>> = [
? Omit<Props, "root" | "camera" | "graph"> & { root?: Props["root"] }
: never,
];
export type TGraphConfig<TBlockMeta extends Record<string, unknown> = {}> = {
export type TGraphConfig<B extends TBlock = TBlock> = {
configurationName?: string;
blocks?: TBlock<TBlockMeta>[];
blocks?: B[];
connections?: TConnection[];
rect?: TRect;
cameraXY?: TPoint;
Expand Down
127 changes: 63 additions & 64 deletions src/mixins/withEvents.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,86 @@
import { Component } from "../lib/Component";
import { Constructor } from "../lib/tshelpers";

export function withEvent<T extends Constructor<Component>>(Base: T): T & Constructor<IWithEvent> {
return class WithEvent extends Base implements IWithEvent {
private _listenEvents: object = {};
export class EventedComponent extends Component {
private _listenEvents: object = {};

public unmount() {
super.unmount();
this._listenEvents = {};
}
public readonly cursor?: string;

public hasEventListener(type: string) {
return Object.prototype.hasOwnProperty.call(this._listenEvents, type);
}
protected unmount() {
super.unmount();
this._listenEvents = {};
}

public addEventListener(type: string, cbOrObject: any) {
if (Array.isArray(this._listenEvents[type])) {
this._listenEvents[type].push(cbOrObject);
} else {
this._listenEvents[type] = [cbOrObject];
}
public hasEventListener(type: string) {
return Object.prototype.hasOwnProperty.call(this._listenEvents, type);
}

public addEventListener(type: string, cbOrObject: any) {
if (Array.isArray(this._listenEvents[type])) {
this._listenEvents[type].push(cbOrObject);
} else {
this._listenEvents[type] = [cbOrObject];
}
return () => this.removeEventListener(type, cbOrObject);
}

public removeEventListener(type: string, cbOrObject: any) {
if (Array.isArray(this._listenEvents[type])) {
const i = this._listenEvents[type].indexOf(cbOrObject);
public removeEventListener(type: string, cbOrObject: any) {
if (Array.isArray(this._listenEvents[type])) {
const i = this._listenEvents[type].indexOf(cbOrObject);

if (i !== -1) {
this._listenEvents[type].splice(i, 1);
}
if (i !== -1) {
this._listenEvents[type].splice(i, 1);
}
}
}

public _fireEvent(cmp: any, event: Event) {
if (!this._hasListener(cmp, event.type)) {
return;
}
public _fireEvent(cmp: any, event: Event) {
if (!this._hasListener(cmp, event.type)) {
return;
}

const fnsOrObjects = cmp._listenEvents[event.type];
const fnsOrObjects = cmp._listenEvents[event.type];

for (let i = 0; i < fnsOrObjects.length; i += 1) {
if (typeof fnsOrObjects[i] === "function") {
fnsOrObjects[i](event);
} else if (typeof fnsOrObjects[i] === "object" && typeof fnsOrObjects[i].handleEvent === "function") {
fnsOrObjects[i].handleEvent(event);
}
for (let i = 0; i < fnsOrObjects.length; i += 1) {
if (typeof fnsOrObjects[i] === "function") {
fnsOrObjects[i](event);
} else if (typeof fnsOrObjects[i] === "object" && typeof fnsOrObjects[i].handleEvent === "function") {
fnsOrObjects[i].handleEvent(event);
}
}
}

public dispatchEvent(event: Event): boolean {
const bubbles = event.bubbles || false;
public dispatchEvent(event: Event): boolean {
const bubbles = event.bubbles || false;

if (bubbles) {
return this._dipping(this, event);
} else if (this._hasListener(this, event.type)) {
this._fireEvent(this, event);
return false;
}
if (bubbles) {
return this._dipping(this, event);
} else if (this._hasListener(this, event.type)) {
this._fireEvent(this, event);
return false;
}
return false;
}

public _dipping(startParent: Component, event: Event) {
let stopPropagation = false;
let parent: Component | undefined = startParent;
event.stopPropagation = () => {
stopPropagation = true;
};

do {
this._fireEvent(parent, event);
if (stopPropagation) {
return false;
}
parent = parent.getParent() as Component;
} while (parent);

public _dipping(startParent: Component, event: Event) {
let stopPropagation = false;
let parent: Component | undefined = startParent;
event.stopPropagation = () => {
stopPropagation = true;
};

do {
this._fireEvent(parent, event);
if (stopPropagation) {
return false;
}
parent = parent.getParent() as Component;
} while (parent);

return true;
}
return true;
}

public _hasListener(comp: any, type: string) {
return comp._listenEvents !== undefined && comp._listenEvents[type] !== undefined;
}
};
public _hasListener(comp: any, type: string) {
return comp._listenEvents !== undefined && comp._listenEvents[type] !== undefined;
}
}

export class EventedComponent extends withEvent(Component) {}
1 change: 1 addition & 0 deletions src/react-component/Anchor.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
width: var(--graph-block-anchor-width, 16px);
height: var(--graph-block-anchor-height, 16px);
cursor: pointer;
will-change: transform;
}

.graph-block-anchor.graph-block-anchor-selected {
Expand Down
Loading

0 comments on commit 6634dea

Please sign in to comment.