Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Text Wrapping and Resizing for FreeText Annotations #18447

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 98 additions & 5 deletions src/display/annotation_layer.js
shivansh84ya marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2505,20 +2505,31 @@ class FreeTextAnnotationElement extends AnnotationElement {
this.textContent = parameters.data.textContent;
this.textPosition = parameters.data.textPosition;
this.annotationEditorType = AnnotationEditorType.FREETEXT;
this.zoomLevel = 1;
this.initialFontSize = 16;
this.initialLineHeight = 1.2;
this.pageWidth = parameters.data.pageWidth || 600; // Default page width if not provided
}

render() {
this.container.classList.add("freeTextAnnotation");
this.container.style.position = "relative";
this.container.style.overflow = "hidden";

if (this.textContent) {
const content = document.createElement("div");
content.classList.add("annotationTextContent");
content.setAttribute("role", "comment");
for (const line of this.textContent) {
const lineSpan = document.createElement("span");
lineSpan.textContent = line;
content.append(lineSpan);
}
content.style.wordWrap = "break-word";
content.style.whiteSpace = "pre-wrap";
content.style.fontSize = `${this.initialFontSize}px`;
content.style.lineHeight = `${this.initialLineHeight}`;
content.style.width = "100%";
content.style.height = "100%";
content.style.boxSizing = "border-box";
content.textContent = this.textContent;

this.content = content;
this.container.append(content);
}

Expand All @@ -2527,9 +2538,91 @@ class FreeTextAnnotationElement extends AnnotationElement {
}

this._editOnDoubleClick();
this._addResizeHandles();

// Listen for zoom changes
window.addEventListener("zoom", this._onZoom.bind(this));

return this.container;
}

_addResizeHandles() {
const handleSize = 8;

const createHandle = cursor => {
const handle = document.createElement("div");
handle.style.width = `${handleSize}px`;
handle.style.height = `${handleSize}px`;
handle.style.background = "rgba(0, 0, 0, 0.5)";
handle.style.position = "absolute";
handle.style.cursor = cursor;
return handle;
};

// Create resize handles
const handles = {
se: createHandle("se-resize"),
};

handles.se.style.right = "0px";
handles.se.style.bottom = "0px";

// Append handles to the container
this.container.append(handles.se);

// Add event listeners for resizing
this._addResizeListeners(handles);
}

_addResizeListeners(handles) {
const onResize = event => {
const rect = this.container.getBoundingClientRect();
const newWidth = Math.min(
(event.clientX - rect.left) / this.zoomLevel,
this.pageWidth
);
const newHeight = (event.clientY - rect.top) / this.zoomLevel;
this.container.style.width = `${newWidth}px`;
this.container.style.height = `${newHeight}px`;
this.content.style.width = `${newWidth}px`;

// Update font size proportionally
const newFontSize = (this.initialFontSize * newHeight) / (rect.height || newHeight);
this.content.style.fontSize = `${newFontSize}px`;
};

const onStopResize = () => {
window.removeEventListener("mousemove", onResize);
window.removeEventListener("mouseup", onStopResize);
};

handles.se.addEventListener("mousedown", () => {
window.addEventListener("mousemove", onResize);
window.addEventListener("mouseup", onStopResize);
});
}

_onZoom(event) {
this.zoomLevel = event.detail.zoom;
this._applyZoom();
}

_applyZoom() {
// Adjust the dimensions and positions according to the zoom level
const rect = this.data.rect;
const scale = this.zoomLevel;

this.container.style.width = `${rect[2] - rect[0]}px`;
this.container.style.height = `${rect[3] - rect[1]}px`;

this.container.style.transform = `scale(${scale})`;
this.container.style.transformOrigin = "top left";
}

// Make sure to clean up event listeners
cleanup() {
window.removeEventListener("zoom", this._onZoom.bind(this));
}
}

class LineAnnotationElement extends AnnotationElement {
Expand Down
106 changes: 54 additions & 52 deletions web/annotation_editor_layer_builder.css
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,24 @@
width: auto;
height: auto;
touch-action: none;
overflow-wrap: break-word; /* Ensure text wraps */
resize: both; /* Allow resizing */
box-sizing: border-box; /* Include padding and border in element's total width and height */
}

.annotationEditorLayer .freeTextEditor .internal {
background: transparent;
border: none;
inset: 0;
overflow: visible;
white-space: nowrap;
white-space: pre-wrap;
font: 10px sans-serif;
line-height: var(--freetext-line-height);
user-select: none;
width: 100%;
height: 100%;
box-sizing: border-box;
}

.annotationEditorLayer .freeTextEditor .overlay {
position: absolute;
display: none;
Expand All @@ -476,7 +481,7 @@
height: 100%;
}

.annotationEditorLayer freeTextEditor .overlay.enabled {
.annotationEditorLayer .freeTextEditor .overlay.enabled {
display: block;
}

Expand Down Expand Up @@ -521,66 +526,63 @@
}
}

.annotationEditorLayer {
:is(.freeTextEditor, .inkEditor, .stampEditor) {
& > .resizers {
position: absolute;
inset: 0;
.annotationEditorLayer :is(.freeTextEditor, .inkEditor, .stampEditor) > .resizers {
position: absolute;
inset: 0;

&.hidden {
display: none;
}
&.hidden {
display: none;
}

& > .resizer {
width: var(--resizer-size);
height: var(--resizer-size);
background: content-box var(--resizer-bg-color);
border: var(--focus-outline-around);
border-radius: 2px;
position: absolute;

&.topLeft {
top: var(--resizer-shift);
left: var(--resizer-shift);
}
> .resizer {
width: var(--resizer-size);
height: var(--resizer-size);
background: content-box var(--resizer-bg-color);
border: var(--focus-outline-around);
border-radius: 2px;
position: absolute;

&.topMiddle {
top: var(--resizer-shift);
left: calc(50% + var(--resizer-shift));
}
&.topLeft {
top: var(--resizer-shift);
left: var(--resizer-shift);
}

&.topRight {
top: var(--resizer-shift);
right: var(--resizer-shift);
}
&.topMiddle {
top: var(--resizer-shift);
left: calc(50% + var(--resizer-shift));
}

&.middleRight {
top: calc(50% + var(--resizer-shift));
right: var(--resizer-shift);
}
&.topRight {
top: var(--resizer-shift);
right: var(--resizer-shift);
}

&.bottomRight {
bottom: var(--resizer-shift);
right: var(--resizer-shift);
}
&.middleRight {
top: calc(50% + var(--resizer-shift));
right: var(--resizer-shift);
}

&.bottomMiddle {
bottom: var(--resizer-shift);
left: calc(50% + var(--resizer-shift));
}
&.bottomRight {
bottom: var(--resizer-shift);
right: var(--resizer-shift);
}

&.bottomLeft {
bottom: var(--resizer-shift);
left: var(--resizer-shift);
}
&.bottomMiddle {
bottom: var(--resizer-shift);
left: calc(50% + var(--resizer-shift));
}

&.middleLeft {
top: calc(50% + var(--resizer-shift));
left: var(--resizer-shift);
}
}
&.bottomLeft {
bottom: var(--resizer-shift);
left: var(--resizer-shift);
}

&.middleLeft {
top: calc(50% + var(--resizer-shift));
left: var(--resizer-shift);
}
}
}

&[data-main-rotation="0"]
:is([data-editor-rotation="0"], [data-editor-rotation="180"]),
Expand Down