From 704e2ffb2e45429eb83cf305f9a43a0a6a391a42 Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Thu, 30 May 2019 18:10:09 +0200 Subject: [PATCH] Create bpmn-io typings --- .gitignore | 53 ++ .prettierignore | 3 + .prettierrc.json | 6 + .../bpmn-js-properties-panel/index.d.ts | 167 +++++ src/@types/bpmn-js/index.d.ts | 579 ++++++++++++++++++ src/@types/diagram-js/index.d.ts | 276 +++++++++ src/@types/moddle/index.d.ts | 96 +++ tsconfig.json | 11 + 8 files changed, 1191 insertions(+) create mode 100644 .gitignore create mode 100644 .prettierignore create mode 100644 .prettierrc.json create mode 100644 src/@types/bpmn-js-properties-panel/index.d.ts create mode 100644 src/@types/bpmn-js/index.d.ts create mode 100644 src/@types/diagram-js/index.d.ts create mode 100644 src/@types/moddle/index.d.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7c0ee09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,53 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea +*.iml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ +cmake-build-release/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..151fcf7 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +node_modules +target +package-lock.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..3750d8f --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "printWidth": 90, + "singleQuote": true, + "tabWidth": 2, + "endOfLine": "lf" +} diff --git a/src/@types/bpmn-js-properties-panel/index.d.ts b/src/@types/bpmn-js-properties-panel/index.d.ts new file mode 100644 index 0000000..24f7fd5 --- /dev/null +++ b/src/@types/bpmn-js-properties-panel/index.d.ts @@ -0,0 +1,167 @@ +declare module 'bpmn-js-properties-panel/lib/PropertiesActivator' { + import { Base, ModdleElement } from 'bpmn-js'; + import EventBus from 'diagram-js/lib/core/EventBus'; + + export default class PropertiesActivator { + constructor(eventBus: EventBus, priority?: number); + + isEntryVisible(entry: any, element: ModdleElement): boolean; + + isPropertyEditable(entry: any, propertyName: string, element: ModdleElement): boolean; + + getTabs(element: Base | ModdleElement): Tab[]; + } + + export interface Tab { + id: string; + label: string; + groups: Group[]; + enabled?: (element: Base | ModdleElement) => boolean; + } + + export interface Group { + id: string; + label: string; + entries: Entry[]; + enabled?: (element: Base | ModdleElement) => boolean; + } + + export interface Entry { + id: string; + description: string; + validate: (...args: any[]) => { id: string }; + html: string; + + get?(element: ModdleElement): any; + + set?(element: ModdleElement, values?: { [k: string]: any }): any; + } +} + +declare module 'bpmn-js-properties-panel/lib/factory/EntryFactory' { + import { Base, ModdleElement } from 'bpmn-js'; + import { Entry } from 'bpmn-js-properties-panel/lib/PropertiesActivator'; + + export default class EntryFactory { + static textField(options: TextOptions): Entry; + + static validationAwareTextField(options: ValidationTextOptions): Entry; + + static checkbox(options: CheckBoxOptions): Entry; + + static textBox(options: TextBoxOptions): Entry; + + static selectBox(options: SelectBoxOptions): Entry; + + static comboBox(options: ComboBoxOptions): Entry; + + static table(options: TableOptions): Entry; + + static label(options: LabelOptions): Entry; + + static link(options: LinkOptions): Entry; + } + + type Element = Base | ModdleElement; + + export interface Command { + cmd: string; + context: { + element: Element; + businessObject?: ModdleElement; + currentObject?: ModdleElement; + objectsToAdd?: any; + objectsToRemove?: any; + ny; + updatedObjectList?: any[]; + propertyName?: string; + referencePropertyName?: string; + properties?: { [k: string]: any }; + }; + } + + export interface Options { + id: string; + description?: string; + label?: string; + get?: (element: Element, node) => { formKey: any }; + set?: (element: Element, values, node /* opt */) => Command; + validate?: (element: Element, values: { [k: string]: any }) => any; + modelProperty?: string; + } + + export interface ValidationTextOptions { + getProperty: (element: Element) => string; + setProperty: (element: Element, properties?: { [k: string]: any }) => Command; + } + + export interface TextOptions extends Options { + buttonAction?: { + name: string; + method: () => any; + }; + buttonShow?: { + name: string; + method: () => any; + }; + disabled?: (element: Element) => boolean; + hidden?: (element: Element, node /* opt */) => { formKey: any }; + } + + export interface CheckBoxOptions extends Options { + disabled?: (element: Element) => boolean; + hidden?: (element: Element, node /* opt */) => { formKey: any }; + } + + export interface TextBoxOptions extends Options { + show?: (element: Element, node) => boolean; + } + + export interface SelectBoxOptions extends Options { + selectOptions: { + name: string; + value: string; + }[]; + emptyParameter?: boolean; + disabled?: (element: Element) => boolean; + hidden?: (element: Element, node /* opt */) => { formKey: any }; + } + + export interface ComboBoxOptions extends Options { + selectOptions: { + name: string; + value: string; + }[]; + emptyParameter?: boolean; + customValue?: string; + customName?: string; + disabled?: (element: Element) => boolean; + } + + export interface TableOptions extends Options { + modelProperties: string[]; + labels: string[]; + addLabel: string; + getElements: (element: Element, node) => ModdleElement[]; + addElement: (element: Element, node) => Command | Command[]; + updateElement: ( + element: Element, + value: any | null, + node, + idx: number + ) => Command | Command[]; + removeElement: (element: Element, node, idx: number) => Command | Command[]; + show?: (element: Element, node) => boolean; + } + + export interface LabelOptions extends Options { + labelText?: string; + divider?: boolean; + showLabel?: (element: Element, node) => boolean; + } + + export interface LinkOptions extends Options { + handleClick: (element: Element, node, event) => boolean; + showLink?: (element: Element, node) => boolean; + } +} diff --git a/src/@types/bpmn-js/index.d.ts b/src/@types/bpmn-js/index.d.ts new file mode 100644 index 0000000..bdd1699 --- /dev/null +++ b/src/@types/bpmn-js/index.d.ts @@ -0,0 +1,579 @@ +/** + * Typings for the bpmn-js project. + * + * @author Edoardo Luppi + */ +declare module 'bpmn-js' { + import Diagram from 'diagram-js'; + import Canvas from 'diagram-js/lib/core/Canvas'; + import ElementFactory from 'diagram-js/lib/core/ElementFactory'; + import ElementRegistry from 'diagram-js/lib/core/ElementRegistry'; + import EventBus from 'diagram-js/lib/core/EventBus'; + import translate from 'diagram-js/lib/i18n/translate/translate'; + import { Descriptor } from 'moddle/lib/descriptor-builder'; + import Moddle from 'moddle/lib/moddle'; + + export default class Viewer extends Diagram { + constructor(options?: ViewerOptions); + + get(service: T): Service; + + invoke(fn: (...v: any[]) => void | any[]): void; + + /** + * Parse and render a BPMN 2.0 diagram. + * + * Once finished the viewer reports back the result to the + * provided callback function with (err, warnings). + * + * ## Life-Cycle Events + * + * During import the viewer will fire life-cycle events: + * + * * import.parse.start (about to read model from xml) + * * import.parse.complete (model read; may have worked or not) + * * import.render.start (graphical import start) + * * import.render.complete (graphical import finished) + * * import.done (everything done) + * + * You can use these events to hook into the life-cycle. + * + * @param xml The BPMN 2.0 xml + * @param bpmnDiagram BPMN diagram or ID of diagram to render + * (if not provided, the first one will be rendered) + * @param done The completion callback + */ + importXML( + xml: string, + bpmnDiagram: BpmnDiDiagram | string, + done?: DoneCallback + ): void; + importXML(xml: string, done: DoneCallback): void; + + /** + * Open diagram of previously imported XML. + * + * Once finished the viewer reports back the result to the + * provided callback function with (err, warnings). + * + * ## Life-Cycle Events + * + * During switch the viewer will fire life-cycle events: + * + * * import.render.start (graphical import start) + * * import.render.complete (graphical import finished) + * + * You can use these events to hook into the life-cycle. + * + * @param diagram ID or the diagram to open + * @param done The completion callback + */ + open(diagram: BpmnDiDiagram | string, done?: DoneCallback); + open(done: DoneCallback): void; + + /** + * Export the currently displayed BPMN 2.0 diagram as + * a BPMN 2.0 XML document. + * + * ## Life-Cycle Events + * + * During XML saving the viewer will fire life-cycle events: + * + * * saveXML.start (before serialization) + * * saveXML.serialized (after xml generation) + * * saveXML.done (everything done) + * + * You can use these events to hook into the life-cycle. + * + * @param options Output formatted XML or/and preamble + * @param done The completion callback + */ + saveXML(options: WriterOptions, done: DoneCallback): void; + saveXML(done: DoneCallback): void; + + /** + * Export the currently displayed BPMN 2.0 diagram as + * an SVG image. + * + * ## Life-Cycle Events + * + * During SVG saving the viewer will fire life-cycle events: + * + * * saveSVG.start (before serialization) + * * saveSVG.done (everything done) + * + * You can use these events to hook into the life-cycle. + */ + saveSVG(options: WriterOptions, done: DoneCallback): void; + saveSVG(done: DoneCallback): void; + + /** + * Remove all drawn elements from the viewer. + * + * After calling this method the viewer can still + * be reused for opening another diagram. + */ + clear(): void; + + /** + * Destroy the viewer instance and remove all its + * remainders from the document tree. + */ + destroy(): void; + + /** + * Register an event listener. + * + * Remove a previously added listener via {@link #off(event, callback)}. + */ + on(event: T, priority: number, callback: Callback): void; + on(event: T, callback: Callback): void; + + /** + * De-register an event listener. + */ + off(events: Event | Event[], callback: Callback): void; + + attachTo(parentNode: string | Element): void; + + detach(): void; + } + + export interface ViewerOptions { + container?: string | Element; + width?: string | number; + height?: string | number; + position?: string; + deferUpdate?: boolean; + modules?: any[]; + additionalModules?: any[]; + moddleExtensions?: { [k: string]: any }; + propertiesPanel?: { + parent: string | Element; + }; + + [field: string]: any; + } + + export interface WriterOptions { + format?: boolean; + preamble?: boolean; + } + + export type DoneCallback = (err: any, warn: any[]) => void; + + /* Events types */ + + export type GenericEvent = 'attach' | 'detach'; + export type DiagramEvent = 'diagram.init' | 'diagram.destroy' | 'diagram.clear'; + export type ImportEvent = + | 'import.done' + | 'import.parse.start' // (about to read model from xml) + | 'import.parse.complete' // (model read; may have worked or not) + | 'import.render.start' // (graphical import start) + | 'import.render.complete' // (graphical import finished) + | 'import.done'; // (everything done) + export type SaveEvent = + | 'saveXML.start' // (before serialization) + | 'saveXML.serialized' // (after xml generation) + | 'saveXML.done'; // (everything done) + export type SvgEvent = + | 'saveSVG.start' // (before serialization) + | 'saveSVG.done'; // (everything done) + export type ElementEvent = + | 'element.changed' + | 'element.hover' + | 'element.out' + | 'element.click' + | 'element.dblclick' + | 'element.mousedown' + | 'element.mouseup'; + export type Event = + | GenericEvent + | DiagramEvent + | ElementEvent + | ImportEvent + | SaveEvent + | SvgEvent; + + export interface ElementObject { + element: Root; + gfx: SVGElement; + originalEvent: Event; + type: ElementEvent; + } + + export interface Base { + id: string; + type: string; + + /** + * Actual element that gets imported from BPMN 2.0 XML + * and serialized during export. + */ + businessObject: BpmnBaseElement; + label: Label; + parent: Shape; + labels: Label[]; + outgoingRefs: Connection[]; + incomingRefs: Connection[]; + } + + export interface Point { + x: number; + y: number; + original?: Point; + } + + export interface Connection extends Base { + source: Base; + target: Base; + waypoints?: Point[]; + } + + export interface Shape extends Base { + children: Base[]; + host: Shape; + attachers: Shape[]; + collapsed?: boolean; + hidden?: boolean; + width?: number; + height?: number; + x?: number; + y?: number; + } + + export interface Root extends Shape { + } + + export interface Label extends Shape { + labelTarget: Base; + } + + // prettier-ignore + export type CallbackObject = + T extends ElementEvent | SaveEvent ? ElementObject : any; + + export type ServiceName = + | 'eventBus' + | 'elementFactory' + | 'elementRegistry' + | 'canvas' + | 'moddle' + | 'translate'; + + export type Service = ServiceMap extends Record ? E : any; + + interface ServiceMap { + eventBus: EventBus; + elementRegistry: ElementRegistry; + elementFactory: ElementFactory; + canvas: Canvas; + moddle: Moddle; + translate: typeof translate; + } + + export type Callback = (e: CallbackObject) => void; + + /////// BPMN /////// + + export enum BpmnRelationshipDirection { + None = 'None', + Forward = 'Forward', + Backward = 'Backward', + Both = 'Both' + } + + export interface BpmnExtension { + mustUnderstand: boolean; + definition?: BpmnExtensionDefinition; + } + + export interface BpmnImport { + importType: string; + location?: string; + namespace: string; + } + + export interface BpmnExtensionDefinition { + name: string; + extensionAttributeDefinitions?: BpmnExtensionAttributeDefinition[]; + } + + export interface BpmnExtensionAttributeDefinition { + name: string; + type: string; + isReference?: boolean; + } + + export abstract class ModdleBase { + $model: Moddle; + $descriptor: Descriptor; + $instanceOf: (element: ModdleElement, type?: string) => boolean; + + get(name: string): any; + + set(name: string, value: any): void; + } + + export abstract class ModdleElement extends ModdleBase { + static $model: Moddle; + static $descriptor: Descriptor; + + readonly $type: string; + $attrs: any; + $parent: ModdleElement; + + [field: string]: any; + + static hasType(element: ModdleElement, type?: string): boolean; + } + + export abstract class BpmnBaseElement extends ModdleElement { + id: string; + documentation?: BpmnDocumentation; + extensionDefinitions?: BpmnExtensionDefinition[]; + extensionElements?: BpmnExtensionElements; + } + + export class BpmnExtensionElements extends ModdleElement { + readonly $type: 'bpmn:ExtensionElements'; + values?: ModdleElement[]; + valueRef?: Element; + extensionAttributeDefinition?: BpmnExtensionAttributeDefinition; + } + + export class BpmnDocumentation extends BpmnBaseElement { + readonly $type: 'bpmn:Documentation'; + text: string; + textFormat: string; + } + + export class BpmnStartEvent extends BpmnBaseElement { + readonly $type: 'bpmn:StartEvent'; + } + + export class RootElement extends BpmnBaseElement { + } + + export class BpmnRelationship extends BpmnBaseElement { + readonly $type: 'bpmn:Relationship'; + type?: string; + direction?: BpmnRelationshipDirection; + source?: Element[]; + target?: Element[]; + } + + export class BpmnDefinitions extends BpmnBaseElement { + readonly $type: 'bpmn:Definitions'; + name: string; + targetNamespace: string; + expressionLanguage?: string; + typeLanguage: string; + rootElements?: RootElement[]; + diagrams?: BpmnDiDiagram[]; + imports?: BpmnImport[]; + extensions?: BpmnExtension[]; + relationships?: BpmnRelationship[]; + exporter?: string; + exporterVersion?: string; + } + + /////// DI /////// + + export interface DiDiagram { + id: string; + rootElement: DiDiagramElement; + name: string; + documentation: string; + resolution: number; + ownedStyle: DiStyle[]; + } + + export interface DiDiagramElement { + id: string; + extension: DiExtension; + owningDiagram: DiDiagram; + owningElement: DiDiagramElement; + modelElement: Element; + style: DiStyle; + ownedElement: DiDiagramElement; + } + + export interface DiNode extends DiDiagramElement { + } + + export interface DiPlane extends DiNode { + planeElement: DiDiagramElement[]; + } + + export interface DiExtension { + values: Element[]; + } + + export interface DiStyle { + id: string; + } + + export interface DiEdge extends DiDiagramElement { + source?: DiDiagramElement; + target?: DiDiagramElement; + waypoint?: DcPoint[]; + } + + export interface DiShape extends DiNode { + bounds: DcBounds; + } + + export interface DiLabeledEdge extends DiEdge { + ownedLabel?: DiLabel; + } + + export interface DiLabeledShape extends DiShape { + ownedLabel?: DiLabel; + } + + export interface DiLabel extends DiNode { + bounds: DcBounds; + } + + /////// BPMNDI /////// + + export interface BpmnDiDiagram extends DiDiagram, ModdleElement { + $type: 'bpmndi:BPMNDiagram'; + plane: BpmnDiPlane; + labelStyle: BpmnDiLabelStyle[]; + } + + export interface BpmnDiEdge extends DiLabeledEdge, ModdleElement { + $type: 'bpmndi:BPMNEdge'; + bpmnElement: BpmnBaseElement; + } + + export interface BpmnDiPlane extends DiPlane, ModdleElement { + $type: 'bpmndi:BPMNPlane'; + bpmnElement: BpmnBaseElement; + } + + export interface BpmnDiShape extends DiLabeledShape, ModdleElement { + $type: 'bpmndi:BPMNShape'; + bpmnElement: BpmnBaseElement; + isHorizontal?: boolean; + isExpanded?: boolean; + isMarkerVisible?: boolean; + label: BpmnDiLabel; + isMessageVisible?: boolean; + participantBandKind?: BpmnDiParticipantBandKind; + choreographyActivityShape: BpmnDiShape; + } + + export interface BpmnDiLabel extends DiLabel, ModdleElement { + $type: 'bpmndi:BPMNLabel'; + labelStyle?: BpmnDiLabelStyle; + } + + export interface BpmnDiLabelStyle extends DiStyle { + font: DcFont; + } + + export enum BpmnDiParticipantBandKind { + TopInitiating = 'top_initiating', + MiddleInitiating = 'middle_initiating', + BottomInitiating = 'bottom_initiating', + TopNonInitiating = 'top_non_initiating', + MiddleNonInitiating = 'middle_non_initiating', + BottomNonInitiating = 'bottom_non_initiating' + } + + /////// DC /////// + + export interface DcBounds { + x: number; + y: number; + width?: number; + height?: number; + } + + export interface DcFont { + name?: string; + size?: number; + isBold?: boolean; + isItalic?: boolean; + isUnderline?: boolean; + isStrikeThrough?: boolean; + } + + export interface DcPoint { + x: number; + y: number; + } +} + +declare module 'bpmn-js/lib/Modeler' { + import Viewer, { DoneCallback, ViewerOptions } from 'bpmn-js'; + + export default class Modeler extends Viewer { + constructor(options?: ViewerOptions); + + /** + * Create a new diagram to start modeling. + */ + createDiagram(done: DoneCallback): void; + } +} + +declare module 'bpmn-js/lib/util/ModelUtil' { + import { Base, ModdleElement } from 'bpmn-js'; + + /** + * Is an element of the given BPMN type? + */ + export function is(element: Base | ModdleElement, type: string): boolean; + + /** + * Return the business object for a given element. + */ + export function getBusinessObject(element: Base | ModdleElement): ModdleElement; +} + +declare module 'bpmn-js/lib/features/modeling/BpmnFactory' { + import { + BpmnBaseElement, + BpmnDiEdge, + BpmnDiLabel, + BpmnDiPlane, + BpmnDiShape, + DcBounds, + DcPoint, + ModdleElement + } from 'bpmn-js'; + import { Attributes } from 'diagram-js/lib/core/ElementFactory'; + + export default class BpmnFactory { + constructor(moddle: any); + + create(type: string | object, attrs: Attributes): ModdleElement; + + createDiLabel(): BpmnDiLabel; + + createDiShape( + semantic: BpmnBaseElement, + bounds: object, + attrs: Attributes + ): BpmnDiShape; + + createDiBounds(bounds: object): DcBounds; + + createDiWaypoints(waypoints: DcPoint[]): DcPoint[]; + + createDiWaypoint(point: DcPoint): DcPoint; + + createDiEdge( + semantic: BpmnBaseElement, + waypoints: DcPoint[], + attrs: Attributes + ): BpmnDiEdge; + + createDiPlane(semantic: BpmnBaseElement): BpmnDiPlane; + } +} diff --git a/src/@types/diagram-js/index.d.ts b/src/@types/diagram-js/index.d.ts new file mode 100644 index 0000000..f077cb9 --- /dev/null +++ b/src/@types/diagram-js/index.d.ts @@ -0,0 +1,276 @@ +declare module 'diagram-js' { + import { Service, ServiceName, ViewerOptions } from 'bpmn-js'; + + export default class Diagram { + constructor(options?: ViewerOptions, injector?: any); + + get(service: T): Service; + + invoke(fn: (...v: any[]) => void | any[]): void; + + /** + * Destroys the diagram. + */ + destroy(): void; + + /** + * Clear the diagram, removing all contents. + */ + clear(): void; + } +} + +declare module 'diagram-js/lib/core/EventBus' { + import { Event, Shape } from 'bpmn-js'; + + export default class EventBus { + /** + * Register an event listener for events with the given name. + * + * The callback will be invoked with `event, ...additionalArguments` + * that have been passed to {@link EventBus#fire}. + * + * Returning false from a listener will prevent the events default action + * (if any is specified). To stop an event from being processed further in + * other listeners execute {@link Event#stopPropagation}. + * + * Returning anything but `undefined` from a listener will stop the listener propagation. + * + * @param events The events to listen to + * @param priority The priority in which this listener is called, larger is higher + */ + on( + events: string | string[], + priority: number, + callback: EventCallback, + that?: object + ): void; + + /** @see EventBus#on */ + on(events: string | string[], callback: EventCallback, that?: object): void; + + /** + * Register an event listener that is executed only once. + * + * @param event The event to listen to + * @param priority The priority in which this listener is called, larger is higher + */ + once(event: string, priority: number, callback: EventCallback): void; + + /** @see EventBus#once */ + once(event: string, callback: EventCallback): void; + + /** + * Removes event listeners by event and callback. + * + * If no callback is given, all listeners for a given event name are being removed. + */ + off(events: string | string[], callback?: EventCallback): void; + + /** + * Create an EventBus event. + */ + createEvent(data?: any): InternalEvent; + + /** + * Fires a named event. + * + * @example + * + * // fire event by name + * events.fire('foo'); + * + * // fire event object with nested type + * var event = { type: 'foo' }; + * events.fire(event); + * + * // fire event with explicit type + * var event = { x: 10, y: 20 }; + * events.fire('element.moved', event); + * + * // pass additional arguments to the event + * events.on('foo', function(event, bar) { + * alert(bar); + * }); + * + * events.fire({ type: 'foo' }, 'I am bar!'); + * + * @param name The event name + * @param event The event object + * @param data Additional arguments to be passed to the callback functions + * + * @return the events return value, if specified or false if the + * default action was prevented by listeners + */ + fire( + name: string | { type: string }, + event: InternalEvent | any, + ...data: any[] + ): any; + + handleError(error: any): boolean; + } + + export type EventCallback = (event: InternalEvent, data: object[]) => any | void; + + export interface InternalEvent { + element: Shape; + gfx: SVGElement; + originalEvent: MouseEvent; + type?: string; + + [field: string]: any; + + init(data: any): void; + + stopPropagation(): void; + + preventDefault(): void; + } +} + +declare module 'diagram-js/lib/core/ElementRegistry' { + import { Base } from 'bpmn-js'; + import EventBus from 'diagram-js/lib/core/EventBus'; + + export default class ElementRegistry { + constructor(eventBus: EventBus); + + /** + * Register a pair of (element, gfx, (secondaryGfx)). + */ + add(element: Base, gfx: SVGElement, secondaryGfx?: SVGElement): void; + + /** + * Removes an element from the registry. + */ + remove(element: string | Base): void; + + /** + * Update the id of an element + */ + updateId(element: string | Base, newId: string): void; + + /** + * Return the model element for a given id or graphics. + * + * @example + * elementRegistry.get('SomeElementId_1'); + * elementRegistry.get(gfx); + * + * @param filter The filter for selecting the element + */ + get(filter: string | SVGElement): Base; + + /** + * Return all elements that match a given filter function. + */ + filter(fn: (element: Base, gfx: SVGElement) => boolean): Base[]; + + /** + * Return all rendered model elements. + */ + getAll(): Base[]; + + /** + * Iterate over all diagram elements. + */ + forEach(fn: (element: Base, gfx: SVGElement) => any): void; + + /** + * Return the graphical representation of an element or its id. + * + * @example + * elementRegistry.getGraphics('SomeElementId_1'); + * elementRegistry.getGraphics(rootElement); // + * + * elementRegistry.getGraphics(rootElement, true); // + * + * + * @param filter The filter to use + * @param secondary Whether to return the secondary connected element + */ + getGraphics(filter: string | Base, secondary?: boolean): SVGElement; + } +} + +declare module 'diagram-js/lib/core/ElementFactory' { + import { Base, Connection, Label, Point, Root, Shape } from 'bpmn-js'; + + /** + * A factory for diagram-js shapes. + */ + export default class ElementFactory { + createRoot(attrs?: Attributes): Root; + + createLabel(attrs?: Attributes): Label; + + createShape(attrs?: Attributes): Shape; + + createConnection(attrs?: Attributes): Connection; + + create(type: string, attrs?: Attributes): Base; + } + + export interface Attributes { + id?: string; + x?: number; + y?: number; + width?: number; + height?: number; + collapsed?: boolean; + hidden?: boolean; + resizable?: boolean | 'maybe' | 'always'; + ignoreResize?: boolean; + waypoints?: Point[]; + source?: Shape; + target?: Shape; + host?: Shape; + alwaysTopLevel?: boolean; + level?: number; + labelTarget?: Base; + } +} + +declare module 'diagram-js/lib/core/Canvas' { + import { ViewerOptions } from 'bpmn-js'; + import ElementRegistry from 'diagram-js/lib/core/ElementRegistry'; + import EventBus from 'diagram-js/lib/core/EventBus'; + import GraphicsFactory from 'diagram-js/lib/core/GraphicsFactory'; + + export default class Canvas { + constructor( + config: ViewerOptions | null, + eventBus: EventBus, + graphicsFactory: GraphicsFactory, + elementRegistry: ElementRegistry + ); + } +} + +declare module 'diagram-js/lib/core/GraphicsFactory' { + import ElementRegistry from 'diagram-js/lib/core/ElementRegistry'; + import EventBus from 'diagram-js/lib/core/EventBus'; + + export default class GraphicsFactory { + constructor(eventBus: EventBus, elementRegistry: ElementRegistry); + } +} + +declare module 'diagram-js/lib/i18n/translate' { + import t from 'diagram-js/lib/i18n/translate/translate'; + + const exp: { + translate: ['value', typeof t]; + }; + + export default exp; +} + +declare module 'diagram-js/lib/i18n/translate/translate' { + // noinspection JSFunctionExpressionToArrowFunction + export default function translate( + template: string, + replacements?: { [id: string]: string } + ): string; +} diff --git a/src/@types/moddle/index.d.ts b/src/@types/moddle/index.d.ts new file mode 100644 index 0000000..836ed5e --- /dev/null +++ b/src/@types/moddle/index.d.ts @@ -0,0 +1,96 @@ +declare module 'moddle' { + export { default } from 'moddle/lib/moddle'; +} + +declare module 'moddle/lib/moddle' { + import { + BpmnDefinitions, + BpmnDocumentation, + BpmnExtensionElements, + BpmnRelationship, + BpmnStartEvent, + ModdleElement + } from 'bpmn-js'; + import { Attributes } from 'diagram-js/lib/core/ElementFactory'; + import { Descriptor } from 'moddle/lib/descriptor-builder'; + + export default class Moddle { + constructor(packages: { [ns: string]: Package }); + + create( + descriptor: T | Descriptor, + attrs?: Attributes + ): BpmnElement; + + getType( + descriptor: T | Descriptor + ): ModdleElementCtor; + } + + export interface Package { + associations: any[]; + enumerations?: any[]; + name: string; + prefix: string; + uri: string; + types: Type[]; + xml?: any; + } + + export interface Type { + name: string; + // TODO + } + + export type BpmnElementType = + | 'bpmn:ExtensionElements' + | 'bpmn:Documentation' + | 'bpmn:Definitions' + | 'bpmn:Relationship' + | 'bpmn:StartEvent'; + + type BpmnElement = BpmnElementMap extends Record + ? E + : ModdleElement; + + type ModdleElementCtor = new (attrs?: Attributes) => BpmnElement; + + interface BpmnElementMap { + 'bpmn:ExtensionElements': BpmnExtensionElements; + 'bpmn:Documentation': BpmnDocumentation; + 'bpmn:Definitions': BpmnDefinitions; + 'bpmn:Relationship': BpmnRelationship; + 'bpmn:StartEvent': BpmnStartEvent; + } +} + +declare module 'moddle/lib/ns' { + export interface NsName { + name: string; + prefix: string; + localName: string; + } +} + +declare module 'moddle/lib/descriptor-builder' { + import { NsName } from 'moddle/lib/ns'; + + export default class DescriptorBuilder { + constructor(nameNs: NsName); + + build(): Descriptor; + + // TODO + } + + export interface Descriptor { + ns: NsName; + name: string; + allTypes: any[]; + allTypesByName: object; + properties: any; + propertiesByName: object; + bodyProperty?: any; + idProperty?: any; + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..714580e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "module": "es6", + "target": "es2018", + "sourceMap": true, + "outDir": "build" + }, + "exclude": [ + "node_modules" + ] +}