Skip to content

Commit

Permalink
Allow newlines in text annotations (Submitty#356)
Browse files Browse the repository at this point in the history
* allow newlines in text annotations

* linting
  • Loading branch information
eddiekrystowski authored Aug 17, 2021
1 parent c03ce73 commit 8ffad71
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/UI/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function handleDocumentMouseup(e) {
if (!e.srcElement.classList.contains('annotationLayer')) {
return;
}
input = document.createElement('input');
input = document.createElement('textarea');
input.setAttribute('id', 'pdf-annotate-text-input');
input.setAttribute('placeholder', 'Enter text');
input.setAttribute('placeholder', 'Enter text... SHIFT + ENTER for new line');
input.style.border = `3px solid ${BORDER_COLOR}`;
input.style.borderRadius = '3px';
input.style.position = 'absolute';
Expand Down Expand Up @@ -57,7 +57,7 @@ function handleInputKeyup(e) {
if (e.keyCode === 27) {
closeInput();
}
else if (e.keyCode === 13) {
else if (e.keyCode === 13 && !e.shiftKey) {
saveText();
}
}
Expand Down
28 changes: 27 additions & 1 deletion src/render/renderText.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import setAttributes from '../utils/setAttributes';
import normalizeColor from '../utils/normalizeColor';

/**
* Wrap each line of given text in a `<tspan>` element and append these
* lines to the given SVGTextElement
*
* @param {SVGTextElement} textElement A text element to hold the split text
* @param {String} textContent String to render with line breaks
*/
function insertLineBreaks(textElement, textContent) {
const lines = (textContent || '').split('\n');
// can't use dy attribute here since we want empty lines to take up space as well,
// so we will update y manually based on font size
const x = textElement.getAttribute('x');
let y = Number(textElement.getAttribute('y'));
const size = Number(textElement.getAttribute('font-size'));
for (const line of lines) {
const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
tspan.setAttribute('y', y.toString());
tspan.setAttribute('x', x);
tspan.innerHTML = line;
textElement.appendChild(tspan);

y += size;
}
}

/**
* Create SVGTextElement from an annotation definition.
* This is used for anntations of type `textbox`.
Expand All @@ -20,7 +45,8 @@ export default function renderText(a) {
transform: `rotate(${a.rotation})`,
style: 'white-space: pre'
});
text.innerHTML = a.content;

insertLineBreaks(text, a.content);

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

0 comments on commit 8ffad71

Please sign in to comment.