Code Monkey home page Code Monkey logo

slate-tables's Introduction

slate-tables

A Slate plugin to handle table edition.

Demo: https://whatever-company.github.io/slate-tables/

Build Status

Install

$ npm login --registry=https://npm.pkg.github.com --scope=@whatever-company
> Username: USERNAME
> Password: TOKEN (from your github settings)
> Email: PUBLIC-EMAIL-ADDRESS
$ npm install @whatever-company/slate-tables

Features

  • Pressing Up/Down moves the cursor to the row above/below
  • Pressing Left/Right moves the cursor to the cell before/after
  • Pressing Enter inserts a new row (if enabled)
  • Pressing Tab moves the cursor to the next cell and creats a new row
  • Pressing Shift+Tab moves the cursor to the previous cell

Nested tables

The plugin supports nested tables natively.

colspan & rowspan

Colspan and Rowspan are supported. All operations create a matrix containing all cells' positions.

Copy/Paste behavior

Nothing is done to support copy/paste in this plugin. You have to implement it in you codebase.

Simple Usage

import TablePlugin from '@eliumhq/slate-tables'

const tablePlugin = TablePlugin({
	saveColumns: false,
	enterCreatesRow: true,
	blocks: {
		table: 'table',
		row: 'table-row',
		cell: 'table-cell',
		content: 'paragraph'
	}
})

const plugins = [tablePlugin]

Data structure

Here is what your Slate document containing tables should look like:

<value>
	<document>
		<paragraph>Some text</paragraph>

		<table>
			<table-row>
				<table-cell colspan="2">
					<paragraph>Any block can goes into cells</paragraph>
				</table-cell>
			</table-row>

			<table-row>
				<table-cell>
					<paragraph>Second row</paragraph>
				</table-cell>

				<table-cell>
					<paragraph>Second row</paragraph>
				</table-cell>
			</table-row>
		</table>
	</document>
</value>

Options

Option object you can pass to the plugin.

prop type default desc
saveColumns boolean false when enabled, it will save the columns in an array of object in the table node's data (useful to render a <colgroup/> tag)
enterCreatesRow boolean false Add the support of the creation of a new row when user press enter
blocks -> table string table Node name for the <table />
blocks -> row string table-row Node name for the <tr />
blocks -> cell string table-cell Node name for the <td />
blocks -> content string paragraph Node name for the default child of a cell

TablePlugin

TablePlugin(options: Options) => Instance

Constructs an instance of the table plugin, for the given options. You can then add this instance to the list of plugins passed to Slate.

Commands

editor.insertTable()

Creates a table at current selection.

editor.insertTableAtRange(range: Range)

Creates a table at the given range.

editor.insertRow()

Creates a row at the current start block.

editor.insertRowAtKey(key: string, atIndex: number)

Creates a row after the focused one inside the table at key. If atIndex is given, it will insert the new row at this index.

editor.insertRowAtEnd()

Creates a row at the end of the focused table (start of the selection must be in a table)

editor.insertColumn()

Creates a column after the focused one. This will alter all row nodes to add a cell.

editor.insertColumnAtKey(key: string)

Creates a column after the row found at key. This will alter all row nodes to add a cell.

editor.deleteColumn()

Delete the focused column. This will alter all row nodes to remove a cell.

editor.deleteColumnAtKey(key: string)

Delete the column at key. This will alter all row nodes to remove a cell.

editor.deleteTable()

Deletes the focused table.

editor.deleteTableAtKey(key: string)

Deletes the focused at key.

editor.deleteRow()

Deletes focused row.

editor.deleteRowAtKey(key: string)

Deletes the row at key.

editor.deleteCellsContentAtRange(range: Range)

Deletes content in cells found in range. It's necessary to use this instead of editor.deleteAtRange to not delete row & cell nodes.

editor.setCellProperties

Merges properties in focused cell's data.

editor.setCellPropertiesAtKey(key: string, properties: Object)

Merges properties in cell's data.

editor.setColumnProperties(properties: Object)

It will merges properties with the current column's data (stored in table.data.columns)

editor.setColumnPropertiesAtIndex(tableKey: string, index: number, properties: Object)

Merges column's data with properties at given index.

editor.setTableProperties(properties: Object)

Merges properties with table's data at current selection.

editor.setTablePropertiesAtKey(key: string, properties: Object)

Merges properties with table's data at key.

editor.increaseRowspanAtKey

Increases the rowspan of the cell at key.

editor.increaseColspanAtKey

Increases the colspan of the cell at key.

editor.decreaseRowspanAtKey

Decreases the rowspan of the cel at key.

editor.decreaseColspanAtKey

Decreases the colspan of the cel at key.

Queries

editor.isInTable()

Returns true if selection.start or selection.end is in a table node.

editor.isRangeInTable(range: Range)

Returns true if range.start or range.end is in a table node.

editor.getTableAtKey(key: string)

Returns a Table object for the table at key. This object contains helpers to access sibling cells, rows, columns.

editor.getCellAtKey(key: string)

Returns the cell at key.

editor.getRowAtKey(key: string)

Returns the row at key.

editor.canIncreaseColspanAtKey(key: string)

Returns true if the cell at key can have a colspan increased by one.

editor.canIncreaseRowspanAtKey(key: string)

Returns true if the cell at key can have a rowspan increased by one.

editor.canDecreaseColspanAtKey(key: string)

Returns true if the cell at key can have a colspan decreased by one.

editor.canDecreaseRowspanAtKey(key: string)

Returns true if the cell at key can have a rowspan decreased by one.

editor.getColumnPropertiesAtKey(key: string)

Returns properties of the table at key

slate-tables's People

Contributors

dependabot[bot] avatar emerzh avatar jothillaye avatar qgerome avatar renovate-bot avatar renovate[bot] avatar rslinckx avatar tch-elium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slate-tables's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whatever-company/renovate-config)

Migrate to TypeScript

Hello, is there any reasons not to use TypeScript here?
If TS is acceptable i can conduct migration to TypeScript.
Thanks.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore: update babel packages (@babel/cli, @babel/core, @babel/eslint-parser, @babel/preset-env, @babel/preset-react)
  • chore: update dependency babel-jest to v29.7.0
  • chore: update dependency eslint-plugin-import to v2.28.1
  • chore: update dependency eslint-plugin-react to v7.33.2
  • chore: update dependency react-test-renderer to v16.14.0
  • chore: update jest packages to v29.7.0 (jest, jest-diff)
  • deps: update dependency core-js to v3.33.0
  • deps: update dependency is-hotkey to v0.2.0
  • chore: update dependency eslint-config-prettier to v9
  • chore: update dependency eslint-config-standard to v17
  • chore: update dependency eslint-plugin-prettier to v5
  • chore: update dependency prettier to v3
  • chore: update react to v18 (major) (react, react-dom, react-test-renderer)
  • chore: lock file maintenance
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • core-js 3.29.1
  • is-hotkey 0.1.6
  • @babel/cli 7.22.5
  • @babel/core 7.22.5
  • @babel/eslint-parser 7.22.5
  • @babel/preset-env 7.22.5
  • @babel/preset-react 7.22.5
  • babel-jest 29.5.0
  • eslint 8.43.0
  • eslint-config-prettier 8.6.0
  • eslint-config-standard 15.0.1
  • eslint-config-standard-react 13.0.0
  • eslint-plugin-babel 5.3.1
  • eslint-plugin-import 2.27.5
  • eslint-plugin-node 11.1.0
  • eslint-plugin-prettier 4.2.1
  • eslint-plugin-promise 6.1.1
  • eslint-plugin-react 7.32.2
  • eslint-plugin-react-hooks 4.6.0
  • eslint-plugin-standard 5.0.0
  • immutable 4.0.0-rc.12
  • jest 29.2.1
  • jest-diff 29.2.1
  • jest-watch-typeahead 2.2.2
  • lodash.isequal 4.5.0
  • parcel-bundler 1.12.5
  • prettier 2.8.8
  • react 16.14.0
  • react-dom 16.14.0
  • react-test-renderer 16.13.1
  • standard-version 9.5.0
  • react ^16.9.0
  • react-dom ^16.9.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.