Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Vizy fields in one entry are affected by each other's plugins #310

Open
f-elix opened this issue Sep 12, 2024 · 2 comments
Open

Comments

@f-elix
Copy link

f-elix commented Sep 12, 2024

Describe the bug

The way we can extend Vizy is by registering custom extensions (or nodes, marks, etc.) on the global Craft.Vizy.Config, which seems to be shared by all Vizy fields in an entry form.

The problem is that if one field registers a plugin and the other does not, the plugin is going to affect both of them because extensions are registered globally.

To give a specific example, I have the following plugin:

document.addEventListener('onVizyConfigReady', () => {
	const { Extension } = Craft.Vizy.Config.tiptap.core;

	const HardBreak = Extension.create({
		name: 'forceHardBreak',
		addKeyboardShortcuts() {
			return {
				Enter: () => {
					const plugins = this.editor.vizyField._.props.settings.vizyConfig.plugins ?? [];
					if (!plugins.includes('force-hard-break')) {
						return;
					}
					return this.editor.commands.setHardBreak();
				}
			};
		}
	});

	Craft.Vizy.Config.registerExtensions(() => {
		return [HardBreak];
	});
});

As you can see in the Enter function, I have to access some deeply nested prop to retrieve the current field config and check if the plugin is actually enabled. for that field.

This is a pretty hacky workaround.

I was wondering if there was a way to scope registered plugins to their respective fields? If not, I believe this should be addressed.

Steps to reproduce

  1. Register a plugin on one Vizy field
  2. Create another Vizy field without the plugin
  3. Add both fields on an entry type
  4. Create an entry
  5. Both fields will have the plugin enabled

Craft CMS version

5.4.3

Plugin version

3.0.3

Multi-site?

Yes

Additional context

This might not be the case with ALL plugins, it depends on what they register.

@engram-design
Copy link
Member

That's a good point and is unique to extensions. Unlike buttons or commands that have a separate item in the field config, extensions are a free-for-all.

That's going to mean that we need to register the extension with our plugin. So the format for registering the extension will need to change. Fortunately, this isn't a breaking change, and is backward-compatible. You'll just continue to have your extension globally enabled.

Change your code to:

Craft.Vizy.Config.registerExtensions((extensions, vizyInput) => {
    return [{ plugin: 'force-hard-break', extension: HardBreak }];
});

Notice the inclusion of the plugin the extension belongs to. I've also added vizyInput in this callback to allow you access to the instance we're registering things on, for all sorts of handling.

Updated for the next release. To get this early, run composer require verbb/vizy:"dev-craft-5 as 3.0.3".

@f-elix
Copy link
Author

f-elix commented Sep 13, 2024

Works prefectly! Thank you so much for the quick response :) I'll let you close the issue once the change is officially released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants