drawio_dsl 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.builders/.templates/command.rb +8 -0
- data/.builders/.templates/schema_shape.rb +9 -0
- data/.builders/blueprint/shapes.rb +19 -0
- data/.builders/boot.rb +1 -0
- data/.builders/generators/25-themes.rb +3 -0
- data/CHANGELOG.md +7 -0
- data/lib/drawio_dsl/configuration.rb +36 -27
- data/lib/drawio_dsl/dom_builder.rb +15 -0
- data/lib/drawio_dsl/drawio.rb +19 -1
- data/lib/drawio_dsl/layout_engine.rb +1 -1
- data/lib/drawio_dsl/schema/_.rb +23 -0
- data/lib/drawio_dsl/schema/common_style.rb +42 -0
- data/lib/drawio_dsl/schema/default_palette.rb +31 -0
- data/lib/drawio_dsl/schema/diagram.rb +57 -0
- data/lib/drawio_dsl/schema/layouts/flex_layout.rb +87 -0
- data/lib/drawio_dsl/schema/layouts/grid_layout.rb +102 -0
- data/lib/drawio_dsl/schema/layouts/layout.rb +41 -0
- data/lib/drawio_dsl/schema/node.rb +53 -0
- data/lib/drawio_dsl/schema/page.rb +135 -0
- data/lib/drawio_dsl/schema/shapes/callout.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/circle.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/cloud.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/diamond.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/ellipse.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/hexagon.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/note.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/process.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/rectangle.rb +9 -0
- data/lib/drawio_dsl/schema/shapes/shape.rb +125 -0
- data/lib/drawio_dsl/schema/shapes/square.rb +9 -0
- data/lib/drawio_dsl/version.rb +1 -1
- data/lib/drawio_dsl.rb +1 -1
- data/package-lock.json +2 -2
- data/package.json +1 -1
- metadata +25 -3
- data/lib/drawio_dsl/schema.rb +0 -617
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56780c6c8fa8c1ea29a29e07bf389cf257fd7a33faf52e955b3fe56133367063
|
4
|
+
data.tar.gz: f583d40a64cea1177d7d52ab627dcbfa954cb3091dada0903947f69f5792d3b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92b5c4e8e2a8210c3e2213df96f1d35034689a3d2ff9132e17fc1fa30f68b89551cb395f725e0b1b5da3b506e62471e2e64def33683f8959f53a41a43e4f5805
|
7
|
+
data.tar.gz: 112eb4f963aa4c295559f8d1e8ed0eaf0121bb768282924ca54059c3de4924d16a33b5a3d33dd9044378315840aafda7db34d228aa2064606eccf5d29776da52
|
@@ -0,0 +1,19 @@
|
|
1
|
+
KManager.model :shapes, namespace: %i[domain] do
|
2
|
+
# microapp = import(:handlebars_helpers, :microapp)
|
3
|
+
|
4
|
+
table :shapes do
|
5
|
+
fields :type, :x, :y, :w, :h, :style_modifiers
|
6
|
+
|
7
|
+
# row :shape , x: 0, y: 0, w: 20, h: 20, style_modifiers: ''
|
8
|
+
row :square , x: 0, y: 0, w: 160, h: 160, style_modifiers: ''
|
9
|
+
row :rectangle , x: 0, y: 0, w: 200, h: 120, style_modifiers: ''
|
10
|
+
row :circle , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;ellipse'
|
11
|
+
row :process , x: 0, y: 0, w: 200, h: 120, style_modifiers: 'double=1;shape=process'
|
12
|
+
row :ellipse , x: 0, y: 0, w: 200, h: 120, style_modifiers: 'double=1;ellipse'
|
13
|
+
row :diamond , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;rhombus'
|
14
|
+
row :hexagon , x: 0, y: 0, w: 200, h: 120, style_modifiers: 'double=1;shape=hexagon'
|
15
|
+
row :cloud , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;shape=cloud'
|
16
|
+
row :note , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;shape=note'
|
17
|
+
row :callout , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;shape=callout'
|
18
|
+
end
|
19
|
+
end
|
data/.builders/boot.rb
CHANGED
@@ -8,6 +8,9 @@ KManager.action :bootstrap do
|
|
8
8
|
.page('Style-Plain', margin_left: 0, margin_top: 0) do
|
9
9
|
grid_layout(wrap_at: 5)
|
10
10
|
|
11
|
+
square(title: 'The quick brown fox jumps over the lazy dog')
|
12
|
+
circle(title: 'Mary had a little lamb')
|
13
|
+
|
11
14
|
KConfig.configuration.drawio.themes.keys.each do |theme|
|
12
15
|
random(title: theme.to_s, theme: theme)
|
13
16
|
end
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# [0.3.0](https://github.com/klueless-io/drawio_dsl/compare/v0.2.0...v0.3.0) (2022-03-04)
|
2
|
+
|
3
|
+
|
4
|
+
### Features
|
5
|
+
|
6
|
+
* add samples for direction, alignment, themes, styles and page margin ([2e9f735](https://github.com/klueless-io/drawio_dsl/commit/2e9f735ba50c9391d7dea64c565ca9c0fb61978a))
|
7
|
+
|
1
8
|
# [0.2.0](https://github.com/klueless-io/drawio_dsl/compare/v0.1.0...v0.2.0) (2022-03-04)
|
2
9
|
|
3
10
|
|
@@ -16,42 +16,33 @@ module DrawioDsl
|
|
16
16
|
include KLog::Logging
|
17
17
|
|
18
18
|
BaseStyle = Struct.new(:white_space, :html, :rounded, :shadow, :sketch, :glass, keyword_init: true)
|
19
|
-
|
20
|
-
ShapeDefaults = Struct.new(:type, :x, :y, :w, :h, :style_modifiers)
|
19
|
+
ShapeDefaults = Struct.new(:type, :x, :y, :w, :h, :style_modifiers, keyword_init: true)
|
21
20
|
ShapeThemeStyle = Struct.new(:fill_color, :stroke_color, :font_color, :gradient, keyword_init: true)
|
22
21
|
|
22
|
+
Shapes = Struct.new(
|
23
|
+
:shape,
|
24
|
+
:square,
|
25
|
+
:rectangle,
|
26
|
+
:circle,
|
27
|
+
:process,
|
28
|
+
:ellipse,
|
29
|
+
:diamond,
|
30
|
+
:hexagon,
|
31
|
+
:cloud,
|
32
|
+
:note,
|
33
|
+
:callout,
|
34
|
+
keyword_init: true
|
35
|
+
)
|
36
|
+
|
23
37
|
attr_accessor :base_style
|
24
38
|
|
25
|
-
# Theme colors
|
26
39
|
attr_accessor :themes
|
27
|
-
|
28
|
-
# Shape shapes
|
29
|
-
attr_accessor :shape
|
30
|
-
attr_accessor :square
|
31
|
-
attr_accessor :rectangle
|
32
|
-
attr_accessor :circle
|
33
|
-
attr_accessor :process
|
34
|
-
attr_accessor :ellipse
|
35
|
-
attr_accessor :diamond
|
36
|
-
attr_accessor :hexagon
|
37
|
-
|
38
|
-
# <mxCell id="6" value="" style="rhombus;whiteSpace=wrap;html=1;" vertex="1" parent="TqO-B">
|
39
|
-
# <mxGeometry x="-496" y="115" width="80" height="80" as="geometry"/>
|
40
|
-
# </mxCell>
|
40
|
+
attr_accessor :shapes
|
41
41
|
|
42
42
|
def initialize
|
43
43
|
@base_style = BaseStyle.new(white_space: :wrap, html: 1, rounded: nil, shadow: nil, sketch: nil, glass: nil)
|
44
44
|
|
45
|
-
|
46
|
-
@square = ShapeDefaults.new(:square , 0, 0, 160, 160, '')
|
47
|
-
@rectangle = ShapeDefaults.new(:rectangle , 0, 0, 200, 120, '')
|
48
|
-
@circle = ShapeDefaults.new(:circle , 0, 0, 160, 160, 'ellipse')
|
49
|
-
@process = ShapeDefaults.new(:process , 0, 0, 200, 120, 'shape=process')
|
50
|
-
@ellipse = ShapeDefaults.new(:ellipse , 0, 0, 200, 120, 'ellipse')
|
51
|
-
@diamond = ShapeDefaults.new(:diamond , 0, 0, 160, 160, 'rhombus')
|
52
|
-
@hexagon = ShapeDefaults.new(:hexagon , 0, 0, 200, 120, 'shape=hexagon')
|
53
|
-
|
54
|
-
@themes = {}
|
45
|
+
add_shapes
|
55
46
|
add_themes
|
56
47
|
end
|
57
48
|
|
@@ -65,12 +56,30 @@ module DrawioDsl
|
|
65
56
|
|
66
57
|
private
|
67
58
|
|
59
|
+
def add_shapes
|
60
|
+
@shapes = Shapes.new(
|
61
|
+
shape: ShapeDefaults.new(type: :shape , x: 0, y: 0, w: 20, h: 20, style_modifiers: ''),
|
62
|
+
square: ShapeDefaults.new(type: :square , x: 0, y: 0, w: 160, h: 160, style_modifiers: ''),
|
63
|
+
rectangle: ShapeDefaults.new(type: :rectangle , x: 0, y: 0, w: 200, h: 120, style_modifiers: ''),
|
64
|
+
circle: ShapeDefaults.new(type: :circle , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;ellipse'),
|
65
|
+
process: ShapeDefaults.new(type: :process , x: 0, y: 0, w: 200, h: 120, style_modifiers: 'double=1;shape=process'),
|
66
|
+
ellipse: ShapeDefaults.new(type: :ellipse , x: 0, y: 0, w: 200, h: 120, style_modifiers: 'double=1;ellipse'),
|
67
|
+
diamond: ShapeDefaults.new(type: :diamond , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;rhombus'),
|
68
|
+
hexagon: ShapeDefaults.new(type: :hexagon , x: 0, y: 0, w: 200, h: 120, style_modifiers: 'double=1;shape=hexagon'),
|
69
|
+
cloud: ShapeDefaults.new(type: :cloud , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;shape=cloud'),
|
70
|
+
note: ShapeDefaults.new(type: :note , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;shape=note'),
|
71
|
+
callout: ShapeDefaults.new(type: :callout , x: 0, y: 0, w: 160, h: 160, style_modifiers: 'double=1;shape=callout')
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
68
75
|
def add_theme(name, **opts)
|
69
76
|
@themes[name] = ShapeThemeStyle.new(**opts)
|
70
77
|
end
|
71
78
|
|
72
79
|
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
73
80
|
def add_themes
|
81
|
+
@themes = {}
|
82
|
+
|
74
83
|
add_theme(:style_01 , fill_color: '#f5f5f5', stroke_color: '#666666', font_color: '#333333')
|
75
84
|
add_theme(:style_02 , fill_color: '#dae8fc', stroke_color: '#6c8ebf', font_color: '#333333')
|
76
85
|
add_theme(:style_03 , fill_color: '#d5e8d4', stroke_color: '#82b366', font_color: '#333333')
|
@@ -119,6 +119,21 @@ module DrawioDsl
|
|
119
119
|
add_shape(add_hexagon)
|
120
120
|
end
|
121
121
|
|
122
|
+
def add_cloud(**opts)
|
123
|
+
cloud = DrawioDsl::Schema::Cloud.new(current_page, **opts)
|
124
|
+
add_shape(cloud)
|
125
|
+
end
|
126
|
+
|
127
|
+
def add_note(**opts)
|
128
|
+
note = DrawioDsl::Schema::Note.new(current_page, **opts)
|
129
|
+
add_shape(note)
|
130
|
+
end
|
131
|
+
|
132
|
+
def add_callout(**opts)
|
133
|
+
callout = DrawioDsl::Schema::Callout.new(current_page, **opts)
|
134
|
+
add_shape(callout)
|
135
|
+
end
|
136
|
+
|
122
137
|
def debug
|
123
138
|
puts JSON.pretty_generate(actions)
|
124
139
|
puts JSON.pretty_generate(diagram.to_h)
|
data/lib/drawio_dsl/drawio.rb
CHANGED
@@ -58,7 +58,7 @@ module DrawioDsl
|
|
58
58
|
|
59
59
|
# rubocop:disable Metrics/CyclomaticComplexity
|
60
60
|
def random(**opts)
|
61
|
-
case rand(
|
61
|
+
case rand(10)
|
62
62
|
when 0
|
63
63
|
square(**opts)
|
64
64
|
when 1
|
@@ -73,6 +73,12 @@ module DrawioDsl
|
|
73
73
|
diamond(**opts)
|
74
74
|
when 6
|
75
75
|
hexagon(**opts)
|
76
|
+
when 7
|
77
|
+
cloud(**opts)
|
78
|
+
when 8
|
79
|
+
note(**opts)
|
80
|
+
when 9
|
81
|
+
callout(**opts)
|
76
82
|
end
|
77
83
|
end
|
78
84
|
# rubocop:enable Metrics/CyclomaticComplexity
|
@@ -105,6 +111,18 @@ module DrawioDsl
|
|
105
111
|
builder.add_hexagon(**opts)
|
106
112
|
end
|
107
113
|
|
114
|
+
def cloud(**opts)
|
115
|
+
builder.add_cloud(**opts)
|
116
|
+
end
|
117
|
+
|
118
|
+
def note(**opts)
|
119
|
+
builder.add_note(**opts)
|
120
|
+
end
|
121
|
+
|
122
|
+
def callout(**opts)
|
123
|
+
builder.add_callout(**opts)
|
124
|
+
end
|
125
|
+
|
108
126
|
private
|
109
127
|
|
110
128
|
def current_shape
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'common_style'
|
4
|
+
require_relative 'default_palette'
|
5
|
+
require_relative 'diagram'
|
6
|
+
require_relative 'node'
|
7
|
+
require_relative 'page'
|
8
|
+
|
9
|
+
require_relative 'layouts/layout'
|
10
|
+
require_relative 'layouts/flex_layout'
|
11
|
+
require_relative 'layouts/grid_layout'
|
12
|
+
|
13
|
+
require_relative 'shapes/shape'
|
14
|
+
require_relative 'shapes/callout'
|
15
|
+
require_relative 'shapes/circle'
|
16
|
+
require_relative 'shapes/cloud'
|
17
|
+
require_relative 'shapes/diamond'
|
18
|
+
require_relative 'shapes/ellipse'
|
19
|
+
require_relative 'shapes/hexagon'
|
20
|
+
require_relative 'shapes/note'
|
21
|
+
require_relative 'shapes/process'
|
22
|
+
require_relative 'shapes/rectangle'
|
23
|
+
require_relative 'shapes/square'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DrawioDsl
|
4
|
+
module Schema
|
5
|
+
# Common Style is the reused on Diagram, Page and Shape
|
6
|
+
#
|
7
|
+
# When styles are not provided at each level, then they will inherit from
|
8
|
+
# the parent common style.
|
9
|
+
#
|
10
|
+
# Shapes will use the common style of their page
|
11
|
+
class CommonStyle
|
12
|
+
attr_accessor :white_space
|
13
|
+
attr_accessor :html
|
14
|
+
attr_accessor :rounded
|
15
|
+
attr_accessor :shadow
|
16
|
+
attr_accessor :glass
|
17
|
+
attr_accessor :sketch
|
18
|
+
|
19
|
+
def initialize(**args, &block)
|
20
|
+
@white_space = args[:white_space]
|
21
|
+
@html = args[:html]
|
22
|
+
@rounded = args[:rounded]
|
23
|
+
@shadow = args[:shadow]
|
24
|
+
@sketch = args[:sketch]
|
25
|
+
@glass = args[:glass]
|
26
|
+
|
27
|
+
instance_eval(&block) if block_given?
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_h
|
31
|
+
{
|
32
|
+
white_space: white_space,
|
33
|
+
html: html,
|
34
|
+
rounded: rounded,
|
35
|
+
shadow: shadow,
|
36
|
+
sketch: sketch,
|
37
|
+
glass: glass
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DrawioDsl
|
4
|
+
module Schema
|
5
|
+
# Default Palette contains palette information that can be inherited at each level
|
6
|
+
class DefaultPalette
|
7
|
+
attr_accessor :fill_color
|
8
|
+
attr_accessor :stroke_color
|
9
|
+
attr_accessor :font_color
|
10
|
+
attr_accessor :gradient
|
11
|
+
|
12
|
+
def initialize(owner, **args, &block)
|
13
|
+
@fill_color = args[:fill_color]
|
14
|
+
@stroke_color = args[:stroke_color]
|
15
|
+
@font_color = args[:font_color]
|
16
|
+
@gradient = args[:gradient]
|
17
|
+
|
18
|
+
instance_exec(owner, &block) if block_given?
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_h
|
22
|
+
{
|
23
|
+
fill_color: fill_color,
|
24
|
+
stroke_color: stroke_color,
|
25
|
+
font_color: font_color,
|
26
|
+
gradient: gradient
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DrawioDsl
|
4
|
+
module Schema
|
5
|
+
# Diagram is the root of the schema, it contains pages
|
6
|
+
class Diagram
|
7
|
+
attr_accessor :host
|
8
|
+
attr_accessor :theme
|
9
|
+
attr_accessor :style
|
10
|
+
attr_accessor :palette
|
11
|
+
attr_accessor :pages
|
12
|
+
|
13
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
14
|
+
def initialize(**args)
|
15
|
+
@host = args[:host] || SecureRandom.alphanumeric(3)
|
16
|
+
|
17
|
+
# Apply a random theme to the diagram if none is specified.
|
18
|
+
@theme = args[:theme] || KConfig.configuration.drawio.random_theme
|
19
|
+
|
20
|
+
@style = DrawioDsl::Schema::CommonStyle.new(**args) do
|
21
|
+
default_style = KConfig.configuration.drawio.base_style
|
22
|
+
|
23
|
+
# Inherit from configured style when specific style not specified.
|
24
|
+
@white_space ||= default_style.white_space
|
25
|
+
@html ||= default_style.html
|
26
|
+
@rounded ||= default_style.rounded
|
27
|
+
@shadow ||= default_style.shadow
|
28
|
+
@sketch ||= default_style.sketch
|
29
|
+
@glass ||= default_style.glass
|
30
|
+
end
|
31
|
+
|
32
|
+
@palette = DrawioDsl::Schema::DefaultPalette.new(self, **args) do |diagram|
|
33
|
+
theme_palette = KConfig.configuration.drawio.palette(diagram.theme)
|
34
|
+
|
35
|
+
# Inherit from theme when specific palette options are not specified.
|
36
|
+
@fill_color ||= theme_palette.fill_color
|
37
|
+
@stroke_color ||= theme_palette.stroke_color
|
38
|
+
@font_color ||= theme_palette.font_color
|
39
|
+
@gradient ||= theme_palette.gradient
|
40
|
+
end
|
41
|
+
|
42
|
+
@pages = args[:pages] || []
|
43
|
+
end
|
44
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
45
|
+
|
46
|
+
def to_h
|
47
|
+
{
|
48
|
+
host: host,
|
49
|
+
theme: theme,
|
50
|
+
palette: palette.to_h,
|
51
|
+
style: style.to_h,
|
52
|
+
pages: pages.map(&:to_h)
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DrawioDsl
|
4
|
+
module Schema
|
5
|
+
# Provides flex style layouts
|
6
|
+
class FlexLayout < Layout
|
7
|
+
attr_accessor :direction
|
8
|
+
attr_accessor :wrap_at
|
9
|
+
attr_reader :gap
|
10
|
+
attr_reader :perpendicular_max
|
11
|
+
|
12
|
+
def initialize(page, **args)
|
13
|
+
@type = :flex_layout
|
14
|
+
@direction = args[:direction] || :horizontal
|
15
|
+
@wrap_at = args[:wrap_at] || (direction == :horizontal ? 1000 : 800)
|
16
|
+
@gap = args[:gap] || 20
|
17
|
+
@perpendicular_max = 0
|
18
|
+
|
19
|
+
super(page, **args)
|
20
|
+
end
|
21
|
+
|
22
|
+
def position_shape(shape)
|
23
|
+
fire_after_init
|
24
|
+
|
25
|
+
position_shape_horizontally(shape) if direction == :horizontal
|
26
|
+
position_shape_vertically(shape) if direction == :vertical
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_h
|
30
|
+
fire_after_init
|
31
|
+
|
32
|
+
super.merge(
|
33
|
+
direction: direction,
|
34
|
+
wrap_at: wrap_at,
|
35
|
+
boundary: boundary,
|
36
|
+
perpendicular_max: perpendicular_max
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# rubocop:disable Metrics/AbcSize
|
43
|
+
def position_shape_horizontally(shape)
|
44
|
+
boundary_violation = page.position_x + shape.w + gap > boundary
|
45
|
+
|
46
|
+
if boundary_violation
|
47
|
+
page.position_x = anchor_x
|
48
|
+
page.position_y += perpendicular_max
|
49
|
+
end
|
50
|
+
|
51
|
+
shape.x = page.position_x
|
52
|
+
shape.y = page.position_y
|
53
|
+
|
54
|
+
return if boundary_violation
|
55
|
+
|
56
|
+
page.position_x += shape.w + gap
|
57
|
+
@perpendicular_max = [perpendicular_max, shape.h].max
|
58
|
+
end
|
59
|
+
# rubocop:enable Metrics/AbcSize
|
60
|
+
|
61
|
+
# rubocop:disable Metrics/AbcSize
|
62
|
+
def position_shape_vertically(shape)
|
63
|
+
boundary_violation = page.position_y + shape.h + gap > boundary
|
64
|
+
|
65
|
+
if boundary_violation
|
66
|
+
page.position_y = anchor_y
|
67
|
+
page.position_x += perpendicular_max
|
68
|
+
end
|
69
|
+
|
70
|
+
shape.x = page.position_x
|
71
|
+
shape.y = page.position_y
|
72
|
+
|
73
|
+
return if boundary_violation
|
74
|
+
|
75
|
+
page.position_y += shape.h + gap
|
76
|
+
@perpendicular_max = [perpendicular_max, shape.w].max
|
77
|
+
end
|
78
|
+
# rubocop:enable Metrics/AbcSize
|
79
|
+
|
80
|
+
def boundary
|
81
|
+
return @boundary if defined? @boundary
|
82
|
+
|
83
|
+
@boundary = wrap_at + (direction == :horizontal ? anchor_x : anchor_y)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DrawioDsl
|
4
|
+
module Schema
|
5
|
+
# Provides grid style layouts
|
6
|
+
class GridLayout < Layout
|
7
|
+
attr_accessor :direction
|
8
|
+
attr_accessor :wrap_at
|
9
|
+
attr_accessor :grid_size
|
10
|
+
attr_accessor :cell_no
|
11
|
+
attr_accessor :h_align
|
12
|
+
attr_accessor :v_align
|
13
|
+
|
14
|
+
def initialize(page, **args)
|
15
|
+
@type = :grid_layout
|
16
|
+
@direction = args[:direction] || :horizontal
|
17
|
+
@wrap_at = args[:wrap_at] || 5
|
18
|
+
@grid_size = args[:grid_size] || 220
|
19
|
+
@h_align = args[:h_align] || :center
|
20
|
+
@v_align = args[:v_align] || :center
|
21
|
+
@cell_no = 1
|
22
|
+
|
23
|
+
super(page, **args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def position_shape(shape)
|
27
|
+
fire_after_init
|
28
|
+
|
29
|
+
shape.x = horizontal_shape_alignment(shape)
|
30
|
+
shape.y = vertical_shape_alignment(shape)
|
31
|
+
|
32
|
+
# puts '------------------'
|
33
|
+
# puts "cell: #{cell_no}"
|
34
|
+
# puts "wrap_at: #{wrap_at}"
|
35
|
+
# puts "shape-x: #{shape.x}"
|
36
|
+
# puts "shape-y: #{shape.y}"
|
37
|
+
# puts "page-x: #{page.position_x}"
|
38
|
+
# puts "page-y: #{page.position_y}"
|
39
|
+
# puts "anchor-x: #{anchor_x}"
|
40
|
+
# puts "anchor-y: #{anchor_y}"
|
41
|
+
|
42
|
+
move_cell_horizontally if direction == :horizontal
|
43
|
+
move_cell_vertically if direction == :vertical
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_h
|
47
|
+
super.merge(
|
48
|
+
direction: direction,
|
49
|
+
wrap_at: wrap_at,
|
50
|
+
grid_size: grid_size,
|
51
|
+
cell_no: cell_no
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
# rubocop:disable Metrics/AbcSize
|
58
|
+
def horizontal_shape_alignment(shape)
|
59
|
+
return page.position_x + ((grid_size - shape.w) / 2) if h_align == :center
|
60
|
+
return page.position_x + (grid_size - shape.w) if h_align == :right
|
61
|
+
|
62
|
+
page.position_x
|
63
|
+
end
|
64
|
+
# rubocop:enable Metrics/AbcSize
|
65
|
+
|
66
|
+
# rubocop:disable Metrics/AbcSize
|
67
|
+
def vertical_shape_alignment(shape)
|
68
|
+
return page.position_y + ((grid_size - shape.h) / 2) if v_align == :center || v_align == :middle
|
69
|
+
return page.position_y + (grid_size - shape.h) if v_align == :bottom
|
70
|
+
|
71
|
+
page.position_y
|
72
|
+
end
|
73
|
+
# rubocop:enable Metrics/AbcSize
|
74
|
+
|
75
|
+
def move_cell_horizontally
|
76
|
+
if cell_no < wrap_at
|
77
|
+
page.position_x += grid_size
|
78
|
+
@cell_no += 1
|
79
|
+
return
|
80
|
+
end
|
81
|
+
|
82
|
+
# Flow down to the next row
|
83
|
+
page.position_x = anchor_x
|
84
|
+
page.position_y += grid_size
|
85
|
+
@cell_no = 1
|
86
|
+
end
|
87
|
+
|
88
|
+
def move_cell_vertically
|
89
|
+
if cell_no < wrap_at
|
90
|
+
page.position_y += grid_size
|
91
|
+
@cell_no += 1
|
92
|
+
return
|
93
|
+
end
|
94
|
+
|
95
|
+
# Flow right to the next column
|
96
|
+
page.position_y = anchor_y
|
97
|
+
page.position_x += grid_size
|
98
|
+
@cell_no = 1
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DrawioDsl
|
4
|
+
module Schema
|
5
|
+
# Provides base configuration for automatic layouts
|
6
|
+
class Layout < Node
|
7
|
+
attr_accessor :type
|
8
|
+
|
9
|
+
# represents the x coordinate of the top left corner layout area
|
10
|
+
# this coordinate is based on the current location of the page
|
11
|
+
attr_accessor :anchor_x
|
12
|
+
attr_accessor :anchor_y
|
13
|
+
|
14
|
+
def initialize(page, **args)
|
15
|
+
@after_init_fired = false
|
16
|
+
|
17
|
+
super(page, **args.merge(classification: :layout_rule))
|
18
|
+
end
|
19
|
+
|
20
|
+
def fire_after_init
|
21
|
+
return if @after_init_fired
|
22
|
+
|
23
|
+
@after_init_fired = true
|
24
|
+
after_init
|
25
|
+
end
|
26
|
+
|
27
|
+
def after_init
|
28
|
+
@anchor_x ||= page.position_x
|
29
|
+
@anchor_y ||= page.position_y
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_h
|
33
|
+
super.merge(
|
34
|
+
type: type,
|
35
|
+
anchor_x: anchor_x,
|
36
|
+
anchor_y: anchor_y
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|