diff --git a/docs/src/demos/Experiments/FontColor/font-color.ts b/docs/src/demos/Experiments/FontColor/font-color.ts new file mode 100644 index 00000000..28ec9358 --- /dev/null +++ b/docs/src/demos/Experiments/FontColor/font-color.ts @@ -0,0 +1,73 @@ +import { Extension } from '@tiptap/core' +import '@tiptap/extension-text-style' + +type FontColorOptions = { + types: string[], +} + +declare module '@tiptap/core' { + interface Commands { + fontColor: { + /** + * Set the font color + */ + setFontColor: (fontColor: string) => ReturnType, + /** + * Unset the font color + */ + unsetFontColor: () => ReturnType, + } + } +} + +export const FontColor = Extension.create({ + name: 'fontColor', + + defaultOptions: { + types: ['textStyle'], + }, + + addGlobalAttributes() { + return [ + { + types: this.options.types, + attributes: { + fontColor: { + default: null, + renderHTML: attributes => { + if (!attributes.fontColor) { + return {} + } + + return { + style: `color: ${attributes.fontColor}`, + } + }, + parseHTML: element => { + console.log('FOO', element.style) + return { + fontColor: element.style.color.replace(/['"]+/g, ''), + } + }, + }, + }, + }, + ] + }, + + addCommands() { + return { + setFontColor: fontColor => ({ chain }) => { + return chain() + .setMark('textStyle', { fontColor }) + .run() + }, + unsetFontColor: () => ({ chain }) => { + return chain() + .setMark('textStyle', { fontColor: null }) + .removeEmptyTextStyle() + .run() + }, + } + }, +}) diff --git a/docs/src/demos/Experiments/FontColor/index.vue b/docs/src/demos/Experiments/FontColor/index.vue new file mode 100644 index 00000000..c2ed31fe --- /dev/null +++ b/docs/src/demos/Experiments/FontColor/index.vue @@ -0,0 +1,70 @@ + + + diff --git a/docs/src/docPages/experiments.md b/docs/src/docPages/experiments.md index c346b3ac..04595bc7 100644 --- a/docs/src/docPages/experiments.md +++ b/docs/src/docPages/experiments.md @@ -13,3 +13,4 @@ Congratulations! You’ve found our playground with a list of experiments. Be aw * [@tiptap/extension-collaboration-annotation](/experiments/collaboration-annotation) * [@tiptap/extension-trailing-node](/experiments/trailing-node) * [@tiptap/extension-figure](/experiments/figure) +* [@tiptap/extension-font-color](/experiments/font-color) diff --git a/docs/src/docPages/experiments/font-color.md b/docs/src/docPages/experiments/font-color.md new file mode 100644 index 00000000..a07416cb --- /dev/null +++ b/docs/src/docPages/experiments/font-color.md @@ -0,0 +1,32 @@ +# FontFamily +[![Version](https://img.shields.io/npm/v/@tiptap/extension-font-color.svg?label=version)](https://www.npmjs.com/package/@tiptap/extension-font-color) +[![Downloads](https://img.shields.io/npm/dm/@tiptap/extension-font-color.svg)](https://npmcharts.com/compare/@tiptap/extension-font-color?minimal=true) + +This extension enables you to set the font color in the editor. It uses the [`TextStyle`](/api/marks/text-style) mark, which renders a `` tag (and only that). The font color is applied as inline style then, for example ``. + +## Installation +```bash +# with npm +npm install @tiptap/extension-text-style @tiptap/extension-font-color + +# with Yarn +yarn add @tiptap/extension-text-style @tiptap/extension-font-color +``` + +This extension requires the [`TextStyle`](/api/marks/text-style) mark. + +## Settings +| Option | Type | Default | Description | +| ------ | ------- | --------------- | ------------------------------------------------------------------------ | +| types | `Array` | `['textStyle']` | A list of marks to which the font family attribute should be applied to. | + +## Commands +| Command | Parameters | Description | +| ---------- | ---------- | --------------------------------------------- | +| fontColor | fontColor | Applies the given font color as inline style | + +## Source code +[packages/extension-font-color/](https://github.com/ueberdosis/tiptap/blob/main/packages/extension-font-color/) + +## Usage +