├── src ├── setupTests.js ├── utils │ ├── contants.js │ ├── testExample.js │ ├── parseRawInputByMarkdownIt.js │ ├── util.js │ ├── withTable.js │ └── table.js ├── assets │ ├── icons │ │ ├── layout-row-line.svg │ │ ├── layout-top-line.svg │ │ ├── layout-bottom-line.svg │ │ ├── layout-column-line.svg │ │ ├── layout-left-line.svg │ │ ├── layout-right-line.svg │ │ ├── table-2.svg │ │ └── grid-line.svg │ └── logo.svg ├── pages │ ├── App.css │ ├── App.test.js │ └── App.jsx ├── locales │ ├── i18n.js │ ├── zh-CN │ │ └── translation.json │ └── en │ │ └── translation.json ├── index.css ├── components │ ├── ToolBar.jsx │ └── TableEditor.jsx └── index.js ├── demo.gif ├── logo.png ├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json └── index.html ├── tailwind.config.js ├── config ├── jest │ ├── cssTransform.js │ ├── babelTransform.js │ └── fileTransform.js ├── pnpTs.js ├── getHttpsConfig.js ├── paths.js ├── modules.js ├── env.js ├── webpackDevServer.config.js └── webpack.config.js ├── .gitignore ├── README-zh_CN.md ├── craco.config.js ├── .github ├── workflows │ └── release.yml └── ISSUE_TEMPLATE │ └── bug_report.md ├── .releaserc ├── README.md ├── LICENSE ├── scripts ├── test.js ├── start.js └── build.js ├── CHANGELOG.md └── package.json /src/setupTests.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | window.logseq = { 4 | App: {}, 5 | Editor: {}, 6 | } -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haydenull/logseq-plugin-markdown-table/HEAD/demo.gif -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haydenull/logseq-plugin-markdown-table/HEAD/logo.png -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haydenull/logseq-plugin-markdown-table/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haydenull/logseq-plugin-markdown-table/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haydenull/logseq-plugin-markdown-table/HEAD/public/logo512.png -------------------------------------------------------------------------------- /src/utils/contants.js: -------------------------------------------------------------------------------- 1 | export const DEFAULT_TABLE = '|||\n|--|--|\n|||' 2 | 3 | export const tableLineReg = /^\|.+\|$/ -------------------------------------------------------------------------------- /src/assets/icons/layout-row-line.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/icons/layout-top-line.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'], 3 | darkMode: false, // or 'media' or 'class' 4 | theme: { 5 | extend: {}, 6 | }, 7 | variants: { 8 | extend: {}, 9 | }, 10 | plugins: [], 11 | } 12 | -------------------------------------------------------------------------------- /src/assets/icons/layout-bottom-line.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/icons/layout-column-line.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/icons/layout-left-line.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/icons/layout-right-line.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/icons/table-2.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/icons/grid-line.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /config/jest/cssTransform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // This is a custom Jest transformer turning style imports into empty objects. 4 | // http://facebook.github.io/jest/docs/en/webpack.html 5 | 6 | module.exports = { 7 | process() { 8 | return 'module.exports = {};'; 9 | }, 10 | getCacheKey() { 11 | // The output is always the same. 12 | return 'cssTransform'; 13 | }, 14 | }; 15 | -------------------------------------------------------------------------------- /src/pages/App.css: -------------------------------------------------------------------------------- 1 | table, th, td { 2 | @apply border; 3 | @apply border-gray-200; 4 | } 5 | table{ 6 | border-collapse: collapse; 7 | width: 100%; 8 | overflow: hidden; 9 | @apply shadow-md; 10 | @apply table-auto; 11 | @apply rounded-md; 12 | } 13 | td { 14 | padding: 10px; 15 | } 16 | 17 | tr:nth-child(odd) { 18 | @apply bg-gray-300; 19 | } 20 | tr:nth-child(even) { 21 | @apply bg-gray-50; 22 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README-zh_CN.md: -------------------------------------------------------------------------------- 1 | # Logseq Markdown Table Editor Plugin 2 | 3 | [English](./README.md) | 简体中文 4 | 5 | 编辑器参考[https://codesandbox.io/s/yt8jc](https://codesandbox.io/s/yt8jc) 6 | 7 | ## 使用前请注意 8 | - **多个表格,需要用空行隔开,否则会识别为一个表格。** 9 | 10 | ## 示例 11 | ![demo](./demo.gif) 12 | 13 | ## 快捷键 14 | - `Tab`: 移动光标到下一个单元格 15 | - `Shift + Tab`: 移动光标到上一个单元格 16 | - `Shift + Enter`: 在当前单元格换行 17 | 18 | ## 开发 19 | ```shell 20 | $ yarn 21 | 22 | $ yarn start 23 | ``` -------------------------------------------------------------------------------- /craco.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | style: { 3 | postcss: { 4 | plugins: [ 5 | require('tailwindcss'), 6 | require('autoprefixer'), 7 | ], 8 | }, 9 | }, 10 | babel: { 11 | plugins: [['import', { 12 | libraryName: 'antd', 13 | libraryDirectory: 'es', 14 | style: 'css', 15 | }]], 16 | }, 17 | // babel: { 18 | // loaderOptions: { 19 | // exclude: /node_modules/, 20 | // } 21 | // } 22 | } -------------------------------------------------------------------------------- /src/locales/i18n.js: -------------------------------------------------------------------------------- 1 | import i18n from "i18next"; 2 | import { initReactI18next } from "react-i18next"; 3 | 4 | import translationEN from "./en/translation.json"; 5 | import translationZhCN from "./zh-CN/translation.json"; 6 | 7 | const resources = { 8 | en: { 9 | translation: translationEN, 10 | }, 11 | 'zh-CN': { 12 | translation: translationZhCN, 13 | }, 14 | }; 15 | 16 | i18n.use(initReactI18next).init({ 17 | resources, 18 | lng: "en", 19 | fallbackLng: "en", 20 | interpolation: { 21 | escapeValue: false, 22 | }, 23 | }); 24 | 25 | export default i18n; 26 | -------------------------------------------------------------------------------- /src/locales/zh-CN/translation.json: -------------------------------------------------------------------------------- 1 | { 2 | "Markdown Table Editor": "Markdown 表格编辑器", 3 | "Markdown table editor only support markdown": "Markdown 表格编辑器仅支持 Markdown 格式", 4 | "Add New Table": "添加新表格", 5 | "Cancel": "取消", 6 | "Confirm": "确认", 7 | "insert row above": "在上方插入新行", 8 | "insert row below": "在下方插入新行", 9 | "delete row": "删除行", 10 | "insert column before": "在左侧插入新列", 11 | "insert column after": "在右侧插入新列", 12 | "delete column": "删除列", 13 | "uuid error": "uuid 错误", 14 | "markdown table overwrite success": "markdown 表格覆盖成功", 15 | "markdown table overwrite error": "markdown 表格覆盖失败" 16 | } -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /config/jest/babelTransform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const babelJest = require('babel-jest'); 4 | 5 | const hasJsxRuntime = (() => { 6 | if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') { 7 | return false; 8 | } 9 | 10 | try { 11 | require.resolve('react/jsx-runtime'); 12 | return true; 13 | } catch (e) { 14 | return false; 15 | } 16 | })(); 17 | 18 | module.exports = babelJest.createTransformer({ 19 | presets: [ 20 | [ 21 | require.resolve('babel-preset-react-app'), 22 | { 23 | runtime: hasJsxRuntime ? 'automatic' : 'classic', 24 | }, 25 | ], 26 | ], 27 | babelrc: false, 28 | configFile: false, 29 | }); 30 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | /* body { 6 | margin: 0; 7 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 8 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 9 | sans-serif; 10 | -webkit-font-smoothing: antialiased; 11 | -moz-osx-font-smoothing: grayscale; 12 | } 13 | 14 | code { 15 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 16 | monospace; 17 | } */ 18 | 19 | /* body { 20 | @apply px-8 py-4; 21 | @apply bg-gray-100; 22 | } */ 23 | html, body { 24 | margin: 0; 25 | padding: 0; 26 | background: transparent; 27 | } 28 | -------------------------------------------------------------------------------- /src/locales/en/translation.json: -------------------------------------------------------------------------------- 1 | { 2 | "Markdown Table Editor": "Markdown Table Editor", 3 | "Markdown table editor only support markdown": "Markdown table editor only support markdown format", 4 | "Add New Table": "Add New Table", 5 | "Cancel": "Cancel", 6 | "Confirm": "Confirm", 7 | "insert row above": "insert row above", 8 | "insert row below": "insert row below", 9 | "delete row": "delete row", 10 | "insert column before": "insert column before", 11 | "insert column after": "insert column after", 12 | "delete column": "delete column", 13 | "uuid error": "uuid error", 14 | "markdown table overwrite success": "markdown table overwrite success", 15 | "markdown table overwrite error": "markdown table overwrite error" 16 | } -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | branches: 8 | - main 9 | jobs: 10 | release: 11 | name: Release 12 | runs-on: ubuntu-20.04 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v3 16 | with: 17 | fetch-depth: 0 18 | - name: Setup Node.js 19 | uses: actions/setup-node@v3 20 | with: 21 | node-version: 14 22 | - name: Install dependencies 23 | run: yarn 24 | - name: Build 25 | run: yarn build 26 | - name: Release 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | run: npx semantic-release@18.0.0 30 | -------------------------------------------------------------------------------- /config/pnpTs.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const { resolveModuleName } = require('ts-pnp'); 4 | 5 | exports.resolveModuleName = ( 6 | typescript, 7 | moduleName, 8 | containingFile, 9 | compilerOptions, 10 | resolutionHost 11 | ) => { 12 | return resolveModuleName( 13 | moduleName, 14 | containingFile, 15 | compilerOptions, 16 | resolutionHost, 17 | typescript.resolveModuleName 18 | ); 19 | }; 20 | 21 | exports.resolveTypeReferenceDirective = ( 22 | typescript, 23 | moduleName, 24 | containingFile, 25 | compilerOptions, 26 | resolutionHost 27 | ) => { 28 | return resolveModuleName( 29 | moduleName, 30 | containingFile, 31 | compilerOptions, 32 | resolutionHost, 33 | typescript.resolveTypeReferenceDirective 34 | ); 35 | }; 36 | -------------------------------------------------------------------------------- /.releaserc: -------------------------------------------------------------------------------- 1 | { 2 | "branches": "main", 3 | "plugins": [ 4 | "@semantic-release/commit-analyzer", 5 | "@semantic-release/release-notes-generator", 6 | "@semantic-release/changelog", 7 | [ 8 | "@semantic-release/npm", 9 | { "npmPublish": false } 10 | ], 11 | [ 12 | "@semantic-release/git", 13 | { 14 | "assets": ["CHANGELOG.md", "package.json"], 15 | "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" 16 | } 17 | ], 18 | [ 19 | "@semantic-release/exec", 20 | { 21 | "prepareCmd": "zip -qq -r logseq-plugin-markdown-table-${nextRelease.version}.zip package.json logo.png build/" 22 | } 23 | ], 24 | [ 25 | "@semantic-release/github", 26 | { 27 | "assets": ["logseq-plugin-markdown-table-*.zip"] 28 | } 29 | ] 30 | ] 31 | } -------------------------------------------------------------------------------- /src/utils/testExample.js: -------------------------------------------------------------------------------- 1 | export const empty = '' 2 | 3 | export const onlyText = 'normal text\nnor' 4 | 5 | export const onlyOneTable = '|title1|title2|\n|--|--|\n|content1|content2|' 6 | 7 | export const tableWithTextBefore = 'foo\n`yarn install`\n|title1|title2|\n|--|--|\n|content1|content2|' 8 | 9 | export const tableWithTextBeforeAndAfter = 'foo\n`yarn install`\n|title1|title2|\n|--|--|\n|content1|content2|\n**bold**\nother' 10 | 11 | export const multipleTables = 'foo\n`yarn install`\n|title1|title2|\n|--|--|\n|content1|content2|\n|**bold**|\nother\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\ntest text' 12 | 13 | export const longTables = 'foo\n`yarn install`\n|title1[:br]new line|title2|\n|--|--|\n|content1|content2|\n\n**bold**\nother\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\n\n|table2|table2|\n|--|--|\n|contentB|contentB|\n|contentB|contentB|\n|contentB|contentB|\n|contentB|contentB|' -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Logseq Markdown Table Editor Plugin 2 | 3 | [![latest release version](https://img.shields.io/github/v/release/haydenull/logseq-plugin-markdown-table)](https://github.com/haydenull/logseq-plugin-markdown-table/releases) 4 | [![License](https://img.shields.io/github/license/haydenull/logseq-plugin-markdown-table?color=blue)](https://github.com/haydenull/logseq-plugin-markdown-table/blob/main/LICENSE) 5 | 6 | English | [简体中文](./README-zh_CN.md) 7 | 8 | reference[https://codesandbox.io/s/yt8jc](https://codesandbox.io/s/yt8jc) 9 | 10 | ## Read Before Use 11 | - **Multiple tables need to be separated by blank lines, otherwise they will be recognized as one table** 12 | 13 | ## demo 14 | ![demo](./demo.gif) 15 | 16 | ## Shortcuts 17 | - `Tab`: Move cursor to the next cell 18 | - `Shift + Tab`: Move cursor to the previous cell 19 | - `Shift + Enter`: Break line in the current cell 20 | 21 | ## Development 22 | ```shell 23 | $ yarn 24 | 25 | $ yarn start 26 | ``` -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Hayden Chen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/utils/parseRawInputByMarkdownIt.js: -------------------------------------------------------------------------------- 1 | import MarkdownIt from "markdown-it" 2 | 3 | import { tableLineReg, DEFAULT_TABLE } from "./contants" 4 | 5 | const md = new MarkdownIt() 6 | 7 | const parseMarkdownTable = (input) => { 8 | const strArr = input.split('\n') 9 | // token https://github.com/markdown-it/markdown-it/blob/master/lib/token.js 10 | const tokenList = md.parse(input, {}) 11 | 12 | const tables = tokenList 13 | .filter(token => token?.type === 'table_open') 14 | .map(token => { 15 | // map is Sourse map, format [startLine, endLine] 16 | let [startLine , endLine] = token.map 17 | const endLineStr = strArr[endLine] 18 | 19 | if (tableLineReg.test(endLineStr)) return token.map 20 | 21 | // fix markdown-it table must have a newLine after 22 | let trueEndLine = -1 23 | while (endLine > startLine) { 24 | if (tableLineReg.test(strArr[endLine])) { 25 | trueEndLine = endLine 26 | break 27 | } 28 | endLine-- 29 | } 30 | return [startLine, trueEndLine + 1] 31 | }) 32 | 33 | if (tables?.length === 0) { 34 | console.warn('[faiz:] === No Table Found') 35 | input += `${input === '' ? '' : '\n'}${DEFAULT_TABLE}` 36 | return parseMarkdownTable(input) 37 | } 38 | 39 | return tables 40 | } 41 | 42 | export default parseMarkdownTable -------------------------------------------------------------------------------- /config/jest/fileTransform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const camelcase = require('camelcase'); 5 | 6 | // This is a custom Jest transformer turning file imports into filenames. 7 | // http://facebook.github.io/jest/docs/en/webpack.html 8 | 9 | module.exports = { 10 | process(src, filename) { 11 | const assetFilename = JSON.stringify(path.basename(filename)); 12 | 13 | if (filename.match(/\.svg$/)) { 14 | // Based on how SVGR generates a component name: 15 | // https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6 16 | const pascalCaseFilename = camelcase(path.parse(filename).name, { 17 | pascalCase: true, 18 | }); 19 | const componentName = `Svg${pascalCaseFilename}`; 20 | return `const React = require('react'); 21 | module.exports = { 22 | __esModule: true, 23 | default: ${assetFilename}, 24 | ReactComponent: React.forwardRef(function ${componentName}(props, ref) { 25 | return { 26 | $$typeof: Symbol.for('react.element'), 27 | type: 'svg', 28 | ref: ref, 29 | key: null, 30 | props: Object.assign({}, props, { 31 | children: ${assetFilename} 32 | }) 33 | }; 34 | }), 35 | };`; 36 | } 37 | 38 | return `module.exports = ${assetFilename};`; 39 | }, 40 | }; 41 | -------------------------------------------------------------------------------- /src/utils/util.js: -------------------------------------------------------------------------------- 1 | import { DEFAULT_TABLE } from './contants' 2 | 3 | export const stringToSlateValue = (str = '') => { 4 | str = str || DEFAULT_TABLE 5 | // 将 [:br] 转为换行符 6 | const _arr = str.trim().split('\n').filter(Boolean).map(_str => _str.replaceAll('[:br]', '\n')) 7 | const contentArr = [_arr[0]].concat(_arr.slice(2)) 8 | const res = contentArr.map(rowStr => { 9 | const rowArr = rowStr.trim().split('|') 10 | return rowArr.slice(1, rowArr.length - 1) 11 | }) 12 | return createTableNode(res) 13 | } 14 | 15 | export const slateValueToString = (slateVal) => { 16 | let rowStrs = Array.from(slateVal.children, (row) => { 17 | const cells = Array.from(row.children, (cell) => { 18 | // 将换行符替换为 [:br] 19 | return cell.children[0].text?.replaceAll('\n', '[:br]') 20 | }).join('|') 21 | return `|${cells}|` 22 | }) 23 | rowStrs.splice(1, 0, `|${Array.from(slateVal.children[0].children, () => '--').join('|')}|`) 24 | return rowStrs.join('\n') 25 | } 26 | 27 | const createRow = (cellText) => { 28 | const newRow = Array.from(cellText, (value) => createTableCell(value)) 29 | return { 30 | type: "table-row", 31 | children: newRow 32 | } 33 | } 34 | 35 | const createTableCell = (text) => { 36 | return { 37 | type: "table-cell", 38 | children: [{ text }] 39 | } 40 | } 41 | 42 | export const createTableNode = (cellText) => { 43 | const tableChildren = Array.from(cellText, (value) => createRow(value)) 44 | let tableNode = { type: "table", children: tableChildren } 45 | return tableNode 46 | } -------------------------------------------------------------------------------- /scripts/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Do this as the first thing so that any code reading it knows the right env. 4 | process.env.BABEL_ENV = 'test'; 5 | process.env.NODE_ENV = 'test'; 6 | process.env.PUBLIC_URL = ''; 7 | 8 | // Makes the script crash on unhandled rejections instead of silently 9 | // ignoring them. In the future, promise rejections that are not handled will 10 | // terminate the Node.js process with a non-zero exit code. 11 | process.on('unhandledRejection', err => { 12 | throw err; 13 | }); 14 | 15 | // Ensure environment variables are read. 16 | require('../config/env'); 17 | 18 | 19 | const jest = require('jest'); 20 | const execSync = require('child_process').execSync; 21 | let argv = process.argv.slice(2); 22 | 23 | function isInGitRepository() { 24 | try { 25 | execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' }); 26 | return true; 27 | } catch (e) { 28 | return false; 29 | } 30 | } 31 | 32 | function isInMercurialRepository() { 33 | try { 34 | execSync('hg --cwd . root', { stdio: 'ignore' }); 35 | return true; 36 | } catch (e) { 37 | return false; 38 | } 39 | } 40 | 41 | // Watch unless on CI or explicitly running all tests 42 | if ( 43 | !process.env.CI && 44 | argv.indexOf('--watchAll') === -1 && 45 | argv.indexOf('--watchAll=false') === -1 46 | ) { 47 | // https://github.com/facebook/create-react-app/issues/5210 48 | const hasSourceControl = isInGitRepository() || isInMercurialRepository(); 49 | argv.push(hasSourceControl ? '--watch' : '--watchAll'); 50 | } 51 | 52 | 53 | jest.run(argv); 54 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /config/getHttpsConfig.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const crypto = require('crypto'); 6 | const chalk = require('react-dev-utils/chalk'); 7 | const paths = require('./paths'); 8 | 9 | // Ensure the certificate and key provided are valid and if not 10 | // throw an easy to debug error 11 | function validateKeyAndCerts({ cert, key, keyFile, crtFile }) { 12 | let encrypted; 13 | try { 14 | // publicEncrypt will throw an error with an invalid cert 15 | encrypted = crypto.publicEncrypt(cert, Buffer.from('test')); 16 | } catch (err) { 17 | throw new Error( 18 | `The certificate "${chalk.yellow(crtFile)}" is invalid.\n${err.message}` 19 | ); 20 | } 21 | 22 | try { 23 | // privateDecrypt will throw an error with an invalid key 24 | crypto.privateDecrypt(key, encrypted); 25 | } catch (err) { 26 | throw new Error( 27 | `The certificate key "${chalk.yellow(keyFile)}" is invalid.\n${ 28 | err.message 29 | }` 30 | ); 31 | } 32 | } 33 | 34 | // Read file and throw an error if it doesn't exist 35 | function readEnvFile(file, type) { 36 | if (!fs.existsSync(file)) { 37 | throw new Error( 38 | `You specified ${chalk.cyan( 39 | type 40 | )} in your env, but the file "${chalk.yellow(file)}" can't be found.` 41 | ); 42 | } 43 | return fs.readFileSync(file); 44 | } 45 | 46 | // Get the https config 47 | // Return cert files if provided in env, otherwise just true or false 48 | function getHttpsConfig() { 49 | const { SSL_CRT_FILE, SSL_KEY_FILE, HTTPS } = process.env; 50 | const isHttps = HTTPS === 'true'; 51 | 52 | if (isHttps && SSL_CRT_FILE && SSL_KEY_FILE) { 53 | const crtFile = path.resolve(paths.appPath, SSL_CRT_FILE); 54 | const keyFile = path.resolve(paths.appPath, SSL_KEY_FILE); 55 | const config = { 56 | cert: readEnvFile(crtFile, 'SSL_CRT_FILE'), 57 | key: readEnvFile(keyFile, 'SSL_KEY_FILE'), 58 | }; 59 | 60 | validateKeyAndCerts({ ...config, keyFile, crtFile }); 61 | return config; 62 | } 63 | return isHttps; 64 | } 65 | 66 | module.exports = getHttpsConfig; 67 | -------------------------------------------------------------------------------- /src/components/ToolBar.jsx: -------------------------------------------------------------------------------- 1 | import { useSlate } from 'slate-react' 2 | import { InsertRowAboveOutlined, InsertRowBelowOutlined, InsertRowLeftOutlined, InsertRowRightOutlined, DeleteRowOutlined, DeleteColumnOutlined } from '@ant-design/icons' 3 | import { Tooltip } from 'antd' 4 | import { useTranslation } from 'react-i18next' 5 | 6 | import { TableUtil } from '../utils/table' 7 | 8 | const ToolBar = () => { 9 | const { t } = useTranslation() 10 | const editor = useSlate() 11 | 12 | const table = new TableUtil(editor) 13 | 14 | const handleButtonClick = (e, action) => { 15 | e.preventDefault() 16 | table.edit(action) 17 | } 18 | 19 | return ( 20 |
21 | 22 | handleButtonClick(e, 'insert-row-above')} /> 23 | 24 | 25 | handleButtonClick(e, 'insert-row-below')} /> 26 | 27 | 28 | handleButtonClick(e, 'delete-row')} /> 29 | 30 | 31 |
32 | 33 | 34 | handleButtonClick(e, 'insert-column-above')} /> 35 | 36 | 37 | handleButtonClick(e, 'insert-column-below')} /> 38 | 39 | 40 | handleButtonClick(e, 'delete-column')} /> 41 | 42 | 43 |
44 | ) 45 | } 46 | 47 | export default ToolBar 48 | -------------------------------------------------------------------------------- /src/utils/withTable.js: -------------------------------------------------------------------------------- 1 | import { Editor, Range, Point, Element } from 'slate' 2 | 3 | const withTable = (editor) => { 4 | const { deleteBackward, deleteForward, insertBreak } = editor 5 | 6 | editor.deleteBackward = unit => { 7 | const { selection } = editor; 8 | if (selection) { 9 | const [cell] = Editor.nodes(editor, { 10 | match: n => 11 | !Editor.isEditor(n) && 12 | Element.isElement(n) && 13 | n.type === 'table-cell', 14 | }) 15 | const prevNodePath = Editor.before(editor, selection) 16 | 17 | const [tableNode] = Editor.nodes(editor, { 18 | at: prevNodePath, 19 | match: n => !Editor.isEditor(n) && Element.isElement && n.type === 'table-cell' 20 | }) 21 | 22 | if (cell) { 23 | const [, cellPath] = cell 24 | 25 | const start = Editor.start(editor, cellPath) 26 | if (Point.equals(selection.anchor, start)) { 27 | return 28 | } 29 | } 30 | if (!cell && tableNode) { 31 | return 32 | } 33 | } 34 | 35 | deleteBackward(unit) 36 | } 37 | editor.deleteForward = unit => { 38 | const { selection } = editor 39 | if (selection && Range.isCollapsed(selection)) { 40 | const [cell] = Editor.nodes(editor, { 41 | match: n => 42 | !Editor.isEditor(n) && 43 | Element.isElement(n) && 44 | n.type === 'table-cell', 45 | }) 46 | 47 | const prevNodePath = Editor.after(editor, selection) 48 | const [tableNode] = Editor.nodes(editor, { 49 | at: prevNodePath, 50 | match: n => !Editor.isEditor(n) && Element.isElement && n.type === 'table-cell' 51 | }) 52 | 53 | 54 | if (cell) { 55 | const [, cellPath] = cell 56 | const end = Editor.end(editor, cellPath) 57 | 58 | if (Point.equals(selection.anchor, end)) { 59 | return 60 | } 61 | } 62 | if (!cell && tableNode) { 63 | return 64 | } 65 | } 66 | 67 | deleteForward(unit) 68 | } 69 | 70 | editor.insertBreak = () => { 71 | const { selection } = editor 72 | if (selection) { 73 | const [table] = Editor.nodes(editor, { 74 | match: n => 75 | !Editor.isEditor(n) && 76 | Element.isElement(n) && 77 | n.type === 'table', 78 | }) 79 | 80 | if (table) { 81 | return 82 | } 83 | } 84 | 85 | insertBreak() 86 | } 87 | return editor; 88 | } 89 | 90 | 91 | export default withTable; -------------------------------------------------------------------------------- /src/assets/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /config/paths.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const fs = require('fs'); 5 | const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath'); 6 | 7 | // Make sure any symlinks in the project folder are resolved: 8 | // https://github.com/facebook/create-react-app/issues/637 9 | const appDirectory = fs.realpathSync(process.cwd()); 10 | const resolveApp = relativePath => path.resolve(appDirectory, relativePath); 11 | 12 | // We use `PUBLIC_URL` environment variable or "homepage" field to infer 13 | // "public path" at which the app is served. 14 | // webpack needs to know it to put the right