Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkwinkelmann committed Dec 18, 2021
0 parents commit c12a105
Show file tree
Hide file tree
Showing 23 changed files with 4,602 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
[*.md]
indent_size = 2
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
js/src export-ignore
.git* export-ignore

js/dist/*.js -diff
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
vendor
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Clark Winkelmann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Roll a Die

[![MIT license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/clarkwinkelmann/flarum-ext-roll-die/blob/master/LICENSE.md) [![Latest Stable Version](https://img.shields.io/packagist/v/clarkwinkelmann/flarum-ext-roll-die.svg)](https://packagist.org/packages/clarkwinkelmann/flarum-ext-roll-die) [![Total Downloads](https://img.shields.io/packagist/dt/clarkwinkelmann/flarum-ext-roll-die.svg)](https://packagist.org/packages/clarkwinkelmann/flarum-ext-roll-die) [![Donate](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/clarkwinkelmann)

> This extension is experimental. Don't use it for anything too serious just yet. Please report any issue you find!
To roll a die, insert any of the die emoji (🎲 ⚀ ⚁ ⚂ ⚃ ⚄ ⚅) on its own line in the post.
The emoji can also be inside a block quote.

The random numbers are picked when the post is saved.
When the post is edited, the numbers don't change, and continue to be applied in the same order (first die emoji in the post will keep the same number, even if it was moved in the content, same for second, etc.).
This behavior can be modified in the admin panel.

The native emoji from the user system fonts are used, so the look can differ between devices.
If the emoji fails to render, the random number is still accessible through a tooltip.

There might be issues with die emojis inserted inside of markdown or bbcode if they are alone on their line but not actually at the first level of the content.
When this happens, the numbers could end up misaligned with the emojis during rendering.

The random numbers are not actually stored in the post body, but as a special post attribute.
They are then mapped to the emojis in the frontend based on their order in the body.

If the post is rendered outside the Flarum web discussion, the original emoji inserted by the post author will be visible: emails, push notifications, etc.
But it will not be stylised like the random dice in the discussion.

If the extension is disabled, the original emoji inserted by the post author will become visible again instead of the randomized die.

## Installation

composer require clarkwinkelmann/flarum-ext-roll-die

## Support

This extension is under **minimal maintenance**.

It was developed for a client and released as open-source for the benefit of the community.
I might publish simple bugfixes or compatibility updates for free.

You can [contact me](https://clarkwinkelmann.com/flarum) to sponsor additional features or updates.

Support is offered on a "best effort" basis through the Flarum community thread.

## Links

- [GitHub](https://github.com/clarkwinkelmann/flarum-ext-roll-die)
- [Packagist](https://packagist.org/packages/clarkwinkelmann/flarum-ext-roll-die)
- [Discuss](https://discuss.flarum.org/d/29698)
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "clarkwinkelmann/flarum-ext-roll-die",
"description": "Make die emoji roll a random number",
"keywords": [
"flarum",
"roll",
"die",
"dice"
],
"type": "flarum-extension",
"license": "MIT",
"require": {
"flarum/core": "^1.0"
},
"authors": [
{
"name": "Clark Winkelmann",
"email": "[email protected]",
"homepage": "https://clarkwinkelmann.com/",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/clarkwinkelmann/flarum-ext-roll-die/issues",
"source": "https://github.com/clarkwinkelmann/flarum-ext-roll-die",
"forum": "https://discuss.flarum.org/d/29698"
},
"extra": {
"flarum-extension": {
"title": "Roll a Die",
"category": "feature",
"icon": {
"name": "fas fa-dice",
"backgroundColor": "#684ba6",
"color": "#fff"
}
}
}
}
70 changes: 70 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace ClarkWinkelmann\RollDie;

use Flarum\Api\Serializer\PostSerializer;
use Flarum\Extend;
use Flarum\Post\Event\Saving;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Arr;

return [
(new Extend\Frontend('admin'))
->js(__DIR__ . '/js/dist/admin.js'),

(new Extend\Frontend('forum'))
->css(__DIR__ . '/less/forum.less')
->js(__DIR__ . '/js/dist/forum.js'),

new Extend\Locales(__DIR__ . '/locale'),

(new Extend\Event())
->listen(Saving::class, function (Saving $event) {
$attributes = Arr::get($event->data, 'attributes', []);

if (!Arr::exists($attributes, 'content')) {
return;
}

/**
* @var $settings SettingsRepositoryInterface
*/
$settings = resolve(SettingsRepositoryInterface::class);

$rolls = [];

if ($event->post->dice_rolls && !$settings->get('roll-die.clearOnEdit')) {
$rolls = str_split($event->post->dice_rolls);
}

// We don't actually care about permission to edit post content
// If the user isn't allowed or the content is invalid, the save will fail anyway
// and the values generated here will never be persisted to the database
// Negative lookahead is necessary to match multiple emojis immediately following each other
// Allow die in block quotes, because it's just too difficult to exclude this in the HTML parsing in the frontend
preg_match_all('~(?:[\n\r]|^)(>\s*)?(🎲|⚀|⚁|⚂|⚃|⚄|⚅)(?=[\n\r]|$)~', Arr::get($attributes, 'content'), $matches);

$numberOfRolls = count($matches[0]);

// We only generate additional numbers
// Even if some of the dice have been edited out, we keep the value in case they are added back in
for ($i = count($rolls); $i < $numberOfRolls; $i++) {
$rolls[] = (string)random_int(1, 6);
}

// The rolls are saved as characters in a single string
$event->post->dice_rolls = implode('', $rolls);
}),

(new Extend\ApiSerializer(PostSerializer::class))
->attributes(function (PostSerializer $serializer, Post $post): array {
if ($post->dice_rolls) {
return [
'diceRolls' => $post->dice_rolls,
];
}

return [];
}),
];
1 change: 1 addition & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/admin';
2 changes: 2 additions & 0 deletions js/dist/admin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions js/dist/admin.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions js/dist/forum.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c12a105

Please sign in to comment.