From 0aeb642c11d8334efeecaa495b74c33e56d6c318 Mon Sep 17 00:00:00 2001 From: Merlin Hermes <296129244+pinelliazz@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:26:03 +0800 Subject: [PATCH 1/2] Keep the editor over the next annotation after redraw A click can arrive after relayout replaces the annotation node. Open the editor only on the matching annotation that is still in the graph. --- draftlogs/PR_NUMBER_fix.md | 1 + src/components/annotations/draw.js | 31 +++++++++- test/jasmine/tests/annotations_test.js | 80 ++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 draftlogs/PR_NUMBER_fix.md diff --git a/draftlogs/PR_NUMBER_fix.md b/draftlogs/PR_NUMBER_fix.md new file mode 100644 index 00000000000..786399fceb1 --- /dev/null +++ b/draftlogs/PR_NUMBER_fix.md @@ -0,0 +1 @@ +- Keep the editor over the next annotation after the plot redraws [[#PR_NUMBER](https://github.com/plotly/plotly.js/pull/PR_NUMBER)], with thanks to @pinelliazz for the contribution! diff --git a/src/components/annotations/draw.js b/src/components/annotations/draw.js index 77c4e6ce861..5e5c2062b9f 100644 --- a/src/components/annotations/draw.js +++ b/src/components/annotations/draw.js @@ -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; @@ -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); } diff --git a/test/jasmine/tests/annotations_test.js b/test/jasmine/tests/annotations_test.js index b9d185c5bed..0875d7e9c58 100644 --- a/test/jasmine/tests/annotations_test.js +++ b/test/jasmine/tests/annotations_test.js @@ -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: 'Plot', showarrow: false}, From bffc94a6ab88bd8b064f1722707dfcd98622508b Mon Sep 17 00:00:00 2001 From: Merlin Hermes <296129244+pinelliazz@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:56:21 +0800 Subject: [PATCH 2/2] Set draftlog PR number to #8224 --- draftlogs/8224_fix.md | 1 + draftlogs/PR_NUMBER_fix.md | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 draftlogs/8224_fix.md delete mode 100644 draftlogs/PR_NUMBER_fix.md diff --git a/draftlogs/8224_fix.md b/draftlogs/8224_fix.md new file mode 100644 index 00000000000..258731a7d54 --- /dev/null +++ b/draftlogs/8224_fix.md @@ -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! diff --git a/draftlogs/PR_NUMBER_fix.md b/draftlogs/PR_NUMBER_fix.md deleted file mode 100644 index 786399fceb1..00000000000 --- a/draftlogs/PR_NUMBER_fix.md +++ /dev/null @@ -1 +0,0 @@ -- Keep the editor over the next annotation after the plot redraws [[#PR_NUMBER](https://github.com/plotly/plotly.js/pull/PR_NUMBER)], with thanks to @pinelliazz for the contribution!