SequenceDiagram/library.htm

915 lines
23 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="content-security-policy" content="
base-uri 'self';
default-src 'none';
script-src 'self' 'unsafe-inline';
style-src 'self'
https://fonts.googleapis.com
'sha256-s7UPtBgvov5WNF9C1DlTZDpqwLgEmfiWha5a5p/Zn7E='
;
font-src 'self' data: https://fonts.gstatic.com;
img-src 'self';
form-action 'none';
">
<meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0">
<title>Sequence Diagram Library</title>
<link rel="icon" href="web/resources/favicon.png">
<link rel="apple-touch-icon" href="web/resources/apple-touch-icon.png">
<link href="https://fonts.googleapis.com/css?family=Vollkorn" rel="stylesheet">
<link rel="stylesheet" href="web/styles/library.css">
<!-- DEV
<script src="node_modules/codemirror/lib/codemirror.js"></script>
<link rel="stylesheet" href="node_modules/codemirror/lib/codemirror.css">
<script src="node_modules/codemirror/mode/xml/xml.js"></script>
<script src="node_modules/codemirror/mode/javascript/javascript.js"></script>
<script src="node_modules/codemirror/addon/runmode/runmode.js"></script>
<script src="node_modules/codemirror/addon/runmode/colorize.js"></script>
<script src="scripts/standalone-web.mjs" type="module"></script>
/DEV -->
<!-- LIVE -->
<link rel="stylesheet" href="web/lib/codemirror.css">
<script src="web/lib/codemirror.js"></script>
<script src="web/lib/cm-addons-library.js"></script>
<script src="lib/sequence-diagram-web.min.js"></script>
<!-- /LIVE -->
<script src="web/resources/library.js"></script>
</head>
<body>
<article>
<header>
<h1>Sequence Diagram</h1>
<pre class="sequence-diagram">
define Complex System as sys
define User as usr
define Sequence Diagram as diagram
define Other Users as usr2
begin sys, usr, usr2
sys ~> usr
note over usr: "Take time to\nunderstand System"
usr -> *diagram: Create
usr -> usr2: Inform
usr2 <-> diagram: Learn &amp; understand
usr2 -> sys: Use
terminators box
</pre>
</header>
<h2 id="Introduction">Introduction</h2>
<p>
Want to draw a Sequence Diagram?
<a href="." target="_blank" rel="noopener">Go to the online editor</a>.
</p>
<p>
This library renders sequence diagrams from code. It is
<a href="https://github.com/davidje13/SequenceDiagram" target="_blank" rel="noopener">open-source</a>
(LGPL-3.0), and including it in a website is as simple as adding the script:</p>
<pre data-lang="text/html">
&lt;script src="lib/sequence-diagram-web.min.js"&gt;&lt;/script&gt;
</pre>
<p>Or if you are using requirejs:</p>
<pre data-lang="javascript">
requirejs(['lib/sequence-diagram-web.min'], (SequenceDiagram) => {
SequenceDiagram.convertAll();
});
</pre>
<p>If you are using NPM to manage dependencies, you can install with <code>npm install --save svg-sequence-diagram</code></p>
<p>
Any element with the class <code>sequence-diagram</code> will automatically be
converted when the page loads:
</p>
<div class="right">
<pre class="sequence-diagram" data-lang="sequence">
A -> B: foo
B -> A: bar
</pre>
</div>
<pre data-lang="text/html" class="print-reduced">
&lt;pre class="sequence-diagram"&gt;
A -&gt; B: foo
B -&gt; A: bar
&lt;/pre&gt;
</pre>
<h2 id="Language">Language</h2>
<section>
<h3 id="ConnectionTypes">Connection Types</h3>
<pre class="example" data-lang="sequence">
title Connection Types
begin Foo, Bar, Baz
Foo -> Bar: Simple arrow
Bar --> Baz: Dashed arrow
Foo <- Bar: Reversed arrow
Bar <-- Baz: Reversed & dashed
Foo <-> Bar: Double arrow
Bar <--> Baz: Double dashed arrow
# An arrow with no label:
Foo -> Bar
Bar ->> Baz: Different arrow
Foo <<--> Bar: Mix of arrows
Bar -> Bar: Bar talks to itself
Foo -> +Bar: Foo asks Bar
-Bar --> Foo: and Bar replies
Bar -x Baz: Lost message
# Arrows leaving on the left and right of the diagram
[ -> Foo: From the left
[ <- Foo: To the left
Foo -> ]: To the right
Foo <- ]: From the right
[ ~> ]: Wavy left to right!
# (etc.)
</pre>
</section>
<section>
<h3 id="Dividers">Dividers</h3>
<pre class="example" data-lang="sequence">
title Dividers
begin Foo, Bar, Baz
Foo -> +Bar
divider
Bar -> +Baz
divider delay: This takes a while
-Baz --> Bar
divider tear with height 20: Lots of stuff happens
-Bar --> Foo
</pre>
</section>
<section>
<h3 id="NotesState">Notes &amp; State</h3>
<pre class="example" data-lang="sequence">
title Note Placements
begin Foo, Bar
note over Foo: Foo says something
note left of Foo: Stuff
note right of Bar: More stuff
note over Foo, Bar: "Foo and Bar
on multiple lines"
note between Foo, Bar: Link
text right: "Comments\nOver here!"
state over Foo: Foo is ponderous
</pre>
</section>
<section>
<h3 id="Logic">Logic</h3>
<pre class="example" data-lang="sequence">
title At the Bank
begin Person, ATM, Bank
Person -> ATM: Request money
ATM -> Bank: Check funds
if fraud detected
Bank -> Police: "Get 'em!"
Police -> Person: "You're nicked"
end Police
else if sufficient funds
ATM -> Bank: Withdraw funds
repeat until "all requested money
has been handed over"
ATM -> Person: Dispense note
end
else
ATM -> Person: Error
end
</pre>
</section>
<section>
<h3 id="LabelTemplates">Label Templates</h3>
<pre class="example" data-lang="sequence">
autolabel "[&lt;inc>] &lt;label>"
begin "Underpants\nGnomes" as A
A <- ]: Collect underpants
A <-> ]: ???
A <- ]: Profit!
</pre>
</section>
<section>
<h3 id="MultilineText">Multiline Text</h3>
<pre class="example" data-lang="sequence">
title "My Multiline
Title"
begin Foo, Bar
note over Foo: "Also possible\nwith escapes"
Foo -> Bar: "Lines of text\non this arrow"
if "Even multiline\ninside conditions like this"
Foo -> "Multiline\nagent"
end
state over Foo: "Newlines here,
too!"
</pre>
</section>
<section>
<h3 id="Themes">Themes</h3>
<pre class="example" data-lang="sequence">
theme sketch
title Mockup
A thing -> +Another thing: Something happens
-Another thing --> A thing: With some response
note right of Another thing: Find out what happens here
end Another thing
</pre>
<h4>Available themes</h4>
<ul>
<li>basic</li>
<li>monospace</li>
<li>chunky</li>
<li>sketch</li>
<li>sketch left handed</li>
</ul>
</section>
<section>
<h3 id="ShortLivedAgents">Short-Lived Agents</h3>
<pre class="example" data-lang="sequence">
title "Baz doesn't live long"
note over Foo, Bar: Using begin / end
begin Baz
Bar -> Baz
Baz -> Foo
end Baz
note over Foo, Bar: Using * / !
# * and ! cause agents to be
# created and destroyed inline
Bar -> *Baz: make Baz
Foo <- !Baz: end Baz
# Foo and Bar end with black bars
terminators bar
# (options are: box, bar, cross, fade, none)
</pre>
</section>
<section>
<h3 id="AgentAliases">Agent Aliases</h3>
<pre class="example" data-lang="sequence">
define My complicated agent name as A
define "Another agent name,
and this one's multi-line!" as B
A -> B: this is much easier
A <- B: than writing the whole name
</pre>
</section>
<section>
<h3 id="Markdown">Markdown</h3>
<pre class="example" data-lang="sequence">
define "Name with
**bold** and _italic_" as A
define "Also `code`
and ~strikeout~" as B
A -> B: "_**basic markdown
is supported!**_"
</pre>
</section>
<section>
<h3 id="AlternativeAgentOrdering">Alternative Agent Ordering</h3>
<pre class="example" data-lang="sequence">
define Baz, Foo
Foo -> Bar
Bar -> Baz
</pre>
</section>
<section>
<h3 id="AgentTypes">Agent Types</h3>
<pre class="example" data-lang="sequence">
begin User, Application, PostgreSQL
User -> +Application
Application -> +PostgreSQL
-PostgreSQL --> Application
-Application --> User
User is a person
PostgreSQL is a database
</pre>
</section>
<section>
<h3 id="AsynchronousCommunication">Asynchronous Communication</h3>
<pre class="example" data-lang="sequence">
begin Initiator as I, Receiver as R
# the '...id' syntax allows connections
# to span multiple lines
I -> ...fin1
...fin1 -> R: FIN
# they can even inter-mix!
R -> ...ack1
R -> ...fin2
...ack1 -> I: ACK
...fin2 -> I: FIN
!I -> ...ack2
...ack2 -> !R: ACK
</pre>
</section>
<section>
<h3 id="LanguageMore">More</h3>
<p>
More features are supported. See the
<a href="." target="_blank" rel="noopener">online editor</a>&rsquo;s library and autocomplete
features to discover them.
</p>
</section>
<h2 id="BrowserSupport" class="no-forced-break">Browser Support</h2>
<p>
This has been tested in the latest versions of Google Chrome, Mozilla Firefox,
and Apple Safari. Versions of Microsoft Internet Explorer / Edge have not been
tested and probably won&rsquo;t work. Any bugs found in a supported browser
should be reported in the
<a href="https://github.com/davidje13/SequenceDiagram/issues" target="_blank" rel="noopener">Issue Tracker</a>.
</p>
<h2 id="API">API</h2>
<p>
For more advanced usage, an API is available:
</p>
<div id="hold1" class="right"></div>
<pre data-lang="javascript" class="print-reduced">
var diagram = new SequenceDiagram();
diagram.set('A -> B\nB -> A');
document.body.appendChild(diagram.dom());
// Highlight elements created in line 1 (0-based)
diagram.setHighlight(1);
</pre>
<h3 id="API_Constructor">Constructor</h3>
<pre data-lang="javascript">
diagram = new SequenceDiagram(code, options);
diagram = new SequenceDiagram(code);
diagram = new SequenceDiagram(options);
diagram = new SequenceDiagram();
</pre>
<p>
Creates a new SequenceDiagram object. Options is an object which can contain:
</p>
<ul>
<li><code>code</code>: Alternative way of specifying code, instead of using a
separate argument.</li>
<li><code>container</code>: DOM node to append the diagram to (defaults to
null).</li>
<li><code>document</code>: Document object to base the diagram in (defaults to
container's document, or <code>window.document</code>).</li>
<li><code>textSizerFactory</code>: Function which returns an object capable of
measuring text (defaults to wrapping <code>getComputedTextLength</code>).</li>
<li><code>themes</code>: List of themes to make available to the diagram
(defaults to globally registered themes).</li>
<li><code>namespace</code>: Each diagram on a page must have a unique namespace.
By default a unique namespace is generated, but if you want something specific,
enter it here.</li>
<li><code>interactive</code>: If <code>true</code>, will automatically call
<code>addInteractivity</code> when constructing the diagram.</li>
</ul>
<h3 id="API_clone">.clone</h3>
<pre data-lang="javascript">
newDiagram = diagram.clone(options);
newDiagram = diagram.clone();
</pre>
<p>
Creates a copy of the diagram. If options are given, they will override the
current diagram&rsquo;s state (options is passed to the constructor of the new
object, so all the same options are available). Note that by default, the
<code>namespace</code> will not be cloned (a new unique namespace will be
generated). Also the <code>container</code> will not be cloned; you will need
to specify one explicitly or manually attach the new diagram at a later point.
</p>
<h3 id="API_set">.set</h3>
<pre data-lang="javascript">
diagram.set(code);
</pre>
<p>
Changes the code for the diagram and causes a re-render.
</p>
<h3 id="API_process">.process</h3>
<pre data-lang="javascript">
processed = diagram.process(code);
</pre>
<p>
Processes the given code but does not render it. Causes no change to the
diagram object. This is mostly useful for debounced rendering with immediate
error notifications. The resulting object can be passed to
<a href="#API_render"><code>render</code></a> at a later point.
</p>
<h3 id="API_render">.render</h3>
<pre data-lang="javascript">
diagram.render();
diagram.render(processed);
</pre>
<p>
Forces a re-render of the diagram. Typically this happens automatically.
Optionally, the result of an earlier call to
<a href="#API_process"><code>process</code></a> can be provided.
</p>
<h3 id="API_addTheme">.addTheme</h3>
<pre data-lang="javascript">
diagram.addTheme(theme);
</pre>
<p>
Make a new theme available to the diagram. Any unrecognised themes are replaced
with the default theme.
</p>
<p>
The theme API has not been finalised yet, so this method is not typically
useful.
</p>
<h3 id="API_getThemeNames">.getThemeNames</h3>
<pre data-lang="javascript">
names = diagram.getThemeNames();
</pre>
<p>
Returns a list of names of themes which are available to this diagram. These
can be specified in a <code>theme &lt;name&gt;</code> line in the code.
</p>
<h3 id="API_getThemes">.getThemes</h3>
<pre data-lang="javascript">
themes = diagram.getThemes();
</pre>
<p>
Returns a list of themes which are available to this diagram.
</p>
<h3 id="API_addInteractivity">.addInteractivity</h3>
<div class="right">
<pre class="sequence-diagram" data-lang="sequence" data-sd-interactive>
begin A, B
if bored
A -> +B
-B --> A
end
if still bored
A -> +B
-B --> A
end
</pre>
</div>
<pre data-lang="javascript">
diagram.addInteractivity();
</pre>
<p>
Makes the rendered diagram interactive. Currently this means adding a click
listener to any groups which causes them to collapse / expand. Try clicking on
the example to the right.
</p>
<p>The example here has CSS styling applied:</p>
<pre data-lang="css">
.region.collapsed,
.region.expanded {
cursor: pointer;
user-select: none;
}
.region.collapsed:hover .outline,
.region.expanded:hover .outline {
fill: rgba(255, 128, 0, 0.5);
}
</pre>
<p>It is also possible to enable interactivity using a HTML attribute:</p>
<pre data-lang="text/html">
&lt;pre class="sequence-diagram" data-sd-interactive&gt;
A -&gt; +B
if something
-B --&gt; A
end
&lt;/pre&gt;
</pre>
<h3 id="API_getSVGSynchronous">.getSVGSynchronous</h3>
<pre data-lang="javascript">
svgURL = diagram.getSVGSynchronous(options);
</pre>
<p>
Returns a blob URL which contains the SVG code for the current diagram.
</p>
<p>The options can include:</p>
<ul>
<li><code>size</code>: Object containing optional width, height or zoom to
specify in the SVG</code>.</li>
</ul>
<h3 id="API_getSVG">.getSVG</h3>
<pre data-lang="javascript">
diagram.getSVG(options).then(({url, latest}) => { ... });
</pre>
<p>
Asynchronous version of
<a href="#API_getSVGSynchronous"><code>getSVGSynchronous</code></a>. This is
provided for compatibility with <a href="#API_getPNG"><code>getPNG</code></a>,
which has no synchronous equivalent.
</p>
<p>The options can include:</p>
<ul>
<li><code>size</code>: Object containing optional width, height or zoom to
specify in the SVG</code>.</li>
</ul>
<p>The callback receives an object containing:</p>
<ul>
<li><code>url</code>: The URL of the generated blob.</li>
<li><code>latest</code>: True if the URL given is still valid; subsequent calls
to this method will invalidate previous URLs.</li>
</ul>
<h3 id="API_getPNG">.getPNG</h3>
<pre data-lang="javascript">
diagram.getPNG(options).then(({url, latest}) => { ... });
</pre>
<p>
Generates a PNG image and returns a blob URL.
</p>
<p>The options can include:</p>
<ul>
<li><code>resolution</code>: Desired pixels-per-unit.</li>
<li><code>size</code>: Object containing optional width, height or zoom to
render the image at (defaults to the unit size of the diagram). The final
output will have size <code>width * resolution</code> &times;
<code>height * resolution</code>.</li>
</ul>
<p>The callback receives an object containing:</p>
<ul>
<li><code>url</code>: The URL of the generated blob.</li>
<li><code>latest</code>: True if the URL given is still valid; subsequent calls
to this method will invalidate previous URLs.</li>
</ul>
<h3 id="API_getSize">.getSize</h3>
<pre data-lang="javascript">
size = diagram.getSize();
</pre>
<p>
Returns an object containing <code>width</code> and <code>height</code>
properties, corresponding to the size of the diagram in units.
</p>
<h3 id="API_getTitle">.getTitle</h3>
<pre data-lang="javascript">
title = diagram.getTitle();
</pre>
<p>
Returns the document title as a string. All formatting is lost, and
newlines are replaced with spaces. Returns a blank string if no title is set.
</p>
<h3 id="API_setContainer">.setContainer</h3>
<pre data-lang="javascript">
diagram.setContainer(node);
</pre>
<p>
Same as calling <code>node.appendChild(diagram.dom())</code>.
</p>
<h3 id="API_dom">.dom</h3>
<pre data-lang="javascript">
node = diagram.dom();
</pre>
<p>
Returns the base SVG element which the diagram has been rendered into.
</p>
<h3 id="API_setHighlight">.setHighlight</h3>
<pre data-lang="javascript">
diagram.setHighlight(line);
diagram.setHighlight();
</pre>
<p>
Marks elements generated by the specified line with a "focus" CSS class, which
can be used to style them. Only one line can be highlighted at a time. Calling
with no parameter (or <code>null</code>) will remove the highlighting.
</p>
<p>
The outline effect seen in the editor can be achieved by targetting
<code>.focus .outline</code>. All elements contain an element with the class
"outline", which defines a simple shape surrounding the entire element. For
example:
</p>
<pre data-lang="css">
.region.focus .outline {
stroke-width: 5px;
stroke: rgba(255, 128, 0, 0.5);
}
</pre>
<h3 id="API_setCollapsed">.setCollapsed</h3>
<pre data-lang="javascript">
diagram.setCollapsed(line, collapsed, options);
diagram.setCollapsed(line, collapsed);
</pre>
<p>
Marks the given line as collapsed or non-collapsed. If an element defined at
that line can be collapsed, it will be modified during the next render. Returns
true if a change occurred, or false if the line already had the requested state.
</p>
<p><code>line</code> can also be an array of lines.</p>
<p>
By default, calling this method will trigger an automatic render (unless called
as a no-op). This can be disabled by passing <code>{render: false}</code> in the
options argument.
</p>
<h3 id="API_isCollapsed">.isCollapsed</h3>
<pre data-lang="javascript">
collapsed = diagram.isCollapsed(line);
</pre>
<p>
Returns true if the given line is marked as collapsed, regardless of whether
that line being collapsed has a meaningful impact on the rendered document.
</p>
<h3 id="API_collapse">.collapse</h3>
<pre data-lang="javascript">
diagram.collapse(line, options);
diagram.collapse(line);
</pre>
<p>
Shorthand for <code>.setCollapsed(line, true, options)</code>.
</p>
<h3 id="API_expand">.expand</h3>
<pre data-lang="javascript">
diagram.expand(line, options);
diagram.expand(line);
</pre>
<p>
Shorthand for <code>.setCollapsed(line, false, options)</code>.
</p>
<h3 id="API_toggleCollapsed">.toggleCollapsed</h3>
<pre data-lang="javascript">
diagram.toggleCollapsed(line, options);
diagram.toggleCollapsed(line);
</pre>
<p>
Toggles the given line&rsquo;s collapsed status by calling
<code>.setCollapsed</code>.
</p>
<h3 id="API_expandAll">.expandAll</h3>
<pre data-lang="javascript">
diagram.expandAll(options);
diagram.expandAll();
</pre>
<p>
Marks all lines as non-collapsed. Returns true if a change occurred, or false
if all lines were already non-collapsed.
</p>
<p>
By default, calling this method will trigger an automatic render (unless called
as a no-op). This can be disabled by passing <code>{render: false}</code> in the
options argument.
</p>
<h3 id="API_addEventListener">.addEventListener</h3>
<pre data-lang="javascript">
diagram.addEventListener(type, callback);
</pre>
<p>
Registers an event listener. The available events are:</p>
<ul>
<li><code>mouseover</code>: called when the mouse enters a region of the diagram
(e.g. a note).</li>
<li><code>mouseout</code>: called when the mouse leaves a region of the
diagram.</li>
<li><code>click</code>: called when the user clicks on a region of the
diagram.</li>
<li><code>dblclick</code>: called when the user double-clicks on a region of the
diagram.</li>
<li><code>render</code>: called when the diagram finishes rendering. Receives
the sequence diagram object as an argument.</li>
</ul>
<p>All mouse events are invoked with a single parameter: the element. This
object contains:</p>
<ul>
<li><code>ln</code>: the line number of the source code which defined the
element.</li>
<li><code>type</code>: a string representing the type of the element.</li>
<li>Other fields which define the element (exact fields vary by type).</li>
</ul>
<h3 id="API_removeEventListener">.removeEventListener</h3>
<pre data-lang="javascript">
diagram.removeEventListener(type, callback);
</pre>
<p>
Removes a previously registered event listener.
</p>
<h3 id="API_removeAllEventListeners">.removeAllEventListeners</h3>
<pre data-lang="javascript">
diagram.removeAllEventListeners(type);
diagram.removeAllEventListeners();
</pre>
<p>
Removes all registered event listeners for a given type, or all event listeners
for all types.
</p>
<h2 id="SimilarTools">Similar Tools</h2>
<p>
There are quite a lot of sequence diagram drawing tools available. If this one
doesn&rsquo;t fit your needs for whatever reason, you might want to take a look
at the alternatives: (but also feel free to raise feature requests in the
<a href="https://github.com/davidje13/SequenceDiagram/issues" target="_blank" rel="noopener">issue tracker</a>!)
</p>
<ul>
<li><a href="https://www.websequencediagrams.com/" target="_blank" rel="noopener">websequencediagrams.com</a>
is a commercial offering which inspired the syntax of this project and many
others. It has a wide range of themes but limited functionality. Also offers a
hosted image generation service.</li>
<li><a href="https://github.com/bramp/js-sequence-diagrams" target="_blank" rel="noopener">js-sequence-diagrams</a>
is a Javascript library which has wider browser support via snap.svg but very
limited functionality.</li>
<li><a href="https://github.com/knsv/mermaid" target="_blank" rel="noopener">mermaid</a>
is a Javascript library and online editor which can render multiple types of UML
diagram, including simple sequence diagrams.</li>
<li><a href="https://github.com/aivarsk/scruffy" target="_blank" rel="noopener">Scruffy UML</a>
is a Python library which can render multiple types of UML diagram, including
simple sequence diagrams, from the commandline. Uses yUML syntax rather than the
human friendly syntax used by most other projects.</li>
<li><a href="https://github.com/plantuml/plantuml" target="_blank" rel="noopener">PlantUML</a>
is a Java project and online editor which can render multiple types of UML
diagram, including sequence diagrams. Has a good range of features and allows a
high degree of customisation (e.g. diagram colours).</li>
</ul>
<h2 id="Thanks">Thanks</h2>
<p>
Thanks to the other tools mentioned above for inspiring parts of the syntax of
this project, and to
<a href="https://fonts.google.com/specimen/Handlee" target="_blank" rel="noopener">the Handlee font</a>
which is used in the sketch theme.
</p>
<pre class="sequence-diagram">
theme sketch
begin User, SequenceDiagram as SD, Parser, Generator, Renderer
User -> +SD: code
SD -> +Parser: code
-Parser --> SD: parsed
SD -> +Generator: parsed
-Generator --> SD: generated
-SD -> +Renderer: generated
-Renderer -> *DOM: SVG
User <~> DOM: interaction
terminators box
</pre>
<nav>
<a href="." target="_blank" rel="noopener">Online Editor</a><a href="https://github.com/davidje13/SequenceDiagram" target="_blank" rel="noopener">GitHub</a>
</nav>
</article>
</body>
</html>