Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile/text): keep the table column bar above the toolbar

Elian Doran2aab2f17b98a
diff --git a/apps/client/src/widgets/type_widgets/text/config.spec.ts b/apps/client/src/widgets/type_widgets/text/config.spec.ts
index 6c9db5be62..a1d29bbe93 100644
--- a/apps/client/src/widgets/type_widgets/text/config.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.spec.ts
@@ -298,6 +298,23 @@ describe("CK config - HTML support", () => {
});
describe("CK config - tables", () => {
+ it("keeps the column bar above the phone's toolbar while it is shown", async () => {
+ const bottomObstacle = (await buildConfig(baseOpts())).tableColumnBar?.bottomObstacle;
+ expect(bottomObstacle?.()).toBeNull();
+
+ const container = document.createElement("div");
+ container.className = "classic-toolbar-outer-container visible active";
+ const island = document.createElement("div");
+ island.className = "classic-toolbar-island";
+ container.appendChild(island);
+ document.body.appendChild(container);
+ expect(bottomObstacle?.()).toBe(island);
+
+ container.classList.remove("active");
+ expect(bottomObstacle?.()).toBeNull();
+ container.remove();
+ });
+
it("offers the Sort dropdown just before the table properties", async () => {
const toolbar = (await buildConfig(baseOpts())).table?.contentToolbar ?? [];
expect(toolbar).toContain("tableSort");
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index 3c4f26d4e6..fba000d101 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -21,6 +21,7 @@ import diffAiResponse from "./ai_diff.js";
import { createColorMenuHost } from "./color_menu.js";
import { buildFontColorConfig, buildTableColorConfig } from "./color_palette.js";
import { createEmojiList, createNoteMentionList, createSlashCommandList } from "./mention_list_view.js";
+import { getShownToolbarIsland } from "./mobile_editor_toolbar.js";
import { buildQuoteTransformation, resolveQuoteSetting } from "./quotes.js";
import { buildCustomTransformations, parseCustomReplacements } from "./replacements.js";
import { buildToolbarConfig } from "./toolbar.js";
@@ -333,6 +334,7 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
config.toolbarGroupMenu = { host: () => createToolbarGroupMenuHost() };
config.colorMenu = { host: () => createColorMenuHost() };
+ config.tableColumnBar = { bottomObstacle: getShownToolbarIsland };
return {
...config,
diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
index 00e1205a79..f59f33c584 100644
--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
@@ -182,6 +182,11 @@ export function useKeyboardPanel(enabled: boolean) {
/** The row of buttons of the phone's toolbar, which scrolls sideways under a menu of it. */
export const MOBILE_TOOLBAR_ROW = ".classic-toolbar-island .classic-toolbar-widget";
+/** The card of the phone's toolbar while it is shown, for `tableColumnBar.bottomObstacle`. */
+export function getShownToolbarIsland() {
+ return document.querySelector<HTMLElement>(".classic-toolbar-outer-container.active .classic-toolbar-island");
+}
+
/**
* Closes a menu of the toolbar as a tap on another item of {@link MOBILE_TOOLBAR_ROW} clicks, before
* the item runs. The menu's `Popup` keeps it open on a press there, which can start a scroll; the
diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.ts
index 1c5ba7c04a..a9a568f113 100644
--- a/packages/ckeditor5/src/index.ts
+++ b/packages/ckeditor5/src/index.ts
@@ -54,6 +54,7 @@ import "./plugins/tabs/tabs.js";
import "./plugins/multicolumn/multicolumn.js";
import "./plugins/mermaid/mermaid.js";
import "./plugins/referencelink.js";
+import "./plugins/table_column_bar/table_column_bar.js";
import "./plugins/table_context_menu.js";
import "./plugins/table_paste_rows.js";
diff --git a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.ts b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.ts
index d516b69597..454a4aa534 100644
--- a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.ts
+++ b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.ts
@@ -214,6 +214,55 @@ describe("TableColumnBar", () => {
expect(withToolbar.ui.viewportOffset.bottom ?? 0).toBe(initialOffset ?? 0);
});
+ it("sits above the element of `tableColumnBar.bottomObstacle`, wherever it floats", async () => {
+ const card = document.createElement("div");
+ card.style.cssText = "position: fixed; bottom: 6px; left: 6px; right: 6px; height: 44px";
+ document.body.appendChild(card);
+ let obstacle: HTMLElement | null = card;
+ const floating = await createTestEditor(PLUGINS, {
+ tableColumnBar: { bottomObstacle: () => obstacle }
+ });
+
+ await showBar(floating, modelTable([["a[]", "b"]]));
+ expectNear(barRectOf(floating).bottom, card.getBoundingClientRect().top);
+
+ // Without one, the bar goes back to the bottom of the viewport.
+ obstacle = null;
+ window.dispatchEvent(new Event("resize"));
+ await expect.poll(() => Math.abs(barRectOf(floating).bottom - document.documentElement.clientHeight))
+ .toBeLessThan(1.5);
+ card.remove();
+ });
+
+ it("corrects for `position: fixed` placing it off the measured viewport", async () => {
+ const card = document.createElement("div");
+ card.style.cssText = "position: fixed; bottom: 6px; left: 6px; right: 6px; height: 44px";
+ document.body.appendChild(card);
+ const shifted = await createTestEditor(PLUGINS, {
+ tableColumnBar: { bottomObstacle: () => card }
+ });
+ // iOS places fixed elements against the layout viewport while the keyboard scrolls
+ // the visual viewport, which a transformed container reproduces.
+ const container = barView(shifted).element?.parentElement;
+ expect(container).toBeInstanceOf(HTMLElement);
+ container?.style.setProperty("transform", "translate(-30px, -40px)");
+
+ try {
+ await showBar(shifted, modelTable([["a[]", "b"]], 'tableWidth="80%"'));
+ expectNear(barRectOf(shifted).bottom, card.getBoundingClientRect().top);
+ expectAligned(shifted);
+
+ const { x, y } = press(dividers(shifted)[0]);
+ moveTo(x + 50, y);
+ await expect.poll(() => isEdgeShown(shifted)).toBe(true);
+ expectNear(edgeRectOf(shifted).left + 1, endEdgeOf(shifted, 0));
+ release();
+ } finally {
+ container?.style.removeProperty("transform");
+ card.remove();
+ }
+ });
+
it("works with an editor without a toolbar and with a detached one", async () => {
const balloon = await createTestEditorOf(BalloonEditor, PLUGINS);
const decoupled = await createTestEditorOf(DecoupledEditor, PLUGINS);
diff --git a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.ts b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.ts
index 4acdf6123e..71bc90a1e8 100644
--- a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.ts
+++ b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.ts
@@ -227,7 +227,7 @@ export default class TableColumnBar extends Plugin {
*/
private layOut(target: BarTarget): number | null {
const view = this.view;
- const area = getBarArea(target.domRoot, this.getToolbarElement());
+ const area = getBarArea(target.domRoot, this.getToolbarElement(), this.getBottomObstacle());
const barTop = area.bottom - view.track.offsetHeight;
const tableRect = target.domTable.getBoundingClientRect();
const clip = getClientBox(findScroller(target.domTable, "overflowX"));
@@ -249,7 +249,11 @@ export default class TableColumnBar extends Plugin {
// The track spans the scroller, so the divider at the table's end shows in full and the
// auto-scroll starts only near the edges of the scroller.
+ // `position: fixed` places the bar against the layout viewport, which iOS shifts away
+ // from the viewport that `getBoundingClientRect()` measures while the keyboard is up.
view.set({ left: area.left, top: barTop, width: area.right - area.left });
+ const shift = getFixedShift(view.element, view.left, view.top);
+ view.set({ left: area.left - shift.x, top: barTop - shift.y });
view.setTrack(trackLeft - area.left, trackRight - trackLeft, tableRect.left - trackLeft);
this.trackBox = { left: trackLeft, right: trackRight };
@@ -257,7 +261,11 @@ export default class TableColumnBar extends Plugin {
view.setActiveDivider(column);
view.setEdge(column === null
? null
- : { left: tableRect.left + edges[column + 1], top, height: bottom - top });
+ : {
+ left: tableRect.left + edges[column + 1] - shift.x,
+ top: top - shift.y,
+ height: bottom - top
+ });
return barTop;
}
@@ -300,6 +308,11 @@ export default class TableColumnBar extends Plugin {
return (this.editor.ui.view as { toolbar?: ToolbarView }).toolbar?.element ?? null;
}
+ private getBottomObstacle() {
+ const element = this.editor.config.get("tableColumnBar")?.bottomObstacle?.();
+ return element?.isConnected ? element : null;
+ }
+
/** Keeps the balloons of the editor and the caret above the bar. */
private setBottomOffset(offset: number) {
if (offset === this.bottomOffset) {
@@ -599,11 +612,16 @@ interface Tap {
/**
* The part of the viewport that the bar spans: the visible part of the scroll container of
- * `domRoot`, up to the top of a toolbar at its bottom.
+ * `domRoot`, up to the top of a toolbar at its bottom, or of `obstacle` wherever it is.
*/
-function getBarArea(domRoot: HTMLElement, toolbar: HTMLElement | null): ClientBox {
+function getBarArea(
+ domRoot: HTMLElement, toolbar: HTMLElement | null, obstacle: HTMLElement | null
+): ClientBox {
const area = getClientBox(findScroller(domRoot, "overflowY"));
let bottom = Math.min(area.bottom, getViewportBottom());
+ if (obstacle) {
+ return { ...area, bottom: Math.min(bottom, obstacle.getBoundingClientRect().top) };
+ }
const toolbarRect = toolbar?.isConnected ? toolbar.getBoundingClientRect() : null;
const isToolbarAtBottom = !!toolbarRect
&& toolbarRect.top < bottom && toolbarRect.bottom >= bottom - 1;
@@ -668,7 +686,27 @@ function vibrate() {
}
declare module "ckeditor5" {
+ interface EditorConfig {
+ tableColumnBar?: {
+ /**
+ * The element the bar stays above while it is on the page, such as a toolbar that
+ * floats over the bottom of the editing area. Without one, the bar stays above the
+ * editor's toolbar when that is at the bottom of the viewport.
+ */
+ bottomObstacle?: () => HTMLElement | null;
+ };
+ }
+
interface PluginsMap {
[TableColumnBar.pluginName]: TableColumnBar;
}
}
+
+/**
+ * How far `position: fixed` places `element` from where its `left` and `top` put it in the
+ * viewport, in pixels.
+ */
+function getFixedShift(element: HTMLElement | null, left: number, top: number) {
+ const rect = element?.getBoundingClientRect();
+ return rect ? { x: rect.left - left, y: rect.top - top } : { x: 0, y: 0 };
+}