Skip to content

Commit

Permalink
rewrite plugin in typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunk committed Mar 18, 2021
1 parent b3b4031 commit 7176b6f
Show file tree
Hide file tree
Showing 18 changed files with 7,607 additions and 698 deletions.
12 changes: 6 additions & 6 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)
MIT License

Copyright (c) 2018 Rupert Dunk. https://rupertdunk.com
Copyright (c) 2021 ʞunp ʇɹǝdnɹ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,13 +9,13 @@ 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 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.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7,415 changes: 7,056 additions & 359 deletions package-lock.json

Large diffs are not rendered by default.

52 changes: 40 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,55 @@
"name": "sanity-plugin-table",
"version": "2.1.0",
"description": "Table schema type and input component for Sanity CMS",
"main": "lib/schema/table.js",
"main": "lib/TableComponent.js",
"scripts": {
"build": "babel src -d lib -D",
"prepublishOnly": "npm run build"
"build": "sanipack build",
"watch": "sanipack build --watch",
"prepublishOnly": "sanipack build && sanipack verify"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/rdunk/sanity-plugin-table.git"
},
"author": "Rupert Dunk <[email protected]>",
"keywords": [
"sanity",
"sanity-plugin"
],
"license": "MIT",
"peerDependencies": {
"@sanity/icons": "^1.0.1",
"@sanity/ui": "^0.33.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"styled-components": "^5.2.1"
},
"devDependencies": {
"@babel/cli": "^7.1.5",
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0"
"@types/react": "^17.0.3",
"eslint": "7.22.0",
"eslint-config-prettier": "8.1.0",
"eslint-config-sanity": "5.1.0",
"eslint-plugin-react": "7.22.0",
"prettier": "2.2.1",
"sanipack": "1.0.8"
},
"bugs": {
"url": "https://github.com/rdunk/sanity-plugin-table/issues"
},
"homepage": "https://github.com/rdunk/sanity-plugin-table#readme",
"prettier": {
"semi": false,
"printWidth": 100,
"bracketSpacing": false,
"singleQuote": true
},
"dependencies": {
"@sanity/uuid": "^3.0.1",
"prop-types": "^15.7.2",
"react": "^17.0.1"
"eslintConfig": {
"parser": "sanipack/babel/eslint-parser",
"extends": [
"sanity",
"sanity/react",
"prettier",
"prettier/react"
]
}
}
4 changes: 2 additions & 2 deletions sanity.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"parts": [
{
"implements": "part:@sanity/base/schema-type",
"path": "schema/row.js"
"path": "schema/row"
},
{
"implements": "part:@sanity/base/schema-type",
"path": "schema/table.js"
"path": "schema/table"
}
]
}
190 changes: 190 additions & 0 deletions src/TableComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import React, { useState, FunctionComponent, forwardRef } from 'react';
import { randomKey } from '@sanity/util/lib/pathUtils';
import FormField from 'part:@sanity/components/formfields/default';
import PatchEvent, { set, unset } from 'part:@sanity/form-builder/patch-event';
import config from 'config:table';
import TableControl from './components/TableControl';
import TableInput from './components/TableInput';
import TableMenu from './components/TableMenu';
import { Box, Button, Card, Dialog, Flex, Inline, Text } from '@sanity/ui';

interface RootProps {
level: number;
markers: any[];
type: {
title: string;
description: string;
options: Record<string, any>;
};
value: {
rows: TableRow[];
};
onChange: (...any) => any;
}

// This probably isn't necessary anymore
function deepClone<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}

const TableComponent: FunctionComponent<RootProps> = (props) => {
const { type, level, value, markers, onChange } = props;
const [dialog, setDialog] = useState<{
type: string;
callback: () => any;
} | null>(null);

const updateValue = (value) => {
return onChange(PatchEvent.from(set(value)));
};

const resetValue = () => {
return onChange(PatchEvent.from(unset()));
};

const createTable = () => {
const newValue = {
rows: [
{
_type: config.rowType,
_key: randomKey(),
cells: ['', ''],
},
{
_type: config.rowType,
_key: randomKey(),
cells: ['', ''],
},
],
};
return updateValue({...(value || {}), ...newValue });
};

const confirmRemoveTable = () => {
setDialog({ type: 'table', callback: removeTable });
};

const removeTable = () => {
resetValue();
setDialog(null);
};

const addRows = (count: number = 1) => {
const newValue = { ...value };
// Calculate the column count from the first row
const columnCount = value.rows[0].cells.length;
for (let i = 0; i < count; i++) {
// Add as many cells as we have columns
newValue.rows.push({
_type: config.rowType,
_key: randomKey(),
cells: Array(columnCount).fill(''),
});
}
return updateValue(newValue);
};

const removeRow = (index: number) => {
const newValue = deepClone(value);
newValue.rows.splice(index, 1);
updateValue(newValue);
setDialog(null);
};

const confirmRemoveRow = (index: number) => {
if (value.rows.length <= 1) return confirmRemoveTable();
return setDialog({ type: 'row', callback: () => removeRow(index) });
};

const confirmRemoveColumn = (index: number) => {
if (value.rows[0].cells.length <= 1) return confirmRemoveTable();
return setDialog({ type: 'column', callback: () => removeColumn(index) });
};

const addColumns = (count: number) => {
const newValue = deepClone(value);
// Add a cell to each of the rows
newValue.rows.forEach((_, i) => {
for (let j = 0; j < count; j++) {
newValue.rows[i].cells.push('');
}
});
return updateValue(newValue);
};

const removeColumn = (index) => {
const newValue = deepClone(value);
newValue.rows.forEach((row) => {
row.cells.splice(index, 1);
});
updateValue(newValue);
setDialog(null);
};

const updateCell = (e, rowIndex, cellIndex) => {
const newValue = deepClone(value);
newValue.rows[rowIndex].cells[cellIndex] = e.target.value;
return updateValue(newValue);
};

return (
<div>
{dialog && (
<Dialog
header={`Remove ${dialog.type}`}
id="dialog-remove"
onClose={() => setDialog(null)}
zOffset={1000}
>
<Card padding={4}>
<Text>Are you sure you want to remove this {dialog.type}?</Text>
<Box marginTop={4}>
<Inline space={1} style={{ textAlign: 'right' }}>
<Button
text="Cancel"
mode="ghost"
onClick={() => setDialog(null)}
/>
<Button
text="Confirm"
tone="critical"
onClick={() => dialog.callback()}
/>
</Inline>
</Box>
</Card>
</Dialog>
)}
<Flex align="flex-start" justify="space-between">
<FormField
label={type.title}
markers={markers}
description={type.description}
level={level}
__unstable_changeIndicator={false}
/>
{value?.rows?.length && (
<TableMenu
addColumns={addColumns}
addRows={addRows}
remove={confirmRemoveTable}
placement="left"
/>
)}
</Flex>
{value?.rows?.length && <TableInput
rows={value.rows}
removeRow={confirmRemoveRow}
removeColumn={confirmRemoveColumn}
updateCell={updateCell}
/>}
{(!value || !value?.rows?.length) && (
<TableControl create={createTable} />
)}
</div>
);
};

export default forwardRef((props: RootProps, ref) => (
<TableComponent {...props} />
));
3 changes: 0 additions & 3 deletions src/component.css

This file was deleted.

Loading

0 comments on commit 7176b6f

Please sign in to comment.