Repository navigation
Expand file tree
/
Copy pathbasic-usage.js
More file actions
150 lines (123 loc) · 3.72 KB
/
Copy pathbasic-usage.js
File metadata and controls
150 lines (123 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Basic usage example of the annotator library
// Import the necessary components
// For local development, import directly from the local build
import {
highlightRange,
removeHighlights,
TextQuoteAnchor,
TextPositionAnchor,
RangeAnchor,
AnnotationBuilder,
TextProcessor,
} from '../dist/index.esm.js';
// Example 1: Basic highlighting
function basicHighlighting() {
// Get some content to highlight
const contentElement = document.getElementById('content');
// Create a range
const range = document.createRange();
range.selectNodeContents(contentElement);
// Apply highlighting
const highlights = highlightRange(range, 'span', 'custom-highlight');
// Later, remove the highlights
setTimeout(() => {
removeHighlights(highlights);
}, 3000);
}
// Example 2: Creating an annotation
function createAnnotation() {
if (!isBrowser) return;
const contentElement = document.getElementById('content');
const textProcessor = new TextProcessor();
const annotationBuilder = new AnnotationBuilder(textProcessor);
// Create a range
const selection = window.getSelection();
const range = selection.getRangeAt(0);
// Create an annotation
const annotation = annotationBuilder.createAnnotation(
contentElement,
range,
'collation-123',
'transcription-456'
);
console.log('Created annotation:', annotation);
// You can use the annotation's selectors to recreate the annotation later
return annotation;
}
// Example 3: Using different anchor types
function useAnchors() {
if (!isBrowser) return;
const contentElement = document.getElementById('content');
// Text quote-based anchor
const quoteAnchor = new TextQuoteAnchor(
contentElement,
'exact text to match',
{
prefix: 'text before the ',
suffix: ' in the document',
}
);
// Get the range for this anchor
const quoteRange = quoteAnchor.toRange();
// Highlight it
const quoteHighlights = highlightRange(quoteRange, 'span', 'quote-highlight');
// Text position-based anchor
const positionAnchor = new TextPositionAnchor(
contentElement,
42, // start position
56 // end position
);
// Get the range for this anchor
const positionRange = positionAnchor.toRange();
// Highlight it
const positionHighlights = highlightRange(
positionRange,
'span',
'position-highlight'
);
// Range-based anchor
const rangeAnchor = new RangeAnchor(
contentElement,
'xpath/to/start/container',
5, // start offset
'xpath/to/end/container',
10 // end offset
);
// Get the range for this anchor
const rangeAnchorRange = rangeAnchor.toRange();
// Highlight it
const rangeHighlights = highlightRange(
rangeAnchorRange,
'span',
'range-highlight'
);
// Clean up all highlights
setTimeout(() => {
removeHighlights([
...quoteHighlights,
...positionHighlights,
...rangeHighlights,
]);
}, 5000);
}
// Only run in browser environment
if (isBrowser) {
// Run the examples when the page loads
window.addEventListener('DOMContentLoaded', () => {
// Add a button to trigger each example
const exampleButtons = [
{ name: 'Basic Highlighting', fn: basicHighlighting },
{ name: 'Create Annotation', fn: createAnnotation },
{ name: 'Use Different Anchors', fn: useAnchors },
];
const buttonContainer = document.createElement('div');
buttonContainer.className = 'button-container';
exampleButtons.forEach((example) => {
const button = document.createElement('button');
button.textContent = example.name;
button.addEventListener('click', example.fn);
buttonContainer.appendChild(button);
});
document.body.insertBefore(buttonContainer, document.body.firstChild);
});
}