Skip to content

Commit

Permalink
feat(tables,react-tables): remove table plugins if view in not editab…
Browse files Browse the repository at this point in the history
…le (#1905)
  • Loading branch information
whawker committed Oct 14, 2022
1 parent 03e16b5 commit 7dbd1ae
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/bright-ways-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@remirror/extension-react-tables': minor
'@remirror/extension-tables': minor
---

Prevent table plugins (such as column resizing) from loading, if the view is not editable
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { useHelpers } from '@remirror/react-core';

import { TableCellMenu, TableCellMenuProps } from './table-cell-menu';
import {
Expand Down Expand Up @@ -53,6 +54,12 @@ export const TableComponents: React.FC<TableComponentsProps> = ({
tableDeleteRowColumnButtonProps,
tableDeleteButtonProps,
}) => {
const { isViewEditable } = useHelpers();

if (!isViewEditable()) {
return null;
}

return (
<>
{enableTableCellMenu && <TableCellMenu {...tableCellMenuProps} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ const TableControllerCell = ({

const events = createControllerEvents({ view, findTable });

const childNodes = view.editable
? [...TableInsertButtonTrigger({ view, findTable }), ...TableInsertMark()]
: [];

const wrapper = h(
'div',
{ contentEditable: 'false', className: ExtensionTablesTheme.TABLE_CONTROLLER_WRAPPER },
contentDOM,
...TableInsertButtonTrigger({ view, findTable }),
...TableInsertMark(),
...childNodes,
);

return h(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ export class TableExtension extends BaseTableExtension {
* Add the table plugins to the editor.
*/
createExternalPlugins(): ProsemirrorPlugin[] {
const plugins = [];
const plugins: ProsemirrorPlugin[] = [];

if (this.store.isMounted() && this.store.helpers.isViewEditable() === false) {
return plugins;
}

if (this.options.resizable) {
// Add first to avoid highlighting cells while resizing
Expand Down Expand Up @@ -127,6 +131,8 @@ export class TableExtension extends BaseTableExtension {
}

onView(view: EditorView): void {
super.onView(view);

// We have multiple node types which share a eom table_row in this
// extension. In order to make the function `tableNodeTypes` from
// `prosemirror-extension-tables` return the correct node type, we
Expand Down
13 changes: 12 additions & 1 deletion packages/remirror__extension-tables/src/table-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CommandFunctionProps,
convertCommand,
EditorState,
EditorView,
extension,
ExtensionPriority,
ExtensionTag,
Expand Down Expand Up @@ -102,11 +103,21 @@ export class TableExtension extends NodeExtension<TableOptions> {
}
}

onView(_: EditorView): void {
if (this.store.helpers.isViewEditable() === false) {
this.store.updateExtensionPlugins(this);
}
}

/**
* Add the table plugins to the editor.
*/
createExternalPlugins(): ProsemirrorPlugin[] {
const plugins = [];
const plugins: ProsemirrorPlugin[] = [];

if (this.store.isMounted() && this.store.helpers.isViewEditable() === false) {
return plugins;
}

if (this.options.resizable) {
// Add first to avoid highlighting cells while resizing
Expand Down

1 comment on commit 7dbd1ae

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉 Published on https://remirror.io as production
🚀 Deployed on https://634986a32f9b9b0d14a60104--remirror.netlify.app

Please sign in to comment.