-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proof of concept: base classes for options, events, interactions #93
Open
mggower
wants to merge
4
commits into
techdebt/imports
Choose a base branch
from
techdebt/abstracts
base: techdebt/imports
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,101 @@ | ||
import { Node, Edge, Annotation, Viewport } from './objects' | ||
|
||
export type RectPoint = 'nw' | 'ne' | 'se' | 'sw' | ||
|
||
interface EventBase<T extends string> { | ||
type: T | ||
altKey?: boolean | ||
ctrlKey?: boolean | ||
metaKey?: boolean | ||
shiftKey?: boolean | ||
} | ||
|
||
interface MouseEvent<T extends string> extends EventBase<T> { | ||
x: number | ||
y: number | ||
clientX: number | ||
clientY: number | ||
} | ||
|
||
interface DragEvent<T extends string> extends MouseEvent<T> { | ||
dx: number | ||
dy: number | ||
} | ||
|
||
export interface NodePointerEvent extends MouseEvent<'nodePointer'> { | ||
target: Node | ||
targetIndex: number | ||
} | ||
|
||
export interface NodeDragEvent extends DragEvent<'nodeDrag'> { | ||
target: Node | ||
targetIndex: number | ||
} | ||
|
||
export interface EdgePointerEvent extends MouseEvent<'edgePointer'> { | ||
target: Edge | ||
targetIndex: number | ||
} | ||
|
||
export interface AnnotationPointerEvent extends MouseEvent<'annotationPointer'> { | ||
target: Annotation | ||
targetIndex: number | ||
relatedPoint: RectPoint | null | ||
} | ||
|
||
export interface AnnotationDragEvent extends DragEvent<'annotationDrag'> { | ||
target: Annotation | ||
targetIndex: number | ||
} | ||
|
||
export interface AnnotationResizeEvent extends MouseEvent<'annotationResize'> { | ||
target: Annotation | ||
targetIndex: number | ||
relatedPoint: RectPoint | ||
} | ||
|
||
export interface ViewportPointerEvent extends MouseEvent<'viewportPointer'> { | ||
target: Viewport | ||
} | ||
|
||
export interface ViewportDragEvent extends DragEvent<'viewportDrag'> { | ||
target: Viewport | ||
} | ||
|
||
export interface ViewportDragDecelerateEvent extends EventBase<'viewportDragDecelerate'> { | ||
dx: number | ||
dy: number | ||
} | ||
|
||
export interface ViewportWheelEvent extends DragEvent<'viewportWheel'> { | ||
dz: number | ||
} | ||
|
||
export interface EventHandlers { | ||
onViewportPointerEnter?: (event: ViewportPointerEvent) => void | ||
onViewportPointerDown?: (event: ViewportPointerEvent) => void | ||
onViewportPointerMove?: (event: ViewportPointerEvent) => void | ||
onViewportDragStart?: (event: ViewportDragEvent | ViewportDragDecelerateEvent) => void | ||
onViewportDrag?: (event: ViewportDragEvent | ViewportDragDecelerateEvent) => void | ||
onViewportDragEnd?: (event: ViewportDragEvent | ViewportDragDecelerateEvent) => void | ||
onViewportPointerUp?: (event: ViewportPointerEvent) => void | ||
onViewportClick?: (event: ViewportPointerEvent) => void | ||
onViewportDoubleClick?: (event: ViewportPointerEvent) => void | ||
onViewportPointerLeave?: (event: ViewportPointerEvent) => void | ||
onViewportWheel?: (event: ViewportWheelEvent) => void | ||
onNodePointerEnter?: (event: NodePointerEvent) => void | ||
onNodePointerDown?: (event: NodePointerEvent) => void | ||
onNodeDragStart?: (event: NodeDragEvent) => void | ||
onNodeDrag?: (event: NodeDragEvent) => void | ||
onNodeDragEnd?: (event: NodeDragEvent) => void | ||
onNodePointerUp?: (event: NodePointerEvent) => void | ||
onNodeClick?: (event: NodePointerEvent) => void | ||
onNodeDoubleClick?: (event: NodePointerEvent) => void | ||
onNodePointerLeave?: (event: NodePointerEvent) => void | ||
onEdgePointerEnter?: (event: EdgePointerEvent) => void | ||
onEdgePointerDown?: (event: EdgePointerEvent) => void | ||
onEdgePointerUp?: (event: EdgePointerEvent) => void | ||
onEdgePointerLeave?: (event: EdgePointerEvent) => void | ||
onEdgeClick?: (event: EdgePointerEvent) => void | ||
onEdgeDoubleClick?: (event: EdgePointerEvent) => void | ||
} |
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,3 @@ | ||
export * from './objects' | ||
export * from './events' | ||
export * from './options' |
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,199 @@ | ||
// viewport | ||
export interface Bounds { | ||
left: number | ||
top: number | ||
right: number | ||
bottom: number | ||
} | ||
|
||
export interface Dimensions { | ||
width: number | ||
height: number | ||
} | ||
|
||
export interface Coordinates { | ||
x: number | ||
y: number | ||
} | ||
|
||
export interface Viewport extends Coordinates { | ||
zoom: number | ||
} | ||
|
||
export interface Graph { | ||
nodes: Node[] | ||
edges: Edge[] | ||
} | ||
|
||
// style | ||
export interface Stroke { | ||
color: string | ||
width: number | ||
} | ||
|
||
export interface FillStyle { | ||
color: string | ||
opacity?: number | ||
} | ||
|
||
// text | ||
export type FontWeight = 'normal' | 'bold' | 'bolder' | 'lighter' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900' | ||
|
||
export type TextAlign = 'left' | 'center' | 'right' | 'justify' | ||
|
||
export type FontStyle = 'normal' | 'italic' | 'oblique' | ||
|
||
export type AnchorPosition = 'bottom' | 'left' | 'top' | 'right' | ||
|
||
export interface TextHighlightStyle extends FillStyle { | ||
padding?: number | number[] | ||
} | ||
|
||
export interface TextStyle { | ||
fontName?: string | ||
fontSize?: number | ||
margin?: number | ||
wordWrap?: number | ||
letterSpacing?: number | ||
fontFamily?: string | ||
fontWeight?: FontWeight | ||
fontStyle?: FontStyle | ||
stroke?: Stroke | ||
color?: string | ||
align?: TextAlign | ||
anchor?: AnchorPosition | ||
highlight?: TextHighlightStyle | ||
} | ||
|
||
// icons | ||
interface IconBase<Type extends string> { | ||
type: Type | ||
offset?: Partial<Coordinates> | ||
} | ||
|
||
export interface TextIcon extends IconBase<'textIcon'> { | ||
content: string | ||
color: string | ||
/** | ||
* @default 10 | ||
*/ | ||
fontSize?: number | ||
/** | ||
* @default 'sans-serif' | ||
*/ | ||
fontFamily?: string | ||
/** | ||
* @default 'normal' | ||
*/ | ||
fontStyle?: FontStyle | ||
/** | ||
* @default 'normal' | ||
*/ | ||
fontWeight?: FontWeight | ||
} | ||
|
||
export interface ImageIcon extends IconBase<'imageIcon'> { | ||
url: string | ||
/** | ||
* @default 1 | ||
*/ | ||
scale?: number | ||
} | ||
|
||
export type IconStyle = TextIcon | ImageIcon | ||
|
||
// nodes | ||
export interface NodeBadge { | ||
position: number | ||
radius: number | ||
color: string | ||
icon?: IconStyle | ||
stroke?: Stroke | ||
} | ||
|
||
export interface NodeStyle { | ||
color?: string | ||
icon?: IconStyle | ||
stroke?: Stroke[] | ||
badge?: NodeBadge[] | ||
label?: TextStyle | ||
} | ||
|
||
export interface Node { | ||
id: string | ||
radius: number | ||
x?: number | ||
y?: number | ||
fx?: number | ||
fy?: number | ||
label?: string | ||
style?: NodeStyle | ||
subgraph?: Graph | ||
} | ||
|
||
// edges | ||
export type ArrowStyle = 'forward' | 'reverse' | 'both' | 'none' | ||
|
||
export interface EdgeLabelStyle extends TextStyle { | ||
anchor?: Exclude<AnchorPosition, 'left' | 'right'> | ||
} | ||
|
||
export interface EdgeStyle { | ||
/** | ||
* @default 1 | ||
*/ | ||
opacity?: number | ||
/** | ||
* @default 'none' | ||
*/ | ||
arrow?: ArrowStyle | ||
label?: EdgeLabelStyle | ||
stroke?: Stroke | ||
} | ||
|
||
export interface Edge { | ||
id: string | ||
source: string | ||
target: string | ||
label?: string | ||
style?: EdgeStyle | ||
} | ||
|
||
// annotations | ||
export interface AnnotationStyle { | ||
background?: FillStyle | ||
stroke?: Stroke[] | ||
} | ||
|
||
export interface TextAnnotationStyle extends AnnotationStyle { | ||
text?: Omit<TextStyle, 'anchor'> | ||
padding?: number | number[] | ||
} | ||
|
||
interface AnnotationType<Type extends string> { | ||
type: Type | ||
id: string | ||
x: number | ||
y: number | ||
resize?: boolean | ||
} | ||
|
||
export interface CircleAnnotation extends AnnotationType<'circle'> { | ||
radius: number | ||
style: AnnotationStyle | ||
} | ||
|
||
export interface RectangleAnnotation extends AnnotationType<'rectangle'> { | ||
height: number | ||
width: number | ||
style: AnnotationStyle | ||
} | ||
|
||
export interface TextAnnotation extends AnnotationType<'text'> { | ||
content: string | ||
height: number | ||
width: number | ||
style: TextAnnotationStyle | ||
} | ||
|
||
export type Annotation = CircleAnnotation | RectangleAnnotation | TextAnnotation |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am anticipating some push back against the use of interfaces over types here. When it comes to inheritance/sharing properties interfaces are by far the easier/cleaner approach. By leaning into inheritance we will be less likely to have inconsistencies in our type model
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably just my preference for functional programming over oop, but my assumption was that anything you can do w/ types you can do with interfaces. e.g.
With the notable difference that types can express both intersections
&
and unions|
. Typescript briefly notes the differences here, and honestly I like it that you can't mutate a type while you can mutate an interface (mutating an interface feels like a bug in waiting).It's worth noting that the creator of Typescript also created C#. C#, like Java, is heavily oriented towards oop, but has also incorporated a lot of functional concepts on top. What if they both (and Typescript...) had just started with a simple functional pattern, avoided oop, and cut the language's surface area in half...?