fix: keep table horizontal scrolling when notes open read-only

TipTap registers the table node view together with column resizing only when the editor starts editable. Opening a note in View mode therefore left tables without their .tableWrapper, so wide tables could not scroll horizontally until the app was restarted in Edit mode.

Keep the table view plugin installed for read-only editors; its resize handlers still ignore non-editable views.

Add a regression test that the table node view is registered when the initial editor is read-only. (Discord report)
This commit is contained in:
Yuri Karamian committed 2026-11-05 01:27:11 +04:00
1 parent 523d1a5511
commit 77164427f1
3 files changed
+79 -2

No files matched your search

+2 -2
View File
@@ -5,7 +5,6 @@
import StarterKit from '@tiptap/starter-kit';
import TaskList from '@tiptap/extension-task-list';
import TaskItem from '@tiptap/extension-task-item';
import { Table } from '@tiptap/extension-table';
import { TableRow } from '@tiptap/extension-table-row';
import { TableCell } from '@tiptap/extension-table-cell';
import { TableHeader } from '@tiptap/extension-table-header';
@@ -19,6 +18,7 @@
import { Color } from '@tiptap/extension-color';
import { TextStyle } from '@tiptap/extension-text-style';
import { CachedCodeBlockLowlight } from '$lib/editor/extensions/cachedCodeBlockLowlight';
import { AlwaysResizableTable } from '$lib/editor/tableExtension';
import { Details, DetailsSummary, DetailsContent } from '@tiptap/extension-details';
import TextAlign from '@tiptap/extension-text-align';
import { common, createLowlight } from 'lowlight';
@@ -5591,7 +5591,7 @@
FocusedPlaceholder,
TaskList,
TaskItem.extend(keepCodeWhitespace).configure({ nested: true }),
Table.configure({ resizable: true }),
AlwaysResizableTable.configure({ resizable: true }),
TableRow,
CustomTableCell,
CustomTableHeader,
+27
View File
@@ -0,0 +1,27 @@
import { Table } from '@tiptap/extension-table';
import { columnResizing, tableEditing } from '@tiptap/pm/tables';
/**
* TipTap registers the table node view together with its column-resizing plugin, and skips both
* when the editor is created read-only. Without the node view there is no `.tableWrapper`, so a
* wide table loses its horizontal scroll until the app is restarted in Edit mode.
*
* Keep the table view and its wrapper even when the initial state is View mode. The resizing
* handlers already ignore non-editable views; this only makes the wrapper available to scroll.
*/
export const AlwaysResizableTable = Table.extend({
addProseMirrorPlugins() {
const plugins = [];
if (this.options.resizable) {
plugins.push(columnResizing({
handleWidth: this.options.handleWidth,
cellMinWidth: this.options.cellMinWidth,
defaultCellMinWidth: this.options.cellMinWidth,
View: this.options.View,
lastColumnResizable: this.options.lastColumnResizable,
}));
}
plugins.push(tableEditing({ allowTableNodeSelection: this.options.allowTableNodeSelection }));
return plugins;
},
});
+50
View File
@@ -0,0 +1,50 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { Table } from '@tiptap/extension-table';
import { columnResizingPluginKey, tableEditingKey, tableNodes } from '@tiptap/pm/tables';
import { EditorState } from '@tiptap/pm/state';
import { Schema } from '@tiptap/pm/model';
import { AlwaysResizableTable } from '../src/lib/editor/tableExtension.ts';
test('the table wrapper is registered even when the editor starts read-only', () => {
const options = {
...Table.options,
resizable: true,
};
const plugins = AlwaysResizableTable.config.addProseMirrorPlugins.call({
options,
editor: { isEditable: false },
});
const keys = new Set(plugins.map((plugin) => plugin.key));
assert.ok(keys.has(columnResizingPluginKey.key), 'missing table node view / column resizing plugin');
assert.ok(keys.has(tableEditingKey.key), 'missing table editing plugin');
// The column-resizing plugin registers the table node view when its state starts. That node
// view is what creates .tableWrapper, which owns the horizontal scrollbar.
const resizing = plugins.find((plugin) => plugin.key === columnResizingPluginKey.key);
const nodes = tableNodes({ tableGroup: 'block', cellContent: 'inline*', cellAttributes: {} });
const schema = new Schema({
nodes: {
doc: { content: 'block+' },
text: { group: 'inline' },
paragraph: { content: 'inline*', group: 'block' },
...nodes,
},
});
EditorState.create({ schema, plugins });
assert.equal(typeof resizing.spec.props.nodeViews.table, 'function');
});
test('a non-resizable table still gets table editing', () => {
const options = {
...Table.options,
resizable: false,
};
const plugins = AlwaysResizableTable.config.addProseMirrorPlugins.call({
options,
editor: { isEditable: false },
});
const keys = new Set(plugins.map((plugin) => plugin.key));
assert.equal(keys.has(columnResizingPluginKey.key), false);
assert.ok(keys.has(tableEditingKey.key));
});