Skip to content

Commit 8ffad71

Browse files
Allow newlines in text annotations (#356)
* allow newlines in text annotations * linting
1 parent c03ce73 commit 8ffad71

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/UI/text.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ function handleDocumentMouseup(e) {
2424
if (!e.srcElement.classList.contains('annotationLayer')) {
2525
return;
2626
}
27-
input = document.createElement('input');
27+
input = document.createElement('textarea');
2828
input.setAttribute('id', 'pdf-annotate-text-input');
29-
input.setAttribute('placeholder', 'Enter text');
29+
input.setAttribute('placeholder', 'Enter text... SHIFT + ENTER for new line');
3030
input.style.border = `3px solid ${BORDER_COLOR}`;
3131
input.style.borderRadius = '3px';
3232
input.style.position = 'absolute';
@@ -57,7 +57,7 @@ function handleInputKeyup(e) {
5757
if (e.keyCode === 27) {
5858
closeInput();
5959
}
60-
else if (e.keyCode === 13) {
60+
else if (e.keyCode === 13 && !e.shiftKey) {
6161
saveText();
6262
}
6363
}

src/render/renderText.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
import setAttributes from '../utils/setAttributes';
22
import normalizeColor from '../utils/normalizeColor';
33

4+
/**
5+
* Wrap each line of given text in a `<tspan>` element and append these
6+
* lines to the given SVGTextElement
7+
*
8+
* @param {SVGTextElement} textElement A text element to hold the split text
9+
* @param {String} textContent String to render with line breaks
10+
*/
11+
function insertLineBreaks(textElement, textContent) {
12+
const lines = (textContent || '').split('\n');
13+
// can't use dy attribute here since we want empty lines to take up space as well,
14+
// so we will update y manually based on font size
15+
const x = textElement.getAttribute('x');
16+
let y = Number(textElement.getAttribute('y'));
17+
const size = Number(textElement.getAttribute('font-size'));
18+
for (const line of lines) {
19+
const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
20+
tspan.setAttribute('y', y.toString());
21+
tspan.setAttribute('x', x);
22+
tspan.innerHTML = line;
23+
textElement.appendChild(tspan);
24+
25+
y += size;
26+
}
27+
}
28+
429
/**
530
* Create SVGTextElement from an annotation definition.
631
* This is used for anntations of type `textbox`.
@@ -20,7 +45,8 @@ export default function renderText(a) {
2045
transform: `rotate(${a.rotation})`,
2146
style: 'white-space: pre'
2247
});
23-
text.innerHTML = a.content;
48+
49+
insertLineBreaks(text, a.content);
2450

2551
let g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
2652
g.appendChild(text);

0 commit comments

Comments
 (0)