Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
304 changes: 232 additions & 72 deletions README.md

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "restruct/silverstripe-shortcodable",
"description": "Provides a simple button to insert Shortcodes into the HTMLEditorField + an API for developers to define shortcodes",
"description": "A shortcode button and dialog for the Silverstripe WYSIWYG editor: CMS editors pick, fill in and edit shortcodes, shown as placeholder images, and developers make any class or DataObject shortcodable.",
"type": "silverstripe-vendormodule",
"keywords": [
"silverstripe",
"shortcode"
"silverstripe-module",
"silverstripe-cms",
"shortcode",
"shortcodes",
"tinymce",
"wysiwyg",
"htmleditor"
],
"license": "BSD-3-Clause",
"authors": [
Expand Down
Binary file added docs/images/dialog-edit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/dialog-types.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/editor-placeholders.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions tests/browser/fixtures/ScbShowcaseButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Restruct\ScbBrowser;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;

/**
* BROWSER-TEST FIXTURE ONLY - README screenshot demo: [button label="..." link="..." style="..."],
* a plain class with attribute fields in the dialog and the module's default placeholder.
* Registered only with SCB_README_SHOWCASE set (fixtures/_config/showcase.yml).
* See ScbYearShortcode for why this never loads in a real install.
*/
class ScbShowcaseButton
{
use Configurable;
use Extensible;
use Injectable;

private static $shortcode = 'button';

public static function parse_shortcode($attrs, $content = null, $parser = null, $shortcode = null, $info = [])
{
return sprintf(
'<a class="btn btn-%s" href="%s">%s</a>',
htmlspecialchars($attrs['style'] ?? 'primary'),
htmlspecialchars($attrs['link'] ?? '#'),
htmlspecialchars($attrs['label'] ?? '')
);
}

public function getShortcodeLabel(): string
{
return 'Call-to-action button';
}

public function getShortcodeFields(): FieldList
{
return FieldList::create(
TextField::create('label', 'Button text'),
TextField::create('link', 'Link'),
DropdownField::create('style', 'Style', [
'primary' => 'Primary (filled)',
'secondary' => 'Secondary (outline)',
])
);
}
}
106 changes: 106 additions & 0 deletions tests/browser/fixtures/ScbShowcaseContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Restruct\ScbBrowser;

use Page;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Environment;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataObject;

/**
* BROWSER-TEST FIXTURE ONLY - README screenshot demo: [contact id="N" show="..."], a DataObject
* shortcode (the dialog lists its records) with its own getShortcodePlaceHolder(), a small card.
*
* With SCB_README_SHOWCASE set it also seeds the demo content on every database build: two contacts
* and a published "Plan your visit" page whose Content uses all four showcase shortcodes.
* Registered only with that variable set (fixtures/_config/showcase.yml).
* See ScbYearShortcode for why this never loads in a real install.
*/
class ScbShowcaseContact extends DataObject
{
private static $table_name = 'ScbBrowser_ShowcaseContact';

private static $singular_name = 'Contact card';

private static $db = [
'Title' => 'Varchar',
'Phone' => 'Varchar',
];

private static $shortcode = 'contact';

public static function parse_shortcode($attrs, $content = null, $parser = null, $shortcode = null, $info = [])
{
$contact = static::get()->byID((int) ($attrs['id'] ?? 0));
if (!$contact) {
return '';
}
$html = '<strong>' . htmlspecialchars($contact->Title) . '</strong>';
if (($attrs['show'] ?? 'all') === 'all') {
$html .= ' ' . htmlspecialchars($contact->Phone);
}
return '<span class="contact">' . $html . '</span>';
}

public function getShortcodeFields(): FieldList
{
return FieldList::create(
DropdownField::create('show', 'Show', [
'all' => 'Name and phone number',
'name' => 'Name only',
])
);
}

/**
* Its own placeholder: a contact card drawn as SVG, with the record's name. Returned as a
* response, which the placeholder endpoint passes on.
*/
public function getShortcodePlaceHolder($attributes)
{
$name = htmlspecialchars((string) $this->Title, ENT_QUOTES | ENT_XML1, 'UTF-8');
$detail = ($attributes['show'] ?? 'all') === 'all'
? htmlspecialchars((string) $this->Phone, ENT_QUOTES | ENT_XML1, 'UTF-8')
: 'name only';
$svg = <<<SVG
<svg xmlns="http://www.w3.org/2000/svg" width="250" height="56" viewBox="0 0 250 56">
<rect x="0.5" y="0.5" width="249" height="55" rx="6" fill="#f3f7fa" stroke="#9fb4c7"/>
<circle cx="28" cy="28" r="15" fill="#338dc1"/>
<circle cx="28" cy="23" r="5" fill="#fff"/>
<path d="M19 37a9 7 0 0 1 18 0z" fill="#fff"/>
<text x="54" y="24" font-family="Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" fill="#1f2b36">{$name}</text>
<text x="54" y="42" font-family="Helvetica, Arial, sans-serif" font-size="12" fill="#5a6b7b">{$detail}</text>
</svg>
SVG;
return HTTPResponse::create($svg)->addHeader('Content-Type', 'image/svg+xml');
}

public function requireDefaultRecords()
{
parent::requireDefaultRecords();
if (!Environment::getEnv('SCB_README_SHOWCASE')) {
return;
}

$ids = [];
foreach (['Visitor centre' => '555-0142', 'Press office' => '555-0177'] as $title => $phone) {
$contact = static::get()->filter('Title', $title)->first() ?: static::create(['Title' => $title]);
$contact->Phone = $phone;
$contact->write();
$ids[$title] = $contact->ID;
}

$page = Page::get()->filter('URLSegment', 'plan-your-visit')->first()
?: Page::create(['Title' => 'Plan your visit', 'URLSegment' => 'plan-your-visit']);
$page->Content = '<p>We are open every day of the year. Annual passes run until 31 December [year].</p>'
. '<p>Groups of ten or more can book a guided tour: '
. '[button label="Book a tour" link="/tours" style="primary"]</p>'
. '<h2>Stay in touch</h2>'
. '<p>[signup_form]</p>'
. '<p>Questions about your visit? Call [contact id="' . $ids['Visitor centre'] . '" show="all"]</p>';
$page->write();
$page->publishRecursive();
}
}
40 changes: 40 additions & 0 deletions tests/browser/fixtures/ScbShowcaseSignup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Restruct\ScbBrowser;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;

/**
* BROWSER-TEST FIXTURE ONLY - README screenshot demo: [signup_form], a block-level shortcode
* ($shortcode_close_parent) with the default placeholder sized through placeholder_settings.
* Registered only with SCB_README_SHOWCASE set (fixtures/_config/showcase.yml).
* See ScbYearShortcode for why this never loads in a real install.
*/
class ScbShowcaseSignup
{
use Configurable;
use Extensible;
use Injectable;

private static $shortcode = 'signup_form';

private static $shortcode_close_parent = true;

private static $placeholder_settings = [
'width' => 560,
'height' => 80,
];

public static function parse_shortcode($attrs, $content = null, $parser = null, $shortcode = null, $info = [])
{
return '<form class="signup"><label>Email <input type="email" name="email"></label>'
. '<button type="submit">Sign up</button></form>';
}

public function getShortcodeLabel(): string
{
return 'Newsletter sign-up form';
}
}
31 changes: 31 additions & 0 deletions tests/browser/fixtures/ScbShowcaseYear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Restruct\ScbBrowser;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;

/**
* BROWSER-TEST FIXTURE ONLY - README screenshot demo: [year], no attributes, the module's default
* placeholder. Registered only with SCB_README_SHOWCASE set (fixtures/_config/showcase.yml).
* See ScbYearShortcode for why this never loads in a real install.
*/
class ScbShowcaseYear
{
use Configurable;
use Extensible;
use Injectable;

private static $shortcode = 'year';

public static function parse_shortcode($attrs, $content = null, $parser = null, $shortcode = null, $info = [])
{
return date('Y');
}

public function getShortcodeLabel(): string
{
return 'Current year';
}
}
5 changes: 4 additions & 1 deletion tests/browser/fixtures/_config/shortcodable.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
---
Name: scb-browser-fixtures
After: '#shortcodable'
Except:
envvarset: SCB_README_SHOWCASE
---
# BROWSER-TEST FIXTURE ONLY - copied to the scratch host's app/_config/browser-fixtures/ by the
# runner (fixture YAML in app/src would never be read). Registers the fixture shortcodes the way a
# project does, through config.
# project does, through config. Not on a README screenshot host (SCB_README_SHOWCASE set), whose
# dialog lists only the showcase shortcodes (showcase.yml).
Shortcodable\Shortcodable:
shortcodable_classes:
- Restruct\ScbBrowser\ScbYearShortcode
Expand Down
16 changes: 16 additions & 0 deletions tests/browser/fixtures/_config/showcase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
Name: scb-showcase-fixtures
After: '#shortcodable'
Only:
envvarset: SCB_README_SHOWCASE
---
# BROWSER-TEST FIXTURE ONLY - the README screenshots' demo shortcodes (ScbShowcase*). Registered
# only when SCB_README_SHOWCASE is set (tests/browser/screenshots/README.md sets it through
# BROWSER_ENV), so the browser specs and CI, which check the dialog's list option by option, never
# see them.
Shortcodable\Shortcodable:
shortcodable_classes:
- Restruct\ScbBrowser\ScbShowcaseYear
- Restruct\ScbBrowser\ScbShowcaseButton
- Restruct\ScbBrowser\ScbShowcaseContact
- Restruct\ScbBrowser\ScbShowcaseSignup
36 changes: 36 additions & 0 deletions tests/browser/screenshots/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# README screenshots

`readme.shot.ts` writes the PNGs in `docs/images/` that the module README shows. It is not a test
suite: it runs with its own config, `screenshots/playwright.config.ts`, which the browser tests and
CI never load, so a normal test run never rewrites the images. Never edit the PNGs by hand;
regenerate them.

What it needs:

- a Silverstripe 6 host with this module and `silverstripe/htmleditor-tinymce` installed, and the
browser fixtures from `tests/browser/fixtures/` copied into it (PHP into `app/src/`, `_config/`
into `app/_config/`);
- `SCB_README_SHOWCASE=1` in that host's environment, then the database built. With it set,
`fixtures/_config/showcase.yml` registers the four showcase shortcodes (`ScbShowcase*`) instead
of the browser specs' own, and `ScbShowcaseContact` seeds two contacts and a published
"Plan your visit" page that uses all four;
- `BROWSER_TARGET_URLS` pointing at that host (only the `ss6` target is used, else the first).

The shared runner in the maintainers' workspace does all of that (`targets.sh` already requires
TinyMCE on Silverstripe 6):

```bash
BROWSER_ONLY=ss6 BROWSER_ENV="SCB_README_SHOWCASE=1" \
tools/browser/run.sh silverstripe-shortcodable --config=screenshots/playwright.config.ts
```

The runner rewrites the host's `BROWSER_ENV` block on every run, so a later spec run on the same
host, without the variable, gets the specs' own shortcodes back (the seeded page stays; the specs
do not look at it).

Against a host you started yourself, from `tests/browser/`:

```bash
npm ci
BROWSER_TARGET_URLS="ss6=http://127.0.0.1:8898" npx playwright test --config=screenshots/playwright.config.ts
```
60 changes: 60 additions & 0 deletions tests/browser/screenshots/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { defineConfig, devices } from '@playwright/test';
import * as path from 'path';

// README screenshots, NOT tests: writes docs/images/*.png from a Silverstripe 6 host with the
// browser fixtures loaded and SCB_README_SHOWCASE set. Kept apart from ../playwright.config.ts on
// purpose: the browser tests (and CI) run that config, and must never rewrite docs/images/.
// How to run: README.md next to this file.

// The same BROWSER_TARGET_URLS the runner passes, but only ONE target is used: two targets would
// overwrite each other's images. ss6 when present, else the first one.
const targets = (process.env.BROWSER_TARGET_URLS ?? '')
.split(',')
.map((pair) => pair.trim())
.filter(Boolean)
.map((pair) => {
const [name, url] = pair.split('=');
return { name, url };
});
const target = targets.find((t) => t.name === 'ss6') ?? targets[0];

if (!target) {
throw new Error(
'BROWSER_TARGET_URLS is empty. Run through tools/browser/run.sh (see screenshots/README.md), ' +
'or set it by hand, e.g. BROWSER_TARGET_URLS="ss6=http://127.0.0.1:8898"',
);
}

// The login setup saves the session under tests/browser/.auth/ (it is run from tests/browser/).
const browserDir = path.join(__dirname, '..');

export default defineConfig({
testDir: browserDir,
outputDir: path.join(browserDir, 'test-results'),
retries: 0,
// One admin session, shared; see ../playwright.config.ts for why that means one worker.
workers: 1,
timeout: 60_000,
expect: { timeout: 10_000 },
reporter: [['list']],
use: {
...devices['Desktop Chrome'],
// ~1280 wide, at 1x, so the PNGs are the width a README shows them at.
viewport: { width: 1280, height: 800 },
deviceScaleFactor: 1,
screenshot: 'only-on-failure',
},
projects: [
{
name: `${target.name}-login`,
testMatch: /specs\/login\.setup\.ts/,
use: { baseURL: target.url },
},
{
name: target.name,
testMatch: /screenshots\/.*\.shot\.ts/,
dependencies: [`${target.name}-login`],
use: { baseURL: target.url, storageState: path.join(browserDir, '.auth', `${target.name}.json`) },
},
],
});
Loading
Loading