Created
February 12, 2025 02:52
-
-
Save engram-design/31d13d6dc617e0fa0f5fe5e2c674d1fb to your computer and use it in GitHub Desktop.
Vizy Custom Bullet List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"buttons": ["custom-bullet-list"], | |
"plugins": ["custom-bullet-list"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const inputRegex = /^\s*([-+*])\s$/; | |
document.addEventListener('onVizyConfigReady', (e) => { | |
const { Mark, Node, mergeAttributes, wrappingInputRule } = Craft.Vizy.Config.tiptap.core; | |
const CustomBulletList = Node.create({ | |
name: 'customBulletList', | |
group: 'block list', | |
addOptions() { | |
return { | |
itemTypeName: 'listItem', | |
HTMLAttributes: { | |
class: ['my-bullet-list'] | |
}, | |
keepMarks: false, | |
keepAttributes: false, | |
} | |
}, | |
content() { | |
return `${this.options.itemTypeName}+` | |
}, | |
parseHTML() { | |
return [ | |
{ | |
tag: 'ul', | |
}, | |
] | |
}, | |
renderHTML({ HTMLAttributes }) { | |
return ['ul', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] | |
}, | |
addCommands() { | |
return { | |
toggleCustomBulletList: () => ({ commands, chain }) => { | |
if (this.options.keepAttributes) { | |
return chain().toggleList(this.name, this.options.itemTypeName, this.options.keepMarks).updateAttributes(ListItemName, this.editor.getAttributes(TextStyleName)).run() | |
} | |
return commands.toggleList(this.name, this.options.itemTypeName, this.options.keepMarks) | |
}, | |
} | |
}, | |
addKeyboardShortcuts() { | |
return { | |
'Mod-Shift-8': () => this.editor.commands.toggleCustomBulletList(), | |
} | |
}, | |
addInputRules() { | |
let inputRule = wrappingInputRule({ | |
find: inputRegex, | |
type: this.type, | |
}) | |
if (this.options.keepMarks || this.options.keepAttributes) { | |
inputRule = wrappingInputRule({ | |
find: inputRegex, | |
type: this.type, | |
keepMarks: this.options.keepMarks, | |
keepAttributes: this.options.keepAttributes, | |
getAttributes: () => { return this.editor.getAttributes(TextStyleName) }, | |
editor: this.editor, | |
}) | |
} | |
return [ | |
inputRule, | |
] | |
}, | |
}); | |
Craft.Vizy.Config.registerExtensions((extensions) => { | |
return [ | |
CustomBulletList, | |
]; | |
}); | |
Craft.Vizy.Config.registerButtons((buttons) => { | |
return [{ | |
name: 'custom-bullet-list', | |
svg: 'list-ul', | |
title: Craft.t('vizy', 'Custom Bullet List'), | |
action: (editor) => { return editor.chain().focus().toggleCustomBulletList().run(); }, | |
isActive: (editor) => { return editor.isActive('customBulletList'); }, | |
}]; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\vizymodule\nodes; | |
use verbb\vizy\nodes\BulletList; | |
class CustomBulletList extends BulletList | |
{ | |
// Properties | |
// ========================================================================= | |
public static ?string $type = 'customBulletList'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\vizymodule\assets; | |
use craft\web\AssetBundle; | |
use craft\web\assets\cp\CpAsset; | |
use verbb\vizy\web\assets\field\VizyAsset; | |
class CustomBulletListAsset extends AssetBundle | |
{ | |
// Public Methods | |
// ========================================================================= | |
public function init(): void | |
{ | |
$this->sourcePath = '@modules/vizymodule/assets'; | |
$this->depends = [ | |
CpAsset::class, | |
VizyAsset::class, | |
]; | |
$this->js = [ | |
'js/custom-bullet-list.js', | |
]; | |
parent::init(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\vizymodule; | |
use yii\base\Event; | |
use verbb\vizy\services\Nodes; | |
use verbb\vizy\events\RegisterPluginEvent; | |
use verbb\vizy\fields\VizyField; | |
use verbb\vizy\base\Plugin as VizyPlugin; | |
use verbb\vizy\events\RegisterNodesEvent; | |
class VizyModule extends Module | |
{ | |
// Public Methods | |
// ========================================================================= | |
public function init() | |
{ | |
parent::init(); | |
Event::on(VizyField::class, VizyField::EVENT_REGISTER_PLUGINS, function(RegisterPluginEvent $event) { | |
$event->plugins[] = new VizyPlugin([ | |
'handle' => 'custom-bullet-list', | |
'assetBundle' => CustomBulletListAsset::class, | |
]); | |
}); | |
Event::on(Nodes::class, Nodes::EVENT_REGISTER_NODES, function(RegisterNodesEvent $event) { | |
$event->nodes[] = CustomBulletList::class; | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment