Skip to content

Commit

Permalink
Feat: make class and id global attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
awcodes committed Dec 19, 2023
1 parent 0c04dbe commit 36f3b32
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 33 deletions.
60 changes: 30 additions & 30 deletions resources/dist/filament-tiptap-editor.js

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions resources/js/extensions/ClassExtension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Extension} from '@tiptap/core'

export const ClassExtension = Extension.create({
name: 'classExtension',

addGlobalAttributes() {
return [
{
types: [
'heading',
'paragraph',
'link',
'image',
'listItem',
'bulletList',
'orderedList',
'table',
'tableHeader',
'tableRow',
'tableCell',
'textStyle',
],
attributes: {
class: {
default: null,
parseHTML: element => element.getAttribute('class'),
renderHTML: attributes => {
if (!attributes.class) {
return {}
}
return {
class: attributes.class
}
},
},
},
},
]
}
})
30 changes: 30 additions & 0 deletions resources/js/extensions/IdExtension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Extension} from '@tiptap/core'

export const IdExtension = Extension.create({
name: 'idExtension',

addGlobalAttributes() {
return [
{
types: [
'heading',
'link',
],
attributes: {
id: {
default: null,
parseHTML: element => element.getAttribute('id'),
renderHTML: attributes => {
if (!attributes.id) {
return {}
}
return {
id: attributes.id
}
},
},
},
},
]
}
})
2 changes: 2 additions & 0 deletions resources/js/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export { GridBuilderColumn } from "./GridBuilder/GridBuilderColumn";
export { DragAndDropExtension } from "./DragAndDrop.js";
export { TiptapBlock } from "./TiptapBlock.js";
export { MergeTag } from "./MergeTag.js";
export { ClassExtension } from "./ClassExtension.js";
export { IdExtension } from "./IdExtension.js";
19 changes: 17 additions & 2 deletions resources/js/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import HorizontalRule from "@tiptap/extension-horizontal-rule";
import Italic from "@tiptap/extension-italic";
import ListItem from "@tiptap/extension-list-item";
import OrderedList from "@tiptap/extension-ordered-list";
import Paragraph from "@tiptap/extension-paragraph";
import Placeholder from "@tiptap/extension-placeholder";
import Strike from "@tiptap/extension-strike";
import Subscript from "@tiptap/extension-subscript";
Expand All @@ -31,7 +32,6 @@ import {
Lead,
CustomLink,
CustomImage,
CustomParagraph,
CustomTextAlign,
Small,
Grid,
Expand All @@ -51,6 +51,8 @@ import {
Video,
TiptapBlock,
DragAndDropExtension,
ClassExtension,
IdExtension,
} from "./extensions";
import {lowlight} from "lowlight/lib/common";
import { HexBase } from 'vanilla-colorful/lib/entrypoints/hex';
Expand Down Expand Up @@ -161,7 +163,20 @@ export default function tiptap({
return tool.id;
})

let exts = [Document, Text, CustomParagraph, Dropcursor, Gapcursor, HardBreak, History, TextStyle, TiptapBlock, DragAndDropExtension];
let exts = [
Document,
Text,
Paragraph,
Dropcursor,
Gapcursor,
HardBreak,
History,
TextStyle,
TiptapBlock,
DragAndDropExtension,
ClassExtension,
IdExtension,
];

if (placeholder && (! disabled)) {
exts.push(Placeholder.configure({ placeholder }));
Expand Down
50 changes: 50 additions & 0 deletions src/Extensions/Extensions/ClassExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace FilamentTiptapEditor\Extensions\Extensions;

use Tiptap\Core\Extension;
use Tiptap\Utils\InlineStyle;

class ClassExtension extends Extension
{
public static $name = 'classExtension';

public function addGlobalAttributes(): array
{
return [
[
'types' => [
'heading',
'paragraph',
'link',
'image',
'listItem',
'bulletList',
'orderedList',
'table',
'tableHeader',
'tableRow',
'tableCell',
'textStyle',
],
'attributes' => [
'class' => [
'default' => null,
'parseHTML' => function ($DOMNode) {
return InlineStyle::getAttribute($DOMNode, 'class') ?? false;
},
'renderHTML' => function ($attributes) {
if (! $attributes->class) {
return null;
}

return [
'class' => $attributes->class,
];
},
],
],
],
];
}
}
40 changes: 40 additions & 0 deletions src/Extensions/Extensions/IdExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace FilamentTiptapEditor\Extensions\Extensions;

use Tiptap\Core\Extension;
use Tiptap\Utils\InlineStyle;

class IdExtension extends Extension
{
public static $name = 'idExtension';

public function addGlobalAttributes(): array
{
return [
[
'types' => [
'heading',
'link',
],
'attributes' => [
'id' => [
'default' => null,
'parseHTML' => function ($DOMNode) {
return InlineStyle::getAttribute($DOMNode, 'id') ?? false;
},
'renderHTML' => function ($attributes) {
if (! $attributes->id) {
return null;
}

return [
'id' => $attributes->id,
];
},
],
],
],
];
}
}
3 changes: 2 additions & 1 deletion src/TiptapConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public function getExtensions(): array

return [
new StarterKit([
'paragraph' => false,
'listItem' => false,
]),
new TextStyle(),
new Extensions\TextAlign([
'types' => ['heading', 'paragraph'],
]),
new Extensions\ClassExtension(),
new Extensions\IdExtension(),
new Extensions\Color(),
new CodeBlockHighlight(),
new Nodes\Paragraph(),
Expand Down

0 comments on commit 36f3b32

Please sign in to comment.