Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve split functions #44

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
8 changes: 6 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
->js(__DIR__.'/js/dist/admin.js'),

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

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

(new Extend\Routes('api'))
->post('/split', 'fof.split.run', Api\Controllers\SplitController::class),

(new Extend\Routes('api'))
->post('/split_to', 'fof.split_to.run', Api\Controllers\SplitToController::class),

(new Extend\Event())
->listen(Renamed::class, Listeners\UpdateSplitTitleAfterDiscussionWasRenamed::class)
->listen(DiscussionWasSplit::class, Listeners\CreatePostWhenSplit::class),
Expand Down
6 changes: 6 additions & 0 deletions js/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 150,
"tabWidth": 4
}
33 changes: 17 additions & 16 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "@fof/split",
"version": "0.0.0",
"dependencies": {
"flarum-webpack-config": "0.1.0-beta.10",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
},
"scripts": {
"build": "webpack --mode production",
"dev": "webpack --mode development --watch",
"lint": "prettier --single-quote --trailing-comma es5 --print-width 150 --tab-width 4 --write src"
},
"devDependencies": {
"flarum": "0.1.0-beta.16",
"prettier": "^2.3.0"
}
"name": "@fof/split",
"version": "1.0.0",
"dependencies": {
"flarum-webpack-config": "1.0.0",
rafaucau marked this conversation as resolved.
Show resolved Hide resolved
"webpack": "^4.46.0",
"webpack-cli": "^4.7.2"
},
"scripts": {
"build": "webpack --mode production",
"dev": "webpack --mode development --watch",
"lint": "prettier --write src"
},
"devDependencies": {
"flarum": "0.1.0-beta.16",
rafaucau marked this conversation as resolved.
Show resolved Hide resolved
"flarum-tsconfig": "^1.0.0",
"prettier": "^2.3.0"
}
}
4 changes: 2 additions & 2 deletions js/src/forum/addSplitControl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { extend } from 'flarum/common/extend';
import app from 'flarum/common/app';
import app from 'flarum/forum/app';
import PostControls from 'flarum/common/utils/PostControls';
import Button from 'flarum/common/components/Button';
import CommentPost from 'flarum/common/components/CommentPost';
import CommentPost from 'flarum/forum/components/CommentPost';
import SplitPostModal from './components/SplitPostModal';

export default function (controller) {
Expand Down
21 changes: 21 additions & 0 deletions js/src/forum/components/DiscussionSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Search from 'flarum/forum/components/Search';
import ItemList from 'flarum/common/utils/ItemList';
import DiscussionSearchSource from './DiscussionSearchSource';

export default class DiscussionSearch extends Search {
view() {
this.hasFocus = true;

const vdom = super.view();
vdom.attrs.className = `SplitPosts-Search ${this.state.getValue() && 'open'} ` + vdom.attrs.className.replace(/(focused|open)/g, '');

return vdom;
}

sourceItems() {
const items = new ItemList();
items.add('discussions', new DiscussionSearchSource(this.attrs.onSelect, this.attrs.ignore));

return items;
}
}
57 changes: 57 additions & 0 deletions js/src/forum/components/DiscussionSearchSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import highlight from 'flarum/common/helpers/highlight';

export default class DiscussionSearchSource {
constructor(onSelect, ignore) {
this.results = new Map();

this.onSelect = onSelect;
this.ignore = ignore;
}

search(query) {
query = query.toLowerCase();

this.results.set(query, []);

const params = {
filter: { q: query },
page: { limit: 4 },
};

const id = Number(query);

if (!Number.isNaN(id) && id !== this.ignore) {
return app.store
.find('discussions', id)
.then((d) => {
this.results.set(query, [d]);
})
.catch(() => []);
}

return app.store.find('discussions', params).then((results) => {
this.results.set(
query,
results.filter((d) => d.id() !== this.ignore)
);
});
}

view(query) {
query = query.toLowerCase();

const results = this.results.get(query) || [];

return [
results.map((discussion) => (
<li className="DiscussionSearchResult" data-index={'discussions' + discussion.id()}>
<a onclick={() => this.onSelect(discussion)}>
<div className="DiscussionSearchResult-title">
<i>{highlight(discussion.id(), query)}</i> ~ {highlight(discussion.title(), query)}
</div>
</a>
</li>
)),
];
}
}
103 changes: 76 additions & 27 deletions js/src/forum/components/SplitPostModal.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import app from 'flarum/forum/app';
import Modal from 'flarum/common/components/Modal';
import Button from 'flarum/common/components/Button';
import Stream from 'flarum/common/utils/Stream';
import GlobalSearchState from 'flarum/forum/states/GlobalSearchState';
import DiscussionSearch from './DiscussionSearch';

export default class SplitPostModal extends Modal {
oninit(vnode) {
super.oninit(vnode);

this.discussion = this.attrs.post.discussion();

this.splitTypes = ['new', 'existing'];
this.selectedType = Stream(this.splitTypes[0]);

this.newDiscussionTitle = Stream('');
this.targetDiscussion = Stream(null);

this.search = new GlobalSearchState();
}

className() {
return 'SplitPostModal Modal--small';
return 'SplitPostModal Modal--large';
}

title() {
Expand All @@ -19,47 +30,85 @@ export default class SplitPostModal extends Modal {

content() {
return [
m('div', { className: 'Modal-body' }, [
m('div', { className: 'Form Form--centered' }, [
m('div', { className: 'Form-group' }, [
m('label', {}, app.translator.trans('fof-split.forum.modal.new_discussion_label')),
m('input', {
className: 'FormControl',
name: 'new_discussion_title',
bidi: this.newDiscussionTitle,
}),
]),
m('div', { className: 'Form-group' }, [
m(
Button,
{
className: 'Button Button--primary Button--block',
type: 'submit',
loading: this.loading,
disabled: !this.newDiscussionTitle(),
},
app.translator.trans('fof-split.forum.modal.submit_button')
),
]),
]),
]),
<div className="Modal-body">
<div className="Form">
<div className="Form-group">
{this.splitTypes.map((key) => (
<div>
<input type="radio" id={`type_${key}`} checked={this.selectedType() === key} onclick={() => this.selectedType(key)} />
&nbsp;
<label htmlFor={`type_${key}`}>{app.translator.trans(`fof-split.forum.modal.type_${key}_label`)}</label>
</div>
))}
</div>

<div className="Form-group">
{this.selectedType() === this.splitTypes[0] ? (
<label>
{app.translator.trans('fof-split.forum.modal.new_discussion_label')}
<input type="text" className="FormControl" bidi={this.newDiscussionTitle} />
</label>
) : (
<label>
{app.translator.trans('fof-split.forum.modal.search_discussion_label')}
<DiscussionSearch state={this.search} onSelect={this.select} ignore={this.discussion.id()} />
</label>
)}
</div>

{this.selectedType() === this.splitTypes[1] && this.targetDiscussion() && (
<p>
{app.translator.trans('fof-split.forum.modal.split_to_discussion_help', {
title: this.targetDiscussion().title(),
})}
</p>
)}

<div className="Form-group">
<Button className="Button Button--primary Button--block" loading={this.loading} type="submit" disabled={this.disabled()}>
{app.translator.trans('fof-split.forum.modal.submit_button')}
</Button>
</div>
</div>
</div>,
];
}

disabled = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both disabled and select should be methods, not class properties.

if (this.selectedType() === this.splitTypes[0] && this.newDiscussionTitle()) {
return false;
}

return !(this.selectedType() === this.splitTypes[1] && this.targetDiscussion() && this.targetDiscussion().id());
};

select = (discussion) => {
if (discussion && discussion.id() === this.discussion.id()) return;

this.targetDiscussion(discussion);
};

onsubmit(e) {
e.preventDefault();

this.loading = true;

const data = new FormData();

data.append('title', this.newDiscussionTitle());
data.append('start_post_id', this.attrs.split.startPostId);
data.append('end_post_number', this.attrs.post.number());

let url = app.forum.attribute('apiUrl') + '/split';
if (this.selectedType() === this.splitTypes[0]) {
data.append('title', this.newDiscussionTitle());
} else {
data.append('discussion_id', this.targetDiscussion().id());
url += '_to';
}

app.request({
method: 'POST',
url: app.forum.attribute('apiUrl') + '/split',
url,
serialize: (raw) => raw,
body: data,
}).then((data) => {
Expand Down
8 changes: 0 additions & 8 deletions js/src/forum/extendDiscussionPage.js

This file was deleted.

1 change: 1 addition & 0 deletions js/src/forum/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import app from 'flarum/forum/app';
import Model from 'flarum/common/Model';

import addSplitControl from './addSplitControl';
Expand Down
14 changes: 14 additions & 0 deletions js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use Flarum's tsconfig as a starting point
"extends": "flarum-tsconfig",
// This will match all .ts, .tsx, .d.ts, .js, .jsx files
"include": ["src/**/*"],
"compilerOptions": {
// This will output typings to `dist-typings`
"declarationDir": "./dist-typings",
"baseUrl": ".",
"paths": {
"flarum/*": ["../vendor/flarum/core/js/dist-typings/*"]
}
}
}
10 changes: 10 additions & 0 deletions resources/less/forum.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.SplitPosts-Search {
.Search-input input {
width: 100%;
}

.Search-results {
left: 0;
position: relative;
}
}
4 changes: 4 additions & 0 deletions locale/en.yml → resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ fof-split:
to: Split to here
modal:
title: Split discussion
type_new_label: Split to a new discussion
type_existing_label: Split into existing discussion
search_discussion_label: Select a discussion to which to split posts
new_discussion_label: Specify the title for the new discussion
split_to_discussion_help: 'Posts will be split into <b>{title}</b>'
rafaucau marked this conversation as resolved.
Show resolved Hide resolved
submit_button: Split
Loading