Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to set up "insertTable" #91

Open
dlants opened this issue Oct 22, 2019 · 7 comments
Open

Document how to set up "insertTable" #91

dlants opened this issue Oct 22, 2019 · 7 comments

Comments

@dlants
Copy link

dlants commented Oct 22, 2019

It seems like the provided demo relies on insertTable command being available in https://github.com/ProseMirror/prosemirror-example-setup ... that's no longer the case.

I haven't been able to find good examples for a command that inserts a table into the document, and none of the commands exported from the module seem to fit.

@dlants
Copy link
Author

dlants commented Oct 23, 2019

I was able to create a 1x2 table with the following command:

  function insertTable(
    state: EditorState,
    dispatch?: (tr: Transaction) => void
  ): boolean {
    const tr = state.tr.replaceSelectionWith(
      state.schema.nodes.table.create(
        undefined,
        Fragment.fromArray([
          state.schema.nodes.table_row.create(undefined, Fragment.fromArray([
            state.schema.nodes.table_cell.create(),
            state.schema.nodes.table_cell.create()
          ]))
        ])
      )
    );

    if (dispatch) {
      dispatch(tr);
    }

    return true;
  }

@DianeKim
Copy link

Where 'Fragment' comes from?:-)

@dlants
Copy link
Author

dlants commented Jan 28, 2020

@olsn
Copy link

olsn commented Mar 19, 2020

create() did not work for me any more, instead i had to use createAndFill() - this is what worked for me:

function insertTable(
  state,
  dispatch
) {
  const tr = state.tr.replaceSelectionWith(
    state.schema.nodes.table.create(
      undefined,
      Fragment.fromArray([
        state.schema.nodes.table_row.create(undefined, Fragment.fromArray([
          state.schema.nodes.table_cell.createAndFill(),
          state.schema.nodes.table_cell.createAndFill()
        ]))
      ])
    )
  );

  if (dispatch) {
    dispatch(tr);
  }

  return true;
}
const insertTableMenuItem = new MenuItem({label: "Table", run: insertTable});
menu.fullMenu[1][0].content.push(insertTableMenuItem);

@xiaweiss
Copy link

Inspired from tiptap

function createTable (state, rowsCount, colsCount, withHeaderRow, cellContent) {
  const types = tableNodeTypes(state.schema)
  const headerCells = []
  const cells = []
  const createCell = (cellType, cellContent) => cellContent ? cellType.createChecked(null, cellContent) : cellType.createAndFill()

  for (let index = 0; index < colsCount; index += 1) {
    const cell = createCell(types.cell, cellContent)

    if (cell) {
      cells.push(cell)
    }

    if (withHeaderRow) {
      const headerCell = createCell(types.header_cell, cellContent)

      if (headerCell) {
        headerCells.push(headerCell)
      }
    }
  }

  const rows = []

  for (let index = 0; index < rowsCount; index += 1) {
    rows.push(types.row.createChecked(null, withHeaderRow && index === 0 ? headerCells : cells))
  }

  return types.table.createChecked(null, rows)
}

function addTable (state, dispatch, { rowsCount, colsCount, withHeaderRow, cellContent }, ) {
  const offset = state.tr.selection.anchor + 1

  const nodes = createTable(state, rowsCount, colsCount, withHeaderRow, cellContent)
  const tr = state.tr.replaceSelectionWith(nodes).scrollIntoView()
  const resolvedPos = tr.doc.resolve(offset)

  tr.setSelection(TextSelection.near(resolvedPos))

  dispatch(tr)
}

// add table to a new paragraph
function addTableToEnd (state, dispatch, { rowsCount, colsCount, withHeaderRow, cellContent }, ) {
  let tr = state.tr

  // get block end position
  const end = tr.selection.$head.end(1) // param 1 is node deep
  const resolvedEnd = tr.doc.resolve(end)

  // move cursor to the end, then insert table
  const nodes = createTable(state, rowsCount, colsCount, withHeaderRow, cellContent)
  tr.setSelection(TextSelection.near(resolvedEnd))
  tr = tr.replaceSelectionWith(nodes).scrollIntoView()

  // move cursor into table
  const offset = end + 1
  const resolvedPos = tr.doc.resolve(offset)
  tr.setSelection(TextSelection.near(resolvedPos))

  dispatch(tr)
}

@Tilesto
Copy link

Tilesto commented Mar 26, 2021

And what type of "cellContent" is necessary for schema.nodes.table_cell.createChecked(null, ?) ?

@LeDNI
Copy link

LeDNI commented Jan 10, 2023

And what type of "cellContent" is necessary for schema.nodes.table_cell.createChecked(null, ?) ?

Fragment | ProsemirrorNode | Array<ProsemirrorNode>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants