Giter Club home page Giter Club logo

less-offset-comments's Introduction

What is this for?

This is utility for less offset in nested comments.

Main idea

Traditional tree-like comments may be as follows:

user1
  user2
    user1
      user2
        user4
        user1
          user2
            user1
              user2
                user1
                  user2
                    user1
                      user2
                        user3
                          user2
                            user3
                              user2

And what if we do the following:

user1
  user2
  user1
  user2
    user4
    user1
      user2
      user1
      user2
      user1
      user2
      user1
      user2
        user3
        user2
        user3
        user2

What is the main idea here? Dialogue between two users may not be offset.

For all comments, this utility changes the property hasOffset to true or to false. A developer should rely on this property when rendering the comments.

Install

npm install less-offset-comments --save

API

Comments interface

To use this utility, your comments should be implemented this TypeScript interface:

interface Comment
{
  userId: number;
  children: this[];
  hasOffset?: boolean;
  parent?: this;
  [key: string]: any;
}

Methods

All methods of this utility are called statically.

static hasOffset(comment: Comment): boolean;

static updateOffset(comment: Comment): void;

static unshiftComment(parent: Comment, newChild: Comment): void;

static pushComment(parent: Comment, newChild: Comment): void;

static deleteComment(comments: Comment[], comment: Comment, index: number): void;

Usage

When rendering comments, and when you walk through the comments tree, you should call LessOffsetComments.updateOffset() for each of them.

And then, for insert and delete a comment, you should using LessOffsetComments.unshiftComment() and LessOffsetComments.deleteComment() like this:

import { LessOffsetComments, Comment } from 'less-offset-comments';

class MyComponent
{
  comments: Comment[];

  onSubmit(parent: Comment)
  {
    const child: Comment =
    {
      userId: 111111,
      parent: parent,
      children: []
    };

    LessOffsetComments.unshiftComment(parent, child);
  }

  onDelete(comment: Comment, index: number)
  {
    LessOffsetComments.deleteComment(this.comments, comment, index);
  }
}

And in your view like this pseudo-code:

<ul>
  <!-- You want to covered the following code in foreach -->

  <li *if="comment.hasOffset">
    <ul>
      <li>
        <!-- Your template for comment here -->
        userId: {{ comment.userId }}
        <button (click)="onSubmit(comment)">Sumbit</button>
        <button (click)="onDelete(comment, index)">Delete</button>
      </li>
    </ul>
  </li>
  <li *if="!comment.hasOffset">
    <!-- Your template for comment here -->
    userId: {{ comment.userId }}
    <button (click)="onSubmit(comment)">Sumbit</button>
    <button (click)="onDelete(comment, index)">Delete</button>
  </li>

  <!-- / end foreach -->
</ul>

Example of usage with Angular 5

Check out this repository specifically created for the demo

less-offset-comments's People

Contributors

kostyatretyak avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.