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

Reverse order of comments #222

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions src/configuration-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ export class ConfigurationComponent {
<option value="photon-dark">Photon Dark</option>
</select>

<h3 id="heading-reverse-order">Reverse Order</h3>
<p>
Choose if you want the order of comments to be reversed and put
the input form on top.
</p>
<p>
Related parameters ("reverse-order" and "input-position-top") accept
both boolean primitives and strings (both true and "true" are valid).
</p>
<label>
<input type="checkbox" id="reverse-order" />
Reverse order, input form on top
</label>

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use "reverse-order" and "input-position" as "HTMLSelectElement"?

Isn't that better?

E.g

      <h3 id="heading-order">Select order of comments</h3>
      <p>
        The order of presentation of comments is sorted by the time when the comments were published.
        <br />
        Ascending order: From the old to the new, the newly posted comments are at the back; Descending order: from the new to the old, and the newly posted comments are at the front.
      </p>
      <fieldset>
        <div>
          <label for="comment-order">
            By time:
          </label>
          <br />
          <select id="comment-order" class="form-select" value="asc" aria-label="Comment order">
            <option value="asc">Asc(default)</option>
            <option value="desc">Desc</option>
          </select>
        </div>
      </fieldset>
      <h3 id="heading-input-position">Select comment box location</h3>
      <p>
        When choosing to set the comment order to "descending", it is recommended to place the comment box at the "top". Because when there are a lot of comments, you can see the success of the comment as soon as you post a comment.
      </p>
      <fieldset>
        <div>
          <label for="input-position">
            The comment box's position at:
          </label>
          <br />
          <select id="input-position" class="form-select" value="asc" aria-label="Comment order">
            <option value="bottom">Bottom (default)</option>
            <option value="top">Top</option>
          </select>
        </div>
      </fieldset>

<h3 id="heading-enable">Enable Utterances</h3>

<p>Add the following script tag to your blog's template. Position it where you want the
Expand All @@ -157,6 +171,8 @@ export class ConfigurationComponent {

this.theme = this.element.querySelector('#theme') as HTMLSelectElement;

this.reverseOrder = this.element.querySelector('#reverse-order') as HTMLInputElement;

const themeStylesheet = document.getElementById('theme-stylesheet') as HTMLLinkElement;
this.theme.addEventListener('change', () => {
themeStylesheet.href = `/stylesheets/themes/${this.theme.value}/index.css`;
Expand Down Expand Up @@ -189,11 +205,18 @@ export class ConfigurationComponent {
} else {
mappingAttr = this.makeConfigScriptAttribute('issue-term', mapping.value);
}
let orderConfig : string = '';
if (this.reverseOrder.checked) {
// using strings to encode booleans - because `deparam` uses only strings
orderConfig = this.makeConfigScriptAttribute('reverse-order', 'true') + '\n' +
this.makeConfigScriptAttribute('input-position-top', 'true') + '\n';
}
this.script.innerHTML = this.makeConfigScript(
this.makeConfigScriptAttribute('repo', this.repo.value === '' ? '[ENTER REPO HERE]' : this.repo.value) + '\n' +
mappingAttr + '\n' +
(this.label.value ? this.makeConfigScriptAttribute('label', this.label.value) + '\n' : '') +
this.makeConfigScriptAttribute('theme', this.theme.value) + '\n' +
orderConfig +
Copy link
Contributor

Choose a reason for hiding this comment

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

        (this.commentOrder.value === 'desc'
          ? this.makeConfigScriptAttribute(
              'comment-order',
              this.commentOrder.value,
            ) + '\n'
          : '') +
        (this.inputPosition.value === 'top'
          ? this.makeConfigScriptAttribute('input-position', 'top') + '\n'
          : '') +

Occam's razor: if not necessary, not by entity.

this.makeConfigScriptAttribute('crossorigin', 'anonymous'));
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
issue-term="homepage"
crossorigin="anonymous"
theme="github-light"
input-position-top=false
reverse-order=false
async>
</script>
</if>
Expand All @@ -82,6 +84,8 @@
label="💬 comment"
crossorigin="anonymous"
theme="github-light"
input-position-top=false
reverse-order=false
async>
</script>
</else>
Expand Down
7 changes: 6 additions & 1 deletion src/page-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function readPageAttributes() {
token.value = params.token;
}

let inputPositionTop : bool = params['input-position-top'] == "true";
let reverseOrder : bool = params['reverse-order'] == "true";

return {
owner: matches[1],
repo: matches[2],
Expand All @@ -56,7 +59,9 @@ function readPageAttributes() {
title: params.title,
description: params.description,
label: params.label,
theme: params.theme || 'github-light'
theme: params.theme || 'github-light',
inputPositionTop: inputPositionTop,
reverseOrder: reverseOrder
Copy link
Contributor

Choose a reason for hiding this comment

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

    commentOrder: params['comment-order'] || 'asc',
    inputPosition: params['input-position'] || 'bottom',

Occam's razor: if not necessary, not by entity.

};
}

Expand Down
10 changes: 9 additions & 1 deletion src/timeline-component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { User, Issue, IssueComment } from './github';
import { CommentComponent } from './comment-component';
import { scheduleMeasure } from './measure';
import { pageAttributes as page } from './page-attributes';


export class TimelineComponent {
public readonly element: HTMLElement;
Expand Down Expand Up @@ -55,7 +57,13 @@ export class TimelineComponent {
comment,
this.user ? this.user.login : null);

const index = this.timeline.findIndex(x => x.comment.id >= comment.id);
if (page.reverseOrder) {
const indexSearchCondition = (x => x.comment.id <= comment.id);
} else {
const indexSearchCondition = (x => x.comment.id >= comment.id);
}

const index = this.timeline.findIndex(indexSearchCondition);
Copy link
Contributor

Choose a reason for hiding this comment

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

const index = this.timeline.findIndex((x) => {
      if (page.reverseOrder) {
        return x.comment.id <= comment.id;
      } else {
        return x.comment.id >= comment.id;
      }
    });

is better than

if (...) {
  const indexSearchCondition = ...
} else {
  const indexSearchCondition = ...
}

use indexSearchCondition...

if (index === -1) {
this.timeline.push(component);
this.element.insertBefore(component.element, this.marker);
Expand Down
6 changes: 5 additions & 1 deletion src/utterances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ async function bootstrap() {
};

const newCommentComponent = new NewCommentComponent(user, submit);
timeline.element.appendChild(newCommentComponent.element);
if (page.inputPositionTop) {
timeline.element.insertAdjacentElement('afterbegin', newCommentComponent.element);
} else {
timeline.element.appendChild(newCommentComponent.element);
}
}

bootstrap();
Expand Down