Skip to content

Commit

Permalink
Option to show first tag as arrow label
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimon committed Sep 22, 2022
1 parent 752c609 commit e4defbf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
4 changes: 3 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ <h1>Homer: The Odyssey</h1>
image: 'hallstatt'
});

var connections = recogito.Connections([ r, a ]);
var connections = recogito.Connections([ r, a ], {
showLabels: true
});

connections.on('createConnection', function(c) {
console.log('created', c);
Expand Down
6 changes: 4 additions & 2 deletions src/NetworkCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ const isHandle = element =>

export default class NetworkCanvas extends EventEmitter {

constructor(instances) {
constructor(instances, config) {
super();

// List of RecogitoJS/Annotorious instances
this.instances = instances;

this.config = config;

this.svg = SVG().addTo('body');
this.svg.attr('class', 'r6o-connections-canvas');

Expand All @@ -42,7 +44,7 @@ export default class NetworkCanvas extends EventEmitter {
}

addEdge = edge => {
const svgEdge = new SVGEdge(edge, this.svg);
const svgEdge = new SVGEdge(edge, this.svg, this.config);

svgEdge.on('click', () =>
this.emit('selectConnection', edge.toAnnotation(), svgEdge.midpoint));
Expand Down
15 changes: 15 additions & 0 deletions src/NetworkCanvas.scss
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,19 @@ svg.r6o-connections-canvas {

}

.r6o-connections-edge-label {

rect {
fill: rgba(255, 255, 255, 0.8);
stroke: rgba(0, 0, 0, 0.65);
}

text {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}

}

}
2 changes: 1 addition & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ConnectionsPlugin extends EventEmitter {
instances : [ instances ];

// Single network canvas, covering the browser viewport
this.canvas = new NetworkCanvas(this.instances);
this.canvas = new NetworkCanvas(this.instances, config);

this.instances.forEach(this.patchInstance);

Expand Down
30 changes: 29 additions & 1 deletion src/svg/SVGEdge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ARROW_CONFIG } from './Config';
*/
export default class SVGEdge extends EventEmitter {

constructor(edge, svg) {
constructor(edge, svg, config) {
super();

this.edge = edge;
Expand Down Expand Up @@ -40,6 +40,29 @@ export default class SVGEdge extends EventEmitter {
this.g.polygon('0,-6 12,0, 0,6')
.attr('class', 'r6o-connections-edge-head');

const firstTag = edge.bodies
.find(b => b.purpose === 'tagging');

if (firstTag && config.showLabels) {
const label = this.g.group()
.attr('class', 'r6o-connections-edge-label');

const rect = label.rect();

const text = label.text(firstTag.value)
.attr('y', 4.5);

const { width, height } = text.node.getBBox();

rect
.attr('x', -5.5)
.attr('y', - Math.round(height / 2) - 1.5)
.attr('rx', 2)
.attr('ry', 2)
.attr('width', Math.round(width) + 10)
.attr('height', Math.round(height) + 4);
}

// Initial render
this.redraw();
}
Expand Down Expand Up @@ -77,6 +100,11 @@ export default class SVGEdge extends EventEmitter {
this.g.find('polygon')
.attr('transform', `translate(${ex},${ey}) rotate(${endAngleAsDegrees})`);

// Label (if any)
const label = this.g.find('.r6o-connections-edge-label');
if (label)
label.attr('transform', `translate(${cx},${cy})`);

// Expose essential anchor points
this.startpoint = { x: sx, y: sy };
this.midpoint = { x: cx, y: cy };
Expand Down

0 comments on commit e4defbf

Please sign in to comment.