A volto addon to turn Plone Volto into a decoupled editor for headless Plone.
Why Hydra? because it lets you have many frontends (heads) written in many framework all connected to the same body (plone) while still letting you edit the content directly into your frontend.
It is a GSoC project to prove with the following goals
It's Volto without having to learn any Volto.
You can try out the editing experience now by logging into https://hydra.pretagov.com and selecting one of the available preset frontend urls from dropdown or you can paste the url of frontend deployed for the demo site.
Available example frontends (go to examples
directory for source code):
Note: not everything works yet. Follow the progress on the Hydra Roadmap or the Hydra README
You can build your own frontend in your favourite frontend framework, deploy it and then submit a ticket to have it listed as one of the test frontends.
TODO: link to more documentation on creating a frontend using @plone/client
You can either run a local hydra instance (see below) or connect it directly to https://hydra.pretagov.com/++api++
If you are testing against https://hydra.pretagov.com/++api++ you will need to ensure you are running on https locally via a proxy to ensure there are no CORS errors
To test against a local hydra instance
1. Clone the Volto-Hydra Repository
Clone the Volto-Hydra repository from GitHub:
git clone https://github.com/collective/volto-hydra.git
cd volto-hydra
2. Start Volto-Hydra
Run the following command to start the Volto-Hydra site:
make start
You can also set your preset frontend URLs with environment variables, making them available in the admin UI. This allows you to switch between them seamlessly:
RAZZLE_DEFAULT_IFRAME_URL=http://localhost:3002,https://hydra-blogsite-nextjs.vercel.app pnpm start
You can find .env.example
at root directory of the project.
Ensure Volto is running on port 3000.
3. Start the Plone Backend
make backend-docker-start
Note : This will also set CORS_ALLOW_ORIGIN
to '*'
, so there are no cors error.
You can use one of the example frontends available at ./examples
directory.
make example-nextjs-admin
make example-nextjs-frontend
Use netlify or similar and make your frontend public and then let us know by creating a ticket and we will advertise your frontend on https://hydra.pretagov.com for others to test.
But be sure to subscribe to the project so you can keep your frontend updated with changes to the hydra api as more capabilities are added. If there are bugs lets us know.
As an integrator you have a choice on how nice you want the editor user experience to be. Each level requires more work to integrate but makes editing easier.
As the GSoC projects progresses more of these levels will be enabled so you can try them out. see Hydra GSoC project progresses
To switch to a different frontend in the Volto Hydra AdminUI, follow these steps:
Navigate to Personal Tools:
Go to Preferences:
Change Frontend URL:
This allows you to switch seamlessly between different frontend URLs for testing or editing purposes.
Note: Make sure the frontend URL is correct and accessible to avoid any CORS issues.
This is the most basic form of integration. You can
To do this you will include the hydra iframe bridge which creates a two way link between the hydra editor and your frontend.
?_edit=true
, checkout below to load hydra.js
asynchronously.access_token
parameter directly from the URL for the ploneClient
token option. Example Usage:
// nextjs 14 using ploneClient
import ploneClient from "@plone/client";
import { useQuery } from "@tanstack/react-query";
export default function Blog({ params }) {
// Extract token directly from the URL
const url = new URL(window.location.href);
const token = url.searchParams.get("access_token");
const client = ploneClient.initialize({
apiPath: "http://localhost:8080/Plone/", // Plone backend
token: token,
});
const { getContentQuery } = client;
const { data, isLoading } = useQuery(getContentQuery({ path: '/blogs' }));
if (isLoading) {
return <div>Loading...</div>;
}
return (
<div> {data.title}</div>
)
}
initBridge
from hydra.js.initBridge
and pass the origin of your adminUI as the argument to the initBridge method.https://hydra.pretagov.com
// In Layout.js or App.js
import { initBridge } from './hydra.js';
const bridge = initBridge("https://hydra.pretagov.com");
User Preferences
and paste in your local running frontend to test.
RAZZLE_DEFAULT_IFRAME_URL
on your hydra instance to have this frontend selectable by the user. Note: You can also pass an options object while initializing the bridge. Currently you can pass a List of allowed blocks (by default image & text blocks are allowed if not specified). E.g. :
// In Layout.js or App.js
import { initBridge } from './hydra.js';
const bridge = initBridge("https://hydra.pretagov.com", {allowedBlocks: ['slate', 'image', 'video']});
Since the script has a considerable size, it’s recommended to load the bridge only when necessary, such as in edit mode. To load the bridge asynchronously, add a function that checks if the bridge is already present. If it isn't, the function will load it and then call a callback function. This ensures the bridge is loaded only when needed.
function loadBridge(callback) {
const existingScript = document.getElementById("hydraBridge");
if (!existingScript) {
const script = document.createElement("script");
script.src = "./hydra.js";
script.id = "hydraBridge";
document.body.appendChild(script);
script.onload = () => {
callback();
};
} else {
callback();
}
}
// Initialize the bridge only inside the admin UI
if (window.location.search.includes('_edit=true')) {
loadBridge(() => {
const { initBridge } = window;
initBridge('https://hydra.pretagov.com');
});
}
If you wish to make the editing experience smoother you can register for onSave
and onRoute
callbacks to prevent reloads of the frontend
You will add data attributes to your rendered block html so hydra knows where they are on the page and it will automatically handle click events and show a quanta toolbar (TODO) and border (TODO) when selecting a block. Without this, you can still manage blocks via the blocks navigation in the sidebar.
It will allow you to naviate to the parent block (TODO) but not add, remove or move blocks unless live updates (level 3) is enabled.
Add the data-block-uid={<<BLOCK_UID>>}
attribute to your outer most container of the rendered block html.
The data-block-uid
requires the block's UID, which you need to provide in the outermost container of the block.
For example, if you are using ploneClient to fetch data
, it will be data.blocks_layout.items[x]
.
Now, Click on your blocks in iframe and the sidebar will show its settings.
Usage:
// Vanilla JS example to render Blocks
// Function to create the block list
function createBlockList(data) {
const blockList = document.createElement('ul');
data.blocks_layout.items.forEach(id => {
if (data.blocks[id]["@type"] === "slate") {
const slateValue = data.blocks[id].value;
const listItem = document.createElement('li');
listItem.className = 'blog-list-item';
listItem.setAttribute('data-block-uid', id); // Set Attribute to enable Clicking on Blocks
const pre = document.createElement('pre');
pre.className = 'pre-block';
pre.textContent = JSON.stringify(slateValue, null, 2);
listItem.appendChild(pre);
blockList.appendChild(listItem);
}
});
document.body.appendChild(blockList);
}
// Call the function to render the blocks
createBlockList(data);
You will need to subscribe to an onEditChange
event that will call the callback with the updated data.
The onEditChange
method listens for changes in the Hydra and triggers a callback with updated data.
The 'data' object follows the same format as you get from the ploneClient.
onEditChange takes following args: |
Args | Description |
---|---|---|
callback | A function to call with the updated data when a change is detected. |
Usage:
// the initial data (from ploneClient)
const initialData = data;
// Define the callback function
function handleEditChange(updatedData) {
console.log('Updated data:', updatedData);
}
// Set up the onEditChange listener
//After initiating the bridge you can use its onEditChange method
const bridge = initBridge('https://hydra.pretagov.com');
bridge.onEditChange(handleEditChange);
If you completed level 2 & 3 (made blocks clickable and enabled live updates) then the editor will automatically gain the management of blocks on the frontend using the quanta toolbar.
With Quanta toobar, you can use following features:
data-bloc-uid="<<BLOCK_UID>>>"
attribute) to add a block below the current block by choosing a type from BlockChooser popup.You will still need to edit the blocks themselves via the sidebar.
If you want to make the editing experience the most intuitive, you can enable real-time inplace editing, where an editor can change check, links or media by typing or clicking directly on your frontend instead of via fields on the sidebar.
You will add data attributes to where a blocks text is editable and where text formatting and features are locationed (like links)
and also subscribe to onBlockFieldChanged
events. This will enable handling fine grained
changes to text being edited such as turning text bold or creating a link. Hydra will notice where you have indicated a block field can
be clicked on and will automatically make it inplace editable handling typing, shortcuts
(slash (TODO),
enter (TODO) and bullets etc) and
selection (TODO](https://github.com/collective/volto-hydra/issues/31))for you.
TODO
TODO