1. Go to this page and download the library: Download hyvor/phrosemirror library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hyvor / phrosemirror example snippets
use Hyvor\Phrosemirror\Types\Schema;
$schema = new Schema(
[
new Doc,
new Text,
new Paragraph,
new Blockquote,
new Image
],
[
new Strong,
new Italic,
]
);
use Hyvor\Phrosemirror\Types\NodeType;
class Doc extends NodeType
{
public string $name = 'doc';
public ?string $content = 'block+';
}
class Paragraph extends NodeType
{
public string $name = 'paragraph';
public ?string $content = 'inline*';
public string $group = 'block';
}
use Hyvor\Phrosemirror\Types\MarkType;
class Strong extends MarkType
{
public string $name = 'strong';
}
use Hyvor\Phrosemirror\Types\AttrsType;
class ImageAttrs extends AttrsType
{
public string $src;
public ?string $alt;
}
class ImageAttrs extends AttrsType
{
public string $src = 'https://hyvor.com/placeholder.png';
}
use Hyvor\Phrosemirror\Types\NodeType;
class Image extends NodeType
{
// ...
public string $attrs = ImageAttrs::class;
}
use Hyvor\Phrosemirror\Types\Schema;
use Hyvor\Phrosemirror\Document\Document;
$schema = new Schema($nodes, $marks);
$json = '{}'; // <- this is the JSON from the front-end
$document = Document::fromJson($schema, $json);
namespace Hyvor\Phrosemirror\Document;
use Hyvor\Phrosemirror\Types\NodeType;
use Hyvor\Phrosemirror\Types\AttrsType;
class Node
{
public NodeType $type;
public AttrsType $attrs;
public Fragment $content;
public Mark[] $marks;
}
foreach ($node->content as $child) {
if ($child->isOfType(Image::class)) {
echo "I found an image!";
}
}
// images
$node->getNodes(Image::class);
// all nodes (including TextNodes)
$node->getNodes();
// nodes of multiple types
$node->getNodes([Paragraph::class, Blockquote::class]);
// images (only direct children)
$node->getNodes(Image::class, false);
// links
$node->getMarks(Link::class);
// all marks
$node->getMarks();
// multiple types
$node->getMarks([Strong::class, Italic::class]);
// without nesting (marks of the current node only)
$node->getMarks(Link::class, false);
namespace Hyvor\Phrosemirror\Document;
use Hyvor\Phrosemirror\Types\MarkType;
use Hyvor\Phrosemirror\Types\AttrsType;
class Mark
{
public MarkType $type;
public AttrsType $attrs;
}
$fragment = $node->content();
// READ
$fragment->first(); // Node | null
$fragment->last(); // Node | null
$fragment->nth(2); // Node | null
$fragment->count(); // int
// get all Nodes in the Fragment as an array
$fragment->all(); // Node[]
// loop through each node
$fragment->each(fn (Node $node) => false);
// WRITE (Be careful, these methods changes the document)
$fragment->addNodeToStart($node);
$fragment->addNodeToEnd($node);
$fragment->addNode($node); // same as addNodeToEnd
$fragment->setNodes($nodes);
$fragment->map(fn (Node $node) => $node); // update nodes in a callback
use Hyvor\Phrosemirror\Document\Node;
use Hyvor\Phrosemirror\Types\NodeType;
class Paragraph extends NodeType
{
public function toHtml(Node $node, string $children) : string
{
return "<p>$children</p>";
}
}
use Hyvor\Phrosemirror\Document\Node;
use Hyvor\Phrosemirror\Types\NodeType;
class Image extends NodeType
{
public function toHtml(Node $node, string $children) : string
{
$src = $node->attr('src');
return "<img src=\"$src\">$children</p>";
}
}
use Hyvor\Phrosemirror\Converters\HtmlParser\HtmlParser;use Hyvor\Phrosemirror\Converters\HtmlParser\ParserRule;
$schema = new Schema($nodes, $marks); // this is the same schema you create for the document
$parser = new HtmlParser($schema, [
new ParserRule(tag: 'p', node: 'paragraph'),
new ParserRule(tag: '#text', node: 'text'),
// ... other rules
])
$doc = $parser->parse($html);
use Hyvor\Phrosemirror\Types\NodeType;
use Hyvor\Phrosemirror\Converters\HtmlParser\ParserRule;
use Hyvor\Phrosemirror\Document\Node;
class Paragraph extends NodeType
{
public string $name = 'paragraph';
public ?string $content = 'inline*';
public string $group = 'block';
public function toHtml(Node $node, string $children): string
{
return "<p>$children</p>";
}
public function fromHtml(): array
{
return [
new ParserRule(tag: 'p'),
];
}
}