npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-markdown-menu

v1.2.3

Published

A small medium-esk react component to allow editing markdown files

Downloads

3

Readme

Build Status code style: prettier

react-markdown-menu

A small medium-esk react component to allow editing markdown files

Install

npm install react-markdown-menu

or

yarn add react-markdown-menu

Useage

While the component is intended to be used as a compound component until it has been tested the main usage will be through the base MarkdownMenu component

class MarkdownMenu extends Component {
  static propTypes = {
    // The number of pixels from the left of the browser to place the menu
    x: PropTypes.number,
    // The number of pixels from the top of the browser to place the menu
    y: PropTypes.number,
    // A function that is called whenever a button on the menu is clicked.
    // The function is of the type:
    // function onChange(modifiedText: string, line: boolean): undefined
    // When a button is clicked to modified text is sent to the onChange,
    // and a boolean to say whether to replace the entire line or
    // just the selected area
    onChange: PropTypes.func.isRequired,
    // The text selection of the entire line the cursor is currently on
    lineSelection: PropTypes.string,
    // The text selection of whatever is curretly highlighted
    selection: PropTypes.string,
  };

  /* the rest of the class */
}

Below is an example using a textarea as the editor

import React from 'react';
import { render, findDOMNode } from 'react-dom';
import { MarkdownMenu } from 'react-markdown-menu';

class App extends React.Component {
  state = {};

  getLineRange(value, selectionEnd) {
    let length = 0;

    const lines = value.split('\n').map(str => (length += str.length + 1));

    const max = lines.filter(l => l < selectionEnd);

    const start = max[max.length - 1] || 0;
    const end = lines[max.length] - 1;

    return [start, end];
  }

  componentDidMount() {
    // Setup event listeners
    if (this.textareaRef) {
      const textarea = (this.textarea = findDOMNode(this.textareaRef));
      const menu = findDOMNode(this.menu);

      console.log(menu);

      const hideMenu = () =>
        this.setState({ selection: null, lineSelection: null });

      // Hide then menu on scroll of the window.
      document.addEventListener('scroll', hideMenu);

      // Hide the menu on clicking outside of the menu
      document.addEventListener(
        'mousedown',
        ({ target }) => !menu.contains(target) && hideMenu()
      );

      // Update the selected text and position of the menu
      let clickEvent;
      // Keep track of the click position to know where to place the menu
      textarea.addEventListener('mousedown', event => (clickEvent = event));
      // Handle text and line selection
      textarea.addEventListener('select', event => {
        const { value, selectionStart, selectionEnd } = textarea;
        const { clientX, clientY } = clickEvent;
        console.log(clickEvent);

        const [lineStart, lineEnd] = this.getLineRange(value, selectionEnd);

        console.log(this);

        this.setState({
          x: clientY,
          y: clientX,
          lineRange: [lineStart, lineEnd],
          selectionRange: [selectionStart, selectionEnd],
          lineSelection: value.substring(lineStart, lineEnd),
          selection: value.substring(selectionStart, selectionEnd),
        });
      });
    }
  }

  onChange(text, line) {
    const { selectionRange, lineRange } = this.state;

    const [start, end] = line ? lineRange : selectionRange;

    this.textarea.value = `${this.textarea.value.slice(
      0,
      start
    )}${text}${this.textarea.value.slice(end)}`;

    this.textarea.setSelectionRange(start, end + (text.length - end - start));
  }

  render() {
    const { x, y, selection, lineSelection } = this.state;

    return (
      <div>
        <MarkdownMenu
          ref={node => (this.menu = node)}
          x={x}
          y={y}
          onChange={this.onChange.bind(this)}
          selection={selection}
          lineSelection={lineSelection}
        />
        <textarea ref={node => (this.textareaRef = node)} />
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

Edit 4jv2612w4x

The experimental compound component example that hasn't been completely tested and the API not yet refined.

import React from 'react';
import { render } from 'react-dom';
import { Editor } from 'react-markdown-menu';

class App extends React.Component {
  getLineRange(value, selectionEnd) {
    let length = 0;

    const lines = value.split('\n').map(str => (length += str.length + 1));

    const max = lines.filter(l => l < selectionEnd);

    const start = max[max.length - 1] || 0;
    const end = lines[max.length] - 1;

    return [start, end];
  }

  getSelectionRange(element) {
    const { selectionStart, selectionEnd } = element;

    return [selectionStart, selectionEnd];
  }

  updateText(element, text, [start, end]) {
    element.value = `${element.value.slice(
      0,
      start
    )}${text}${element.value.slice(end)}`;
  }

  updateSelection(element, text, [start, end]) {
    element.setSelectionRange(start, end + (text.length - end - start));
  }

  render() {
    return (
      <Editor
        onChange={console.log}
        updateText={this.updateText}
        updateSelection={this.updateSelection}
        getSelectionRange={this.getSelectionRange}
        onChangeSelection={(element, update) =>
          element.addEventListener('select', update)
        }
        getLineRange={this.getLineRange}
        render={() => <textarea />}
      />
    );
  }
}

render(<App />, document.getElementById('root'));

Edit 52nnq4pxml