Giter Club home page Giter Club logo

marklib's Introduction

marklib

Circle CI Codacy Badge Coverage Status npm Join the chat at https://gitter.im/BowlingX/marklib

A simple and fast zero-dependencies-library to transform text-selections into serializable markings.

Demo

Demo-Gif

Install

marklib can be installed with npm or bower.

npm install --save-dev marklib

bower install marklib --save

Usage

Render by selection

// obtain a selection from document
var selection = document.getSelection();

// create a new rendering based on the current document
var renderer = new Marklib.Rendering(document, options, context)
renderer.setId('myRenderId') // if an ID is not provided, a autogenerated one will be used

// renders the given selection and returns a result (`RenderResult`).
var result = renderer.renderWithRange(selection.getRangeAt(0));

Important: After a Rendering has been used to render a selection/serialized result, it can't be used to render something again. You need to create a new Instance of Rendering.

Options

You can pass options to each rendering instance, the following shows the default options

var renderer = new Marklib.Rendering(document, {
            hoverClass: 'marklib--hover',
            treeClass: 'marklib--tree',
            // Supports arrays and/or strings
            className: ['marking']
});

Events

Marklib triggers events that can be listened to with instance.on('event-name'). Events are build with wolfy87-eventemitter (https://github.com/Olical/EventEmitter). The following Events are available:

Before you can actually receive events, you need to register the event handler with registerEvents (use import { registerEvents } from 'marklib/src/main/RenderingEvents'; on your application bootstrap code.)

Event-Name Description Arguments
click triggered when clicked on a marking. (originalEvent, instanceHierarchy)
hover-enter triggered when a pointer-device starts hovering over a marking (originalEvent, instanceHierarchy)
hover-leave triggered when a pointer-device leaves a marking (originalEvent, instanceHierarchy)

Additionally, marklib will add hover classes to the current hovered marking.

Constructor Arguments

    1. HTMLDocument document -> the document instance used
    1. Object [options], optional -> an object containing setting for marklib (see Options)
    1. HTMLElement [context], optional -> the context used to serialize / deserialize the rendering, if not given the document instance.

Render by serialized result

A Serialized results consist of 2 strings (start end end) in the following form


'body>section;0;1'`
-▲------------▲-▲ 

  • ▲ The first part defines a css-selector (queryable with document.querySelector).
  • ▲ The second part defines the text-node inside the given selector
  • ▲ The third part defines the string-offset inside this text-node

Example

 // This is the result we get from `RenderResult#serialize()`
 
 var result = {
    startContainerPath: 'body>section;0',
    endContainerPath: 'body>section;1',
    startOffset: 2,
    endOffset: 5
 }

 var rendering = new Marklib.Rendering(document);
 
 rendering.renderWithResult(result);

Use-Cases

Develop

npm run develop or npm run tdd (to start karma in watch mode)

License

The MIT License (MIT)

Copyright (c) 2015 David Heidrich

Any contribution is welcome, just issue a pull-request or bug/feature if you found something :)

marklib's People

Contributors

bowlingx avatar gitter-badger avatar tanertopal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

marklib's Issues

An easy way to remove x-markers

As far as I can see there is no method to remove once rendered markers. Just asking to be sure. I would try to implement one and provide create PR.

no way to unregister events

Currently marklib allows you to register the events of the lib, but no way to unregister them. Can you please implement this? Thanks!

Demo page bug

marklib mov

If you make a selection on the demo page, hit enter, then click on the highlight you get an uncaught exception self.wrapperNodes is undefined.

Use cases?

Could you put a section with a few use cases into the README? I think, it is important that tools provide examples of what they’re useful for.

Trouble with hover events and clicks.

Thanks for a great tool!

I'm having problems getting hover functionality and click events to work on my rendered ranges. I have something like the following:

import { Rendering } from 'marklib';

const range = <my range>;
const renderer = new Rendering(document, { hoverClass: 'highlight-hover', className: 'highlight' });
const onClick = function(instance) {
    console.log('Hello there');
};
renderer.on('click', onClick);
renderer.renderWithRange(range);

This properly wraps the content in x-markers and applies the highlight class. The highlight class specifies a background-color and cursor: pointer. The pointer cursor properly renders when I hover the xmarker, so I know that the css class is being applied, and that I don't have some weird z-index occlusion or pointer-events: none bug.

When hovering, no hover-class is applied. When clicking, my console.log statement never fires.

I've also tried:

Rendering.globalEmitter().on('click', function() {
    console.log('Howdy');
});

but that hasn't worked either.

I see this line in the README - but I'm not quite sure what it means: Before you can actually receive events, you need to register the event handler with registerEvents. Looking at your Site.js also doesn't provide any clues. It seems rather straightforward, yet I can't find my bug.

Any hunches?

Thanks again!

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.