diff --git a/src/Field.js b/src/Field.js new file mode 100644 index 0000000..5a49bbc --- /dev/null +++ b/src/Field.js @@ -0,0 +1,48 @@ +/* eslint-disable class-methods-use-this */ +// ::- The type of field that `FieldPrompt` expects to be passed to it. +export class Field { + // :: (Object) + // Create a field with the given options. Options support by all + // field types are: + // + // **`value`**`: ?any` + // : The starting value for the field. + // + // **`label`**`: string` + // : The label for the field. + // + // **`required`**`: ?bool` + // : Whether the field is required. + // + // **`validate`**`: ?(any) → ?string` + // : A function to validate the given value. Should return an + // error message if it is not valid. + constructor(options) { + this.options = options; + } + + // render:: (state: EditorState, props: Object) → dom.Node + // Render the field to the DOM. Should be implemented by all subclasses. + + // :: (dom.Node) → any + // Read the field's value from its DOM node. + read(dom) { + return dom.value; + } + + // :: (any) → ?string + // A field-type-specific validation function. + validateType() {} + + validate(value) { + if (!value && this.options.required) return 'Required field'; + return ( + this.validateType(value) || + (this.options.validate && this.options.validate(value)) + ); + } + + clean(value) { + return this.options.clean ? this.options.clean(value) : value; + } +} diff --git a/src/Placeholder.js b/src/Placeholder.js new file mode 100644 index 0000000..a2a75a0 --- /dev/null +++ b/src/Placeholder.js @@ -0,0 +1,27 @@ +import { Plugin } from 'prosemirror-state'; +import { Decoration, DecorationSet } from 'prosemirror-view'; + +export default (placeholderText = '') => { + return new Plugin({ + props: { + decorations: state => { + const decorations = []; + + const decorate = (node, pos) => { + if (node.type.isBlock && node.childCount === 0) { + decorations.push( + Decoration.node(pos, pos + node.nodeSize, { + class: 'empty-node', + 'data-placeholder': placeholderText, + }) + ); + } + }; + + state.doc.descendants(decorate); + + return DecorationSet.create(state.doc, decorations); + }, + }, + }); +}; diff --git a/src/SearchField.js b/src/SearchField.js new file mode 100644 index 0000000..5c57235 --- /dev/null +++ b/src/SearchField.js @@ -0,0 +1,18 @@ +import Field from './Field'; + +// ::- A field class for dropdown fields based on a plain `