Skip to content

Commit

Permalink
fix: rebase errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davwheat committed Feb 28, 2023
1 parent 5d9d31d commit 083ff3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 152 deletions.
173 changes: 23 additions & 150 deletions extensions/tags/js/src/forum/components/TagDiscussionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,166 +21,39 @@ export default class TagDiscussionModal extends TagSelectionModal<TagDiscussionM
? app.translator.trans('flarum-tags.forum.choose_tags.edit_title', { title: <em>{attrs.discussion.title()}</em> })
: app.translator.trans('flarum-tags.forum.choose_tags.title');

getInstruction(primaryCount: number, secondaryCount: number) {
if (this.bypassReqs) {
return null;
}
attrs.className = classList(attrs.className, 'TagDiscussionModal');
attrs.title = extractText(title);
attrs.allowResetting = !!app.forum.attribute('canBypassTagCounts');
attrs.limits = {
allowBypassing: attrs.allowResetting,
max: {
primary: app.forum.attribute<number>('minPrimaryTags'),
secondary: app.forum.attribute<number>('maxSecondaryTags'),
},
min: {
primary: app.forum.attribute<number>('maxPrimaryTags'),
secondary: app.forum.attribute<number>('minSecondaryTags'),
},
};
attrs.requireParentTag = true;
attrs.selectableTags = () => getSelectableTags(attrs.discussion);
attrs.selectedTags ??= (attrs.discussion?.tags() as Tag[]) || [];
attrs.canSelect = (tag) => tag.canStartDiscussion();

const suppliedOnsubmit = attrs.onsubmit || null;

return null;
}
// Save changes.
attrs.onsubmit = function (tags) {
const discussion = attrs.discussion;

if (discussion) {
discussion.save({ relationships: { tags } }).then(() => {
if (app.current.matches(DiscussionPage)) {
app.current.get('stream').update();
}

let tags = this.tags;
const filter = this.filter().toLowerCase();
const primaryCount = this.primaryCount();
const secondaryCount = this.secondaryCount();

// Filter out all child tags whose parents have not been selected. This
// makes it impossible to select a child if its parent hasn't been selected.
tags = tags.filter((tag) => {
const parent = tag.parent();
return parent !== null && (parent === false || this.selected.includes(parent));
});

// If the number of selected primary/secondary tags is at the maximum, then
// we'll filter out all other tags of that type.
if (primaryCount >= this.maxPrimary && !this.bypassReqs) {
tags = tags.filter((tag) => !tag.isPrimary() || this.selected.includes(tag));
}

if (secondaryCount >= this.maxSecondary && !this.bypassReqs) {
tags = tags.filter((tag) => tag.isPrimary() || this.selected.includes(tag));
}

// If the user has entered text in the filter input, then filter by tags
// whose name matches what they've entered.
if (filter) {
tags = tags.filter((tag) => tag.name().substr(0, filter.length).toLowerCase() === filter);
}

if (!this.selectedTag || !tags.includes(this.selectedTag)) this.selectedTag = tags[0];

const inputWidth = Math.max(extractText(this.getInstruction(primaryCount, secondaryCount)).length, this.filter().length);

return (
<>
<div className="Modal-body">
<div className="TagDiscussionModal-form">
<div className="TagDiscussionModal-form-input">
<div className={classList('TagsInput FormControl', { focus: this.focused })} onclick={() => this.$('.TagsInput input').focus()}>
<span className="TagsInput-selected">
{this.selected.map((tag) => (
<span
className="TagsInput-tag"
onclick={() => {
this.removeTag(tag);
this.onready();
}}
>
{tagLabel(tag)}
</span>
))}
</span>
<input
className="FormControl"
placeholder={extractText(this.getInstruction(primaryCount, secondaryCount))}
bidi={this.filter}
style={{ width: inputWidth + 'ch' }}
onkeydown={this.navigator.navigate.bind(this.navigator)}
onfocus={() => (this.focused = true)}
onblur={() => (this.focused = false)}
/>
</div>
</div>
<div className="TagDiscussionModal-form-submit App-primaryControl">
<Button
type="submit"
className="Button Button--primary"
disabled={!this.meetsRequirements(primaryCount, secondaryCount)}
icon="fas fa-check"
>
{app.translator.trans('flarum-tags.forum.choose_tags.submit_button')}
</Button>
</div>
</div>
</div>

<div className="Modal-footer">
<ul className="TagDiscussionModal-list SelectTagList">
{tags
.filter((tag) => filter || !tag.parent() || this.selected.includes(tag.parent() as Tag))
.map((tag) => (
<li
data-index={tag.id()}
className={classList({
pinned: tag.position() !== null,
child: !!tag.parent(),
colored: !!tag.color(),
selected: this.selected.includes(tag),
active: this.selectedTag === tag,
})}
style={{ color: tag.color() }}
onmouseover={() => (this.selectedTag = tag)}
onclick={this.toggleTag.bind(this, tag)}
>
{tagIcon(tag)}
<span className="SelectTagListItem-name">{highlight(tag.name(), filter)}</span>
{!!tag.description() && <span className="SelectTagListItem-description">{tag.description()}</span>}
</li>
))}
</ul>
{!!app.forum.attribute('canBypassTagCounts') && (
<div className="TagDiscussionModal-controls">
<ToggleButton className="Button" onclick={() => (this.bypassReqs = !this.bypassReqs)} isToggled={this.bypassReqs}>
{app.translator.trans('flarum-tags.forum.choose_tags.bypass_requirements')}
</ToggleButton>
</div>
)}
</div>
</>
);
}

meetsRequirements(primaryCount: number, secondaryCount: number) {
if (this.bypassReqs) {
return true;
}

return primaryCount >= this.minPrimary && secondaryCount >= this.minSecondary;
}

toggleTag(tag: Tag) {
// Won't happen, needed for type safety.
if (!this.tags) return;

if (this.selected.includes(tag)) {
this.removeTag(tag);
} else {
this.addTag(tag);
}

if (this.filter()) {
this.filter('');
this.selectedTag = this.tags[0];
}

this.onready();
}

select(e: KeyboardEvent) {
// Ctrl + Enter submits the selection, just Enter completes the current entry
if (e.metaKey || e.ctrlKey || (this.selectedTag && this.selected.includes(this.selectedTag))) {
if (this.selected.length) {
// The DOM submit method doesn't emit a `submit event, so we
// simulate a manual submission so our `onsubmit` logic is run.
this.$('button[type="submit"]').click();
m.redraw();
});
}

if (suppliedOnsubmit) suppliedOnsubmit(tags);
Expand Down
1 change: 0 additions & 1 deletion extensions/tags/js/src/forum/components/TagHero.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import classList from '@flarum/core/src/common/utils/classList';
import Component from 'flarum/common/Component';
import textContrastClass from 'flarum/common/helpers/textContrastClass';
import tagIcon from '../../common/helpers/tagIcon';
Expand Down
1 change: 0 additions & 1 deletion extensions/tags/js/src/forum/components/TagsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import classList from 'flarum/common/utils/classList';
import tagIcon from '../../common/helpers/tagIcon';
import tagLabel from '../../common/helpers/tagLabel';
import sortTags from '../../common/utils/sortTags';
import classList from 'flarum/common/utils/classList';

export default class TagsPage extends Page {
oninit(vnode) {
Expand Down

0 comments on commit 083ff3f

Please # to comment.