Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
WiseClock committed Mar 17, 2017
0 parents commit 6b5715d
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
js/*/node_modules
45 changes: 45 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace WiseClock\Reply2See;

use DirectoryIterator;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Event\ConfigureClientView;
use Flarum\Event\ConfigureLocales;
use Flarum\Event\ConfigureFormatter;

return function (Dispatcher $events)
{
$events->subscribe(Listeners\LoadSettingsFromDatabase::class);

$events->listen(ConfigureLocales::class, function (ConfigureLocales $event)
{
foreach (new DirectoryIterator(__DIR__ . '/locale') as $file)
{
if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml']))
{
$event->locales->addTranslations($file->getBasename('.' . $file->getExtension()), $file->getPathname());
}
}
});

$events->listen(ConfigureFormatter::class, function (ConfigureFormatter $event)
{
$event->configurator->BBCodes->addCustom(
'[REPLY]{TEXT}[/REPLY]',
'<reply2see>{TEXT}</reply2see>'
);
});

$events->listen(ConfigureClientView::class, function (ConfigureClientView $event)
{
if ($event->isForum())
{
$event->addAssets([
__DIR__.'/js/forum/dist/extension.js',
__DIR__.'/less/reply2see.less',
]);
$event->addBootstrapper('wiseclock/flarum-ext-reply2see/main');
}
});
};
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "wiseclock/flarum-ext-reply2see",
"description": "Adds a reply to see BBCODE.",
"type": "flarum-extension",
"license": "MIT",
"authors": [
{
"name": "WiseClock"
}
],
"support": {
"issues": "https://github.com/WiseClock/flarum-ext-reply2see/issues",
"source": "https://github.com/WiseClock/flarum-ext-reply2see"
},
"require": {
"flarum/core": "^0.1.0-beta.6",
"flarum/flarum-ext-bbcode": "^0.1.0"
},
"extra": {
"flarum-extension": {
"title": "Reply2See",
"icon": {
"name": "reply-all",
"backgroundColor": "#ff1a00",
"color": "#ffffff"
}
}
},
"autoload": {
"psr-4": {
"WiseClock\\Reply2See\\": "src/"
}
}
}
9 changes: 9 additions & 0 deletions js/forum/Gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var flarum = require('flarum-gulp');

flarum({
modules: {
'wiseclock/flarum-ext-reply2see': [
'src/**/*.js'
]
}
});
33 changes: 33 additions & 0 deletions js/forum/dist/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
System.register('wiseclock/flarum-ext-reply2see/main', ['flarum/extend', 'flarum/app', 'flarum/components/CommentPost', 'flarum/components/DiscussionPage', 'flarum/utils/DiscussionControls', 'flarum/components/LogInModal'], function (_export) {
'use strict';

var extend, override, app, CommentPost, DiscussionPage, DiscussionControls, LogInModal;
return {
setters: [function (_flarumExtend) {
extend = _flarumExtend.extend;
override = _flarumExtend.override;
}, function (_flarumApp) {
app = _flarumApp['default'];
}, function (_flarumComponentsCommentPost) {
CommentPost = _flarumComponentsCommentPost['default'];
}, function (_flarumComponentsDiscussionPage) {
DiscussionPage = _flarumComponentsDiscussionPage['default'];
}, function (_flarumUtilsDiscussionControls) {
DiscussionControls = _flarumUtilsDiscussionControls['default'];
}, function (_flarumComponentsLogInModal) {
LogInModal = _flarumComponentsLogInModal['default'];
}],
execute: function () {

app.initializers.add('wiseclock-reply2see', function () {
extend(CommentPost.prototype, 'config', function () {
if (app.session.user && app.current instanceof DiscussionPage) $('.reply2see_reply').off('click').on('click', function () {
return DiscussionControls.replyAction.call(app.current.discussion, true, false);
});else $('.reply2see_reply').off('click').on('click', function () {
return app.modal.show(new LogInModal());
});
});
});
}
};
});
7 changes: 7 additions & 0 deletions js/forum/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"devDependencies": {
"gulp": "^3.8.11",
"flarum-gulp": "^0.1.0"
}
}
22 changes: 22 additions & 0 deletions js/forum/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { extend, override } from 'flarum/extend';

import app from 'flarum/app';
import CommentPost from 'flarum/components/CommentPost';
import DiscussionPage from 'flarum/components/DiscussionPage';
import DiscussionControls from 'flarum/utils/DiscussionControls';
import LogInModal from 'flarum/components/LogInModal';

app.initializers.add('wiseclock-reply2see', function()
{
extend(CommentPost.prototype, 'config', function()
{
if (app.session.user && app.current instanceof DiscussionPage)
$('.reply2see_reply').off('click').on('click',
() => DiscussionControls.replyAction.call(app.current.discussion, true, false)
);
else
$('.reply2see_reply').off('click').on('click',
() => app.modal.show(new LogInModal())
);
});
});
22 changes: 22 additions & 0 deletions less/reply2see.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.reply2see
{
.reply2see_title
{
text-align: center;
font-weight: bold;
color: @primary-color;
padding: 5px 0 10px 0;
}

.reply2see_alert
{
color: @muted-color;
padding-top: 10px;
text-align: center;
}

border: 1px dashed @primary-color;
border-radius: 5px;
padding: 0 10px 10px 10px;
margin: 10px 0;
}
9 changes: 9 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) Flagrow

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.
4 changes: 4 additions & 0 deletions locale/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wiseclock-reply2see:
forum:
hidden_content: HIDDEN CONTENT
reply_to_see: "You need to {reply} and refresh to see the content"
4 changes: 4 additions & 0 deletions locale/zh-tw.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wiseclock-reply2see:
forum:
hidden_content: 隱藏的內容
reply_to_see: "此內容{reply}並重新整理後可見"
4 changes: 4 additions & 0 deletions locale/zh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wiseclock-reply2see:
forum:
hidden_content: 隐藏的内容
reply_to_see: "此内容{reply}并刷新后可见"
56 changes: 56 additions & 0 deletions src/Listeners/LoadSettingsFromDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace WiseClock\Reply2See\Listeners;

use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\PrepareApiAttributes;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Translation\TranslatorInterface;

class LoadSettingsFromDatabase
{
protected $translator;

public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

public function subscribe(Dispatcher $events)
{
$events->listen(PrepareApiAttributes::class, [$this, 'prepareApiAttributes']);
}

public function prepareApiAttributes(PrepareApiAttributes $event)
{
if ($event->isSerializer(PostSerializer::class))
{
$newHTML = $event->attributes['contentHtml'];
if (strpos($newHTML, '<reply2see>') === false)
return;

$isStartPost = $event->model['relations']['discussion']->start_post_id == $event->model->id;
if (!$isStartPost)
{
$newHTML = preg_replace('/<reply2see>(.*?)<\/reply2see>/is', '<div>$1</div>', $newHTML);
$event->attributes['contentHtml'] = $newHTML;
return;
}

$usersModel = $event->model['relations']['discussion']->participants()->get('id');
$users = array();
foreach ($usersModel as $user)
{
$users[] = $user->id;
}
$replied = !$event->actor->isGuest() && in_array($event->actor->id, $users);

if ($replied)
$newHTML = preg_replace('/<reply2see>(.*?)<\/reply2see>/is', '<div class="reply2see"><div class="reply2see_title">' . $this->translator->trans('wiseclock-reply2see.forum.hidden_content') . '</div>$1</div>', $newHTML);
else
$newHTML = preg_replace('/<reply2see>(.*?)<\/reply2see>/is', '<div class="reply2see"><div class="reply2see_alert">' . $this->translator->trans('wiseclock-reply2see.forum.reply_to_see', array('{reply}' => '<a class="reply2see_reply">' . $this->translator->trans('core.forum.discussion_controls.reply_button') . '</a>')) . '</div></div>', $newHTML);

$event->attributes['contentHtml'] = $newHTML;
}
}
}

0 comments on commit 6b5715d

Please sign in to comment.