easy-marker
is a library for marking text in html. An example is as follows:
npm i easy-marker
import EasyMarker from 'easy-marker'
const easyMarker = new EasyMarker
const container = document.querySelector('xxxx')
easyMarker.create(container)
- EasyMarker
- new EasyMarker(options)
- instance
- static
- inner
- ~menuClickHandler :
function
- ~highlightLineClickHandler :
function
- ~menuClickHandler :
Creates an instance of EasyMarker.
Param | Type | Description |
---|---|---|
options | Object |
options |
options.menuItems | Array.<Object> |
menu item option |
options.menuItems[].text | string |
menu text |
options.menuItems[].handler | menuClickHandler |
menu item click handler |
options.menuTopOffset | number | string |
the offset from the top of the menu relative screen, default 0. |
options.menuStyle | Object |
the menu style |
options.menuStyle.menu | Object |
the menu style |
options.menuStyle.triangle | Object |
the triangle style |
options.menuStyle.item | Object |
the sub menu style |
options.disableTapHighlight | Object |
disable highlight when tap |
options.cursor | Object |
cursor config |
options.cursor.same | Object |
whether the cursor is in the same direction |
Example
const em = new EasyMarker({
menuTopOffset:'2rem',
menuItems: [
{
text: '划线笔记',
handler: function (data) {
console.log('划线笔记', data, this)
this.highlightLine(data,1)
}
},
{
text: '分享',
handler: (data) => {console.log('分享',data)}
},
{
text: '复制',
handler: (data) => {console.log('分享',data)}
}
]
)
em.create(document.querySelector('.article-body'),
document.body,
document.querySelectorAll('.article-body>:not(.text)')
Initialization
Kind: instance method of EasyMarker
Param | Type | Description |
---|---|---|
containerElement | HTMLElement |
container element |
[scrollContainerElement] | HTMLElement |
scroll container element |
[excludeElements] | Array.<HTMLElement> |
not included elements |
Get the selected text
Kind: instance method of EasyMarker
Highlight the lines between the specified nodes
Kind: instance method of EasyMarker
Param | Type | Description |
---|---|---|
selection | Object |
selection |
selection.anchorNode | Node |
start node |
selection.anchorOffset | number |
start node's text offset |
selection.focusNode | Node |
end node |
selection.focusOffset | number |
start node's text offset |
[id] | * |
line id |
[meta] | * |
meta information |
Example
const id = 2;
const selection = {
anchorNode: textNodeA,
anchorOffset: 1,
focusNode: textNodeB,
focusOffset: 2
};
const meta = { someKey: 'someValue' };
em.highlightLine(selection, id, meta);
Highlight multiple lines
Kind: instance method of EasyMarker
Param | Type |
---|---|
lines | Array.<Object> |
[lines[].id] | * |
[lines[].meta] | * |
lines[].selection | Object |
lines[].selection.anchorNode | Node |
lines[].selection.anchorOffset | number |
lines[].selection.focusNode | Node |
lines[].selection.focusOffset | number |
Example
const id = 2;
const selection = {
anchorNode: textNodeA,
anchorOffset: 1,
focusNode: textNodeB,
focusOffset: 2
};
const meta = { someKey: 'someValue' };
em.highlightLines([{selection, id, meta}]);
Cancel highlight
Kind: instance method of EasyMarker
Param | Type | Description |
---|---|---|
id | * |
line ID |
Highlight line click handler
Kind: instance method of EasyMarker
Param | Type |
---|---|
cb | highlightLineClickHandler |
Select status changing callback
Kind: instance method of EasyMarker
Param | Type |
---|---|
cb | function |
Register event hook
Kind: instance method of EasyMarker
Param | Type |
---|---|
cb | * |
Destroy instance
Kind: instance method of EasyMarker
EasyMarker.create(containerElement, [scrollContainerElement], [excludeElements]) ⇒ EasyMarker
Initialization factory
Kind: static method of EasyMarker
Param | Type | Description |
---|---|---|
containerElement | HTMLElement |
container element |
[scrollContainerElement] | HTMLElement |
scroll container element |
[excludeElements] | Array.<HTMLElement> |
not included elements |
Menu item click handler
Kind: inner typedef of EasyMarker
Param | Type | Description |
---|---|---|
selection | Object |
selection |
selection.anchorNode | Node |
start node |
selection.anchorOffset | number |
start node's text offset |
selection.focusNode | Node |
end node |
selection.focusOffset | number |
start node's text offset |
Menu item click handler
Kind: inner typedef of EasyMarker
Param | Type | Description |
---|---|---|
id | * |
line ID |
meta | * |
meta information |
selection | Object |
selection |
selection.anchorNode | Node |
start node |
selection.anchorOffset | number |
start node's text offset |
selection.focusNode | Node |
end node |
selection.focusOffset | number |
start node's text offset |