0.0.2 • Published 2 years ago

@pdejs/core v0.0.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

About PDEJS

PDEJS is inspired by Preact's reconciler implements. We made the implements working standalone for constucting plugins of Editor.js through declarative ui programming.

Why using Preact's reconciler

We first tried to incorporate React philosophy into the data management in the Editor.js block. React is a UI library includes a simple architecture concerned with the View of MVC model. In applications such as block editors that read and write a lot of data, an approach like React is useful because managing data through DOM manipulation is very labor intensive. Therefore, we used React's mechanisms such as reconciliation and hooks to make data management simple and explicit to improve the development experience.

However, React implementation is complex, even for a reconciler or hooks implementation. Even if the exact same code could be run standalone, it would require time spent understanding the code, making ongoing maintenance impractical. Therefore, we decide to utilze Preact for incorporating the React philosophy without relying on React.

Preact is a library known as a lightweight alternative of React. However, we consider that the superiority of the Preact is not that it is a lighter library than React, but it rewrites React with a minimum amount of code to make it more readable. Hence, we worked on cloning the implements from Preact's reconciler and adjusted for Editor.js plugin development.

Getting started

Required Node.js v16 or later.

With TypeScript(recommended)

Install packages.

npm i @editorjs/editorjs @pdejs/core
npm i --save-dev typescript

Add tsconfig.json.

{
  "compilerOptions": {
    "target": "es2016",
    "lib": ["ESNext", "DOM", "DOM.Iterable"],
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "jsx": "react"
  }
}

Write your first plugin like shown below.

/* @jsx h */
import {
  h,
  useMount,
  useWatch,
  useState,
  PDJSX,
  createPlugin,
} from '@pdejs/core';

const Plugin = () => {
  const toolbox: PDJSX.ToolAttributes['static_get_toolbox'] = {
    title: 'Simple',
    icon: '⚔️',
  };
  const save: PDJSX.ToolAttributes['save'] = (blockContent) => {
    return blockContent.innerText;
  };

  const [inputValue, setInputValue] = useState('');
  const [submitValue, setSubmitValue] = useState('');
  const handleFormSubmit = (event: Event) => {
    event.preventDefault();
    setSubmitValue(inputValue);
  };
  const handleInputChange = (event: Event) => {
    if (event.target instanceof HTMLInputElement) {
      setInputValue(event.target.value);
    }
  };

  useMount(() => {
    console.log('[@pdejs/simple] is ready to work!');
  });
  useWatch(() => {
    console.log(`[@pdejs/simple] submitted : `, submitValue);
  }, [submitValue]);

  return (
    <tool save={save} static_get_toolbox={toolbox}>
      <div>
        <form onSubmit={handleFormSubmit}>
          <label>
            type something→
            <input onChange={handleInputChange} value={inputValue} />
          </label>
          <button>submit</button>
        </form>
        <div>
          <span>submitted: </span>
          <span>{submitValue}</span>
        </div>
      </div>
    </tool>
  );
};

export const Simple = createPlugin(<Plugin />);

Create files for completing to setup. We recommend to use vite for hosting locally.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example - Simple</title>
  </head>
  <body>
    <div id="canvas"></div>
    <button id="save">save</button>
    <script type="module" src="/main.ts"></script>
  </body>
</html>
/* @jsx h */
import { Simple } from './plugin';
import EditorJS from '@editorjs/editorjs';

import './style.css';

const canvasElm = document.querySelector<HTMLDivElement>('#canvas');
const saveElm = document.querySelector<HTMLButtonElement>('#save');

if (!canvasElm) {
  throw new Error('Could not find the element#canvas');
}

const editor = new EditorJS({
  holder: canvasElm,
  tools: {
    simple: {
      class: Simple,
    },
  },
  onReady: () => {
    console.log('Editor.js is ready to work!');
  },
  onChange: (_, event) => {
    console.log("Now I know that Editor's content changed!", event);
  },
  autofocus: true,
  placeholder: "Let's write an awesome story!",
});

saveElm?.addEventListener('click', () => {
  editor
    .save()
    .then((outputData) => {
      console.log('saved: ', outputData);
    })
    .catch((error) => {
      console.log('save failded: ', error);
    });
});

It works :tada:

image

See the source for more information.

With JavaScript

Install packages.

npm i @editorjs/editorjs @pdejs/core
npm i --save-dev @babel/core @babel/cli @babel/plugin-transform-react-jsx @babel/preset-env

Add .babelrc.

{
  "presets": [["@babel/preset-env"]],
  "plugins": ["@babel/plugin-transform-react-jsx"]
}

Other steps are almostly the same way of the With TypeScript.

If you do not want to use @jsx h

Modify compilerOptions.jsxFactory in tsconfig.json or add @babel/plugin-transform-react-jsx for modifying pragma as follows.

tsconfig.json (with tsc)

{
  "compilerOptions": {
    "jsxFactory": "h"
  }
}

.babelrc (with @babel-transform-react-jsx)

{
  "plugins": [["transform-react-jsx", { "pragma": "h" }]]
}

APIs

List up the available APIs.

Core Functions

h(type, config, children)

Function for parsing JSX to object.

createPlugin(element)

Adapter function for Editor.js. It receives PDEJS's JSXElement as an argument and returns Editor.js plugin class.

JSXElement

See types for more information of JSXElement props.

<tool>

JSXElement for Editor.js Tools API.

PropDescriptionRequiredData FormatRelated Docs
childrenis children of PDEJS Element.:ballot_box_with_check:Arrayhttps://preactjs.com/guide/v10/api-reference/#h--createelement
saveextracts Block data from the UI.:ballot_box_with_check:Functionhttps://editorjs.io/tools-api#save
validatevalidates Block data after saving.Functionhttps://editorjs.io/tools-api#validate
renderSettingsis object for the settings UI.Objecthttps://editorjs.io/tools-api#rendersettings
destroyclears Tools stuff(cache, variables, events).Functionhttps://editorjs.io/tools-api#destroy
onPastehandles content pasted.Functionhttps://editorjs.io/tools-api#onpaste
mergespecifies how to merge two similar Blocks.Functionhttps://editorjs.io/tools-api#merge
static_get_pasteConfigallows your Tool to substitute pasted contents.Objecthttps://editorjs.io/tools-api#pasteconfig
static_get_sanitizeallows to clean unwanted HTMLElement or attributes.Objecthttps://editorjs.io/tools-api#sanitize
static_get_toolboxdecides icon and title. REQUIRED if Tools should be added to the toolbox.Objecthttps://editorjs.io/tools-api#toolbox
static_get_shortcutregisters a shortcut command.Stringhttps://editorjs.io/tools-api#shortcut
static_get_conversionConfigdecides that Tool can be converted into/form anothor Tool.Objecthttps://editorjs.io/tools-api#conversionconfig
static_get_enableLineBreakshandles Enter keydowns if it's set true.Booleanhttps://editorjs.io/tools-api#enablelinebreaks
static_get_isReadOnlySupportedis a flag for supporting the read-only mode.Booleanhttps://editorjs.io/tools-api#isreadonlysupported

<inlineTool>

JSXElement for Editor.js Inline Tools API.

PropDescriptionRequiredData FormatRelated Docs
childrenis children of PDEJS Element.:ballot_box_with_check:Arrayhttps://preactjs.com/guide/v10/api-reference/#h--createelement
surroundworks with selected range.:ballot_box_with_check:Functionhttps://editorjs.io/inline-tools-api-1#surround
checkStategets Tool's activated state by selected range.:ballot_box_with_check:Functionhttps://editorjs.io/inline-tools-api-1#checkstate
renderActionscreate additional element.Functionhttps://editorjs.io/inline-tools-api-1#renderactions
clearclears Tools stuff.Functionhttps://editorjs.io/inline-tools-api-1#clear
get_shortcutsets a shortcut.Stringhttps://editorjs.io/inline-tools-api-1#shortcut
static_get_isInlinespecifies Tool as Inline Toolbar Tool.:ballot_box_with_check:Boolean(true)https://editorjs.io/inline-tools-api-1#isinline
static_sanitizesanitizer rules.Functionhttps://editorjs.io/inline-tools-api-1#sanitize
static_titledecides Tool's title.Functionhttps://editorjs.io/inline-tools-api-1#title

<blockTune>

JSXElement for Editor.js Block Tunes API

PropDescriptionRequiredData FormatRelated Docs
childrenis children of PDEJS Element.:ballot_box_with_check:Arrayhttps://preactjs.com/guide/v10/api-reference/#h--createelement
savesaves Tune's state.Functionhttps://editorjs.io/block-tunes-api#save
wrapwraps Block's content element.Functionhttps://editorjs.io/block-tunes-api#wrap
static_get_isTunespecifies Tool as Block Tune.Boolean(true)https://editorjs.io/block-tunes-api#static-get-istune
static_preparemakes any preparations required for Tune.Functionhttps://editorjs.io/block-tunes-api#prepare
static_resetresets the value of static_prepare.Functionhttps://editorjs.io/block-tunes-api#reset

Hook

useReducer(reducer, initialState, initilizer)

Update state by provided reducer.

useState(initialState)

Returns state and state updater.

useMount(callback)

Execute callback when mounting DOM.

useWatch(callback, deps)

Execute callback when deps are changed.

useConstructor()

UNSTABLE HOOK

Be able to get the value of Editor.js plugin class constructor.

Roadmap

  • Add types for custom JSX elements
  • Add a parser for JSX and syntax of Editor.js tools
  • Add unit & integration testing
  • Add implements of diff or reconcile
  • Add functions for transforming JSX nodes to plugin class syntax
  • A11y support

Contributing

git clone https://github.com/cam-inc/pde.js.git && cd pde.js && npm run preflight

License

Apache-2.0 License

Inspired