Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cyclecycle/vue-annotated-text

VueJS component to visually represent annotations over text
https://github.com/cyclecycle/vue-annotated-text

javascript vue-components

Last synced: 3 months ago
JSON representation

VueJS component to visually represent annotations over text

Awesome Lists containing this project

README

        

# vue-annotated-text

![Example output](https://github.com/cyclecycle/vue-annotated-text/blob/master/example_output.png)

## Features

- Renders the text as a set of spans representing the segments composing the sentence
- These spans can be associated with custom styles or events
- Overlapping annotations are represented by mixed colors

## Installation

With npm:

```bash
npm install vue-annotated-text
```

With yarn:

```bash
yarn add vue-annotated-text
```

## Example usage

```html



:annotations="annotations"
:getAnnotationColor="getAnnotationColor"
:getSpanClasses="getSpanClasses"
:spanEvents="spanEvents"
:spanAttributes="spanAttributes"
/>

```
```js

import AnnotatedText from 'vue-annotated-text'

export default {
name: 'MyComponent',
components: {
AnnotatedText
},
data() {
return {
text: "Forging is performed by a smith using hammer and anvil.",
annotations: [
{ // All annotations must have a unique 'id', and values for 'start' and 'length'
id: "forging",
start: 0,
length: 7,
class: "process"
},
{
id: "smith_using_hammer",
start: 26,
length: 18,
class: "process"
},
{
id: "hammer",
start: 38,
length: 6,
class: "tool"
},
{
id: "anvil",
start: 49,
length: 5,
class: "tool"
}
],
spanEvents: {
'click': function(e, annotations) { // All event callbacks take arguments: (event, annotations)
console.log(annotations)
},
},
}
},
methods: {
getAnnotationColor: function(annotation) {
const classToColor = {
'process': '#f44283',
'tool': '#41acf4',
}
const color = classToColor[annotation.class]
return color // Must return hex value
},
getSpanClasses: function(span) {
const classes = ['my-span-class']
const annotationIds = span.annotationIds
if (annotationIds.length > 0) {
classes.push('annotated')
}
return classes
}
}
}

```
```css

.my-span-class:hover {
outline: 1px solid black;
}
.annotated {
font-weight: bold;
}

```

## Acknowledgements

Uses:

- [flatten-overlapping-ranges](https://github.com/derhuerst/flatten-overlapping-ranges)
- [color-mixer](https://github.com/kosamari/color-mixer)

## TODO

- Add default info box on hover