0.2.3 • Published 4 years ago

slate-editable-table v0.2.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

slate-editable-table

CircleCI Netlify Status GitHub Actions Status

:pen: An editable table plugin for Slate.js

Table of Contents

Screenshot

Live Demo

Storybook

Storybook

Install

  • use npm
npm i -S slate-editable-table
  • use yarn
yarn add slate-editable-table

Usage

import { Editor } from 'slate-react';
import { ValueJSON, Value } from 'slate';

import React from 'react';
import { EditTable, EditTableCommands, hasTablePlugin } from 'slate-editable-table';

const tablePlugin = EditTable();

const plugins = [tablePlugin];

export type Props = {
  initialValue: ValueJSON;
  onChange: ({ value: Value }) => void;
};

export class ExampleEditor extends React.Component<Props> {
  editor!: Editor & EditTableCommands;
  state: {
    value: Value;
  };

  constructor(props: Props) {
    super(props);
    this.state = {
      value: Value.fromJSON(props.initialValue),
    };
  }

  onChange = ({ value }) => {
    this.setState({ value });
    this.props.onChange({ value });
  };

  removeTable = () => this.editor.removeTable();
  insertTable = () => this.editor.insertTable(3, 3, { columnWidth: 200, maxWidth: 500 });
  insertLeft = () => this.editor.insertLeft();
  insertRight = () => this.editor.insertRight();
  insertAbove = () => this.editor.insertAbove();
  insertBelow = () => this.editor.insertBelow();
  removeColumn = () => this.editor.removeColumn();
  removeRow = () => this.editor.removeRow();
  mergeSelection = () => this.editor.mergeSelection();
  splitCell = () => this.editor.splitCell();
  enableResizing = () => this.editor.enableResizing();
  disableResizing = () => this.editor.disableResizing();

render() {
    return (
      <>
        <button onMouseDown={this.insertTable}>Insert Table</button>
        <button onMouseDown={this.insertAbove}>Insert Above</button>
        <button onMouseDown={this.insertBelow}>Insert Below</button>
        <button onMouseDown={this.insertLeft}>Insert Left</button>
        <button onMouseDown={this.insertRight}>Insert Right</button>
        <button onMouseDown={this.mergeSelection}>merge selection</button>
        <button onMouseDown={this.splitCell}>split cell</button>
        <button onMouseDown={this.removeColumn}>Remove Column</button>
        <button onMouseDown={this.removeRow}>Remove Row</button>
        <button onMouseDown={this.removeTable}>Remove Table</button>
        <button onMouseDown={this.disableResizing}>disable resizing</button>
        <button onMouseDown={this.enableResizing}>enable resizing</button>
        <Editor
          ref={e => {
            if (hasTablePlugin(e)) {
              this.editor = e;
            }
          }}
          plugins={plugins}
          placeholder="Enter some text..."
          value={this.state.value}
          onChange={this.onChange}
        />
      </>
    );
  }
}

Please see also https://github.com/bokuweb/slate-editable-table/blob/master/example/index.tsx

Commands

CommandDescription
insertTablecreate and insert new table
removeTableremove table
insertLeftinsert new column to left of current anchor cell
insertRightinsert new column to right of current anchor cell
insertAboveinsert new row to above of current anchor cell
insertBelowinsert new row to below of current anchor cell
removeColumnremove selected column
removeRowremove selected row
mergeSelectionmerge current selection
splitCellsplit current cell
enableResizingenable cell resizing
enableResizingdisable cell resizing

Query

QueryDescription
isSelectionInTableIf selection is in current table, return true
canSelectedCellsMergeIf selection is able to merge, return true
findCurrentTablefind current table block

Test

npm t

TODO

  • support mobile
  • vertical resizing
  • custom renderer

Contribute

If you have a feature request, please add it as an issue or make a pull request.

Changelog

v0.1.0

  • Initial release

License

The MIT License (MIT)

Copyright (c) 2019 bokuweb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago