-
-
Notifications
You must be signed in to change notification settings - Fork 898
feat: Add SVG engine support for draggable node helper lines #679
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
Open
UmbraCi
wants to merge
1
commit into
hizzgdev:master
Choose a base branch
from
UmbraCi:fix/draggable-line-svg
base: master
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
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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ | |
| }, | ||
| }, | ||
| view: { | ||
| engine: 'svg', | ||
| expander_style: 'char', | ||
| }, | ||
| }; | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -65,10 +65,14 @@ export class DraggableNode { | |
| this.jm = jm; | ||
| /** @type {DraggableNodeOptions} */ | ||
| this.options = opts; | ||
| /** @type {HTMLCanvasElement|null} */ | ||
| /** @type {boolean} */ | ||
| this.is_svg_engine = jm.view.opts.engine === 'svg'; | ||
| /** @type {HTMLCanvasElement|SVGSVGElement|null} */ | ||
| this.e_canvas = null; | ||
| /** @type {CanvasRenderingContext2D|null} */ | ||
| this.canvas_ctx = null; | ||
| /** @type {SVGPathElement|null} */ | ||
| this.helper_line = null; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改为 preview_line |
||
| /** @type {HTMLElement|null} */ | ||
| this.shadow = null; | ||
| /** @type {number} */ | ||
|
|
@@ -114,19 +118,43 @@ export class DraggableNode { | |
| this.create_shadow(); | ||
| this.event_bind(); | ||
| } | ||
| /** Resize canvas and shadow elements. */ | ||
| /** Resize canvas/SVG and shadow elements. */ | ||
| resize() { | ||
| this.jm.view.e_nodes.appendChild(this.shadow); | ||
| this.e_canvas.width = this.jm.view.size.w; | ||
| this.e_canvas.height = this.jm.view.size.h; | ||
| if (this.is_svg_engine) { | ||
| this.e_canvas.setAttribute('width', this.jm.view.size.w); | ||
| this.e_canvas.setAttribute('height', this.jm.view.size.h); | ||
| } else { | ||
| this.e_canvas.width = this.jm.view.size.w; | ||
| this.e_canvas.height = this.jm.view.size.h; | ||
| } | ||
| } | ||
| /** Create canvas for drawing drag lines. */ | ||
| /** Create canvas or SVG for drawing drag lines. */ | ||
| create_canvas() { | ||
| var c = $.c('canvas'); | ||
| this.jm.view.e_panel.appendChild(c); | ||
| var ctx = c.getContext('2d'); | ||
| this.e_canvas = c; | ||
| this.canvas_ctx = ctx; | ||
| if (this.is_svg_engine) { | ||
| // Create SVG element for helper lines | ||
| var svg = this._create_svg_element('svg'); | ||
| svg.setAttribute('class', 'jsmind-draggable-helper'); | ||
| svg.setAttribute('style', 'position: absolute; top: 0; left: 0; pointer-events: none;'); | ||
| this.jm.view.e_panel.appendChild(svg); | ||
| this.e_canvas = svg; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. svg 的容器换一个字段名吧 |
||
| } else { | ||
| // Create Canvas element for helper lines | ||
| var c = $.c('canvas'); | ||
| this.jm.view.e_panel.appendChild(c); | ||
| var ctx = c.getContext('2d'); | ||
| this.e_canvas = c; | ||
| this.canvas_ctx = ctx; | ||
| } | ||
| } | ||
| /** | ||
| * Create SVG element with proper namespace. | ||
| * @param {string} tag - SVG tag name | ||
| * @returns {SVGElement} | ||
| * @private | ||
| */ | ||
| _create_svg_element(tag) { | ||
| return $.d.createElementNS('http://www.w3.org/2000/svg', tag); | ||
| } | ||
| create_shadow() { | ||
| var s = $.c('jmnode'); | ||
|
|
@@ -171,20 +199,31 @@ export class DraggableNode { | |
| * @param {boolean} invalid - Whether current target is invalid | ||
| */ | ||
| magnet_shadow(shadow_p, node_p, invalid) { | ||
| this.canvas_ctx.lineWidth = this.options.line_width; | ||
| this.canvas_ctx.strokeStyle = invalid | ||
| ? this.options.line_color_invalid | ||
| : this.options.line_color; | ||
| this.canvas_ctx.lineCap = 'round'; | ||
| this.clear_lines(); | ||
| this.canvas_lineto(shadow_p.x, shadow_p.y, node_p.x, node_p.y); | ||
| var color = invalid ? this.options.line_color_invalid : this.options.line_color; | ||
|
|
||
| if (this.is_svg_engine) { | ||
| this.svg_draw_line(shadow_p.x, shadow_p.y, node_p.x, node_p.y, color); | ||
| } else { | ||
| this.canvas_ctx.lineWidth = this.options.line_width; | ||
| this.canvas_ctx.strokeStyle = color; | ||
| this.canvas_ctx.lineCap = 'round'; | ||
| this.canvas_lineto(shadow_p.x, shadow_p.y, node_p.x, node_p.y); | ||
| } | ||
| } | ||
| /** Clear helper lines from canvas. */ | ||
| /** Clear helper lines from canvas or SVG. */ | ||
| clear_lines() { | ||
| this.canvas_ctx.clearRect(0, 0, this.jm.view.size.w, this.jm.view.size.h); | ||
| if (this.is_svg_engine) { | ||
| if (this.helper_line && this.helper_line.parentNode) { | ||
| this.e_canvas.removeChild(this.helper_line); | ||
| this.helper_line = null; | ||
| } | ||
| } else { | ||
| this.canvas_ctx.clearRect(0, 0, this.jm.view.size.w, this.jm.view.size.h); | ||
| } | ||
| } | ||
| /** | ||
| * Draw a straight helper line. | ||
| * Draw a straight helper line on canvas. | ||
| * @param {number} x1 | ||
| * @param {number} y1 | ||
| * @param {number} x2 | ||
|
|
@@ -196,6 +235,60 @@ export class DraggableNode { | |
| this.canvas_ctx.lineTo(x2, y2); | ||
| this.canvas_ctx.stroke(); | ||
| } | ||
| /** | ||
| * Draw a helper line on SVG using bezier curve. | ||
| * Reuses the line drawing logic from SvgGraph. | ||
| * @param {number} x1 - Start x coordinate | ||
| * @param {number} y1 - Start y coordinate | ||
| * @param {number} x2 - End x coordinate | ||
| * @param {number} y2 - End y coordinate | ||
| * @param {string} color - Line color | ||
| */ | ||
| svg_draw_line(x1, y1, x2, y2, color) { | ||
| // Create SVG path element for helper line | ||
| this.helper_line = this._create_svg_element('path'); | ||
| this.helper_line.setAttribute('stroke', color); | ||
| this.helper_line.setAttribute('stroke-width', this.options.line_width); | ||
| this.helper_line.setAttribute('fill', 'transparent'); | ||
| this.helper_line.setAttribute('stroke-linecap', 'round'); | ||
|
|
||
| // Draw bezier curve (same as SvgGraph._bezier_to) | ||
| this._svg_bezier_to(this.helper_line, x1, y1, x2, y2); | ||
|
|
||
| // Add to SVG container | ||
| this.e_canvas.appendChild(this.helper_line); | ||
| } | ||
| /** | ||
| * Draw bezier curve to SVG path element. | ||
| * Reuses logic from SvgGraph._bezier_to. | ||
| * @param {SVGPathElement} path - SVG path element | ||
| * @param {number} x1 - Start x coordinate | ||
| * @param {number} y1 - Start y coordinate | ||
| * @param {number} x2 - End x coordinate | ||
| * @param {number} y2 - End y coordinate | ||
| * @private | ||
| */ | ||
| _svg_bezier_to(path, x1, y1, x2, y2) { | ||
| path.setAttribute( | ||
| 'd', | ||
| 'M ' + | ||
| x1 + | ||
| ' ' + | ||
| y1 + | ||
| ' C ' + | ||
| (x1 + ((x2 - x1) * 2) / 3) + | ||
| ' ' + | ||
| y1 + | ||
| ', ' + | ||
| x1 + | ||
| ' ' + | ||
| y2 + | ||
| ', ' + | ||
| x2 + | ||
| ' ' + | ||
| y2 | ||
| ); | ||
| } | ||
| /** Bind mouse/touch events for dragging. */ | ||
| event_bind() { | ||
| var jd = this; | ||
|
|
||
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.
在传入的 option 里加一个参数,用于控制是否启用这个 feature,默认不要启用。等过段时间你用着没啥问题再修改默认值为 true.
参数名可以是: inheritViewEngine=false