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
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { describe, it, expect, vi } from 'vitest';
import { mount } from '@vue/test-utils';
import { defineComponent, ref, KeepAlive } from 'vue';
import Toolbar from './Toolbar.vue';

const ToolbarKeepAliveHost = defineComponent({
components: { KeepAlive, Toolbar },
setup() {
const visible = ref(true);
return { visible };
},
template: '<KeepAlive><Toolbar v-if="visible" /></KeepAlive>',
});

function createMockToolbar() {
return {
config: {
toolbarGroups: ['left', 'center', 'right'],
toolbarButtonsExclude: [],
},
getToolbarItemByGroup: () => [],
getToolbarItemByName: () => null,
onToolbarResize: vi.fn(),
emitCommand: vi.fn(),
overflowItems: [],
activeEditor: null,
};
}

describe('Toolbar', () => {
it('removes resize and keydown listeners on unmount (not only on KeepAlive deactivate)', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test only checks the unmount path. the whole point of this fix is that both paths matter — could you add one that hides the toolbar inside <KeepAlive> too? that way nobody deletes onDeactivated later thinking it's unused.

Copy link
Copy Markdown
Author

@ArturQuirino ArturQuirino Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Now it hides it, check if the removeSpy were called, shows it and check is the addSpy was called.

const mockToolbar = createMockToolbar();
const addSpy = vi.spyOn(window, 'addEventListener');
const removeSpy = vi.spyOn(window, 'removeEventListener');

const wrapper = mount(Toolbar, {
global: {
stubs: { ButtonGroup: true },
plugins: [
(app) => {
app.config.globalProperties.$toolbar = mockToolbar;
},
],
},
});

const resizeHandler = addSpy.mock.calls.find((c) => c[0] === 'resize')?.[1];
const keydownHandler = addSpy.mock.calls.find((c) => c[0] === 'keydown')?.[1];
expect(resizeHandler).toBeTypeOf('function');
expect(keydownHandler).toBeTypeOf('function');

removeSpy.mockClear();
wrapper.unmount();

expect(removeSpy).toHaveBeenCalledWith('resize', resizeHandler);
expect(removeSpy).toHaveBeenCalledWith('keydown', keydownHandler);

addSpy.mockRestore();
removeSpy.mockRestore();
});

it('removes window listeners on KeepAlive deactivate and restores them on activate', async () => {
const mockToolbar = createMockToolbar();
const addSpy = vi.spyOn(window, 'addEventListener');
const removeSpy = vi.spyOn(window, 'removeEventListener');

const wrapper = mount(ToolbarKeepAliveHost, {
global: {
stubs: { ButtonGroup: true },
plugins: [
(app) => {
app.config.globalProperties.$toolbar = mockToolbar;
},
],
},
});

const resizeHandler = addSpy.mock.calls.find((c) => c[0] === 'resize')?.[1];
const keydownHandler = addSpy.mock.calls.find((c) => c[0] === 'keydown')?.[1];
expect(resizeHandler).toBeTypeOf('function');
expect(keydownHandler).toBeTypeOf('function');

addSpy.mockClear();
removeSpy.mockClear();

wrapper.vm.visible = false;
await wrapper.vm.$nextTick();

expect(removeSpy).toHaveBeenCalledWith('resize', resizeHandler);
expect(removeSpy).toHaveBeenCalledWith('keydown', keydownHandler);

addSpy.mockClear();
removeSpy.mockClear();

wrapper.vm.visible = true;
await wrapper.vm.$nextTick();

expect(addSpy).toHaveBeenCalledWith('resize', resizeHandler);
expect(addSpy).toHaveBeenCalledWith('keydown', keydownHandler);

removeSpy.mockClear();
wrapper.unmount();

expect(removeSpy).toHaveBeenCalledWith('resize', resizeHandler);
expect(removeSpy).toHaveBeenCalledWith('keydown', keydownHandler);

addSpy.mockRestore();
removeSpy.mockRestore();
});
});
37 changes: 26 additions & 11 deletions packages/super-editor/src/editors/v1/components/toolbar/Toolbar.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<script setup>
import { ref, getCurrentInstance, onMounted, onDeactivated, nextTick, computed } from 'vue';
import {
ref,
getCurrentInstance,
onMounted,
onActivated,
onDeactivated,
onBeforeUnmount,
nextTick,
computed,
} from 'vue';
import { throttle } from './helpers.js';
import ButtonGroup from './ButtonGroup.vue';

Expand Down Expand Up @@ -40,16 +49,6 @@ const getFilteredItems = (position) => {
return proxy.$toolbar.getToolbarItemByGroup(position).filter((item) => !excludeButtonsList.includes(item.name.value));
};

onMounted(() => {
window.addEventListener('resize', onResizeThrottled);
window.addEventListener('keydown', onKeyDown);
});

onDeactivated(() => {
window.removeEventListener('resize', onResizeThrottled);
window.removeEventListener('keydown', onKeyDown);
});

const onKeyDown = async (e) => {
if (e.metaKey && e.key === 'f') {
const searchItem = proxy.$toolbar.getToolbarItemByName('search');
Expand All @@ -70,6 +69,22 @@ const onWindowResized = async () => {
};
const onResizeThrottled = throttle(onWindowResized, 300);

function teardownWindowListeners() {
window.removeEventListener('resize', onResizeThrottled);
window.removeEventListener('keydown', onKeyDown);
}

function setupWindowListeners() {
teardownWindowListeners();
window.addEventListener('resize', onResizeThrottled);
window.addEventListener('keydown', onKeyDown);
}

onMounted(setupWindowListeners);
onActivated(setupWindowListeners);
onDeactivated(teardownWindowListeners);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onDeactivated runs when a <KeepAlive> hides this component, but there's no onActivated to put the listeners back when it's shown again. if the toolbar is ever wrapped in <KeepAlive>, the second time it shows up Cmd+F and the resize handling will be dead. if it's never wrapped, you can just drop onDeactivated and keep onBeforeUnmount. worth a look?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I updated it so now the events are added on onMounted and onActivated.

onBeforeUnmount(teardownWindowListeners);

const handleCommand = ({ item, argument, option }) => {
proxy.$toolbar.emitCommand({ item, argument, option });
};
Expand Down