Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/8224_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix annotation editor positioning when switching between annotations during editing [[#8224](https://github.com/plotly/plotly.js/pull/8224)], with thanks to @pinelliazz for the contribution!
31 changes: 28 additions & 3 deletions src/components/annotations/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
var getUpdateObj = editHelpers.getUpdateObj;

// remove the existing annotation if there is one
fullLayout._infolayer
.selectAll('.' + className + '[data-index="' + index + '"]')
.remove();
const annotationSelector = `.${className}[data-index="${index}"]`;
fullLayout._infolayer.selectAll(annotationSelector).remove();

var annClipID = 'clip' + fullLayout._uid + '_ann' + index;

Expand Down Expand Up @@ -753,5 +752,31 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {

Registry.call('_guiRelayout', gd, getUpdateObj());
});

const startEdit = annTextGroupInner.on('click');
annTextGroupInner.on('click', null).on('click.annotationEdit', () => {
if(gd.contains(annTextGroupInner.node())) {
startEdit();
return;
}

// The previous editor can redraw the plot before the click arrives.
const currentLayout = gd._fullLayout;
if(!currentLayout) return;
const current = Lib.nestedProperty(currentLayout, containerStr).get()?.[index];
if(!current) return;

// A plot event can replace the input before the new defaults reach the SVG.
const liveInput = Lib.nestedProperty(gd.layout, containerStr).get()?.[index];
if(options._input._templateitemname) {
if(liveInput || !current._input?._templateitemname || current._template !== options._template) return;
} else if(liveInput !== options._input || current._input !== options._input) return;

const currentGroup = currentLayout._infolayer
.select(`${annotationSelector} .annotation-text-g > g`);
if(currentGroup.empty() || !gd.contains(currentGroup.node())) return;
const startCurrentEdit = currentGroup.on('click.annotationEdit');
if(startCurrentEdit) startCurrentEdit();
});
} else annText.call(textLayout);
}
80 changes: 80 additions & 0 deletions test/jasmine/tests/annotations_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,86 @@ describe('annotation effects', function() {
.then(done, done.fail);
});

for(const useTemplate of [false, true]) {
const source = useTemplate ? 'template' : 'layout';
it(`keeps the editor over the next ${source} annotation after autorange redraws the plot`, async () => {
gd = createGraphDiv();

const first = {x: 30, y: 70, text: 'First', showarrow: false};
const second = {x: 70, y: 30, text: 'Second', showarrow: false};
const template = useTemplate ? {layout: {annotations: [{...second, name: 'second'}]}} : undefined;
await Plotly.newPlot(gd, [{x: [0, 100], y: [0, 100], mode: 'markers'}], {
xaxis: {autorange: true},
yaxis: {autorange: true},
width: 500,
height: 500,
margin: {l: 100, r: 100, t: 100, b: 100, pad: 0},
annotations: useTemplate ? [first] : [first, second],
template
}, {editable: true});

const clickAnnotation = index => {
const text = gd.querySelector(`.annotation[data-index="${index}"] .annotation-text`);
const {left, top, width, height} = text.getBoundingClientRect();
const x = left + width / 2;
const y = top + height / 2;
mouseEvent('mousedown', x, y, {buttons: 1});
mouseEvent('mouseup', x, y);
};

clickAnnotation(0);
const firstEditor = document.activeElement;
expect(firstEditor.textContent).toBe('First');
firstEditor.textContent = 'First edited';
const originalSecond = gd.querySelector('.annotation[data-index="1"] .annotation-text');

clickAnnotation(1);
const secondText = gd.querySelector('.annotation[data-index="1"] .annotation-text');
const secondEditor = document.activeElement;
expect(secondText).not.toBe(originalSecond);
expect(secondEditor.textContent).toBe('Second');
expect(gd.layout.annotations[0].text).toBe('First edited');
expect(secondText.style.opacity).toBe('0');
const textBox = secondText.getBoundingClientRect();
const editorBox = secondEditor.getBoundingClientRect();
expect(Math.abs(editorBox.left - textBox.left)).toBeLessThan(3);
expect(Math.abs(editorBox.top - textBox.top)).toBeLessThan(3);

secondEditor.textContent = 'Second edited';
secondEditor.blur();
expect(gd.layout.annotations[1].text).toBe('Second edited');
});
}

it('ignores an annotation edit click when its entire info layer is detached', async () => {
gd = createGraphDiv();

await Plotly.newPlot(gd, [{x: [0, 100], y: [0, 100], mode: 'markers'}], {
annotations: [{x: 30, y: 70, text: 'Only', showarrow: false}]
}, {editable: true});

const layer = gd._fullLayout._infolayer.node();
const textGroup = layer.querySelector('.annotation-text-g > g');
const errors = [];
const recordError = event => {
errors.push(event.message);
event.preventDefault();
event.stopImmediatePropagation();
};

layer.remove();
window.addEventListener('error', recordError, true);
try {
textGroup.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true}));
} finally {
window.removeEventListener('error', recordError, true);
}

expect(errors).toEqual([]);
expect(gd.querySelector('.plugin-editable')).toBeNull();
expect(gd.layout.annotations[0].text).toBe('Only');
});

it('makes the whole text box a link if the link is the whole text', function(done) {
makePlot([
{x: 20, y: 20, text: '<a href="https://plotly.com">Plot</a>', showarrow: false},
Expand Down
Loading