|
| 1 | +/* Copyright 2025 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { noContextMenu } from "../display_utils.js"; |
| 17 | + |
| 18 | +class Comment { |
| 19 | + #commentButton = null; |
| 20 | + |
| 21 | + #commentWasFromKeyBoard = false; |
| 22 | + |
| 23 | + #editor = null; |
| 24 | + |
| 25 | + #initialText = null; |
| 26 | + |
| 27 | + #text = null; |
| 28 | + |
| 29 | + #date = null; |
| 30 | + |
| 31 | + #deleted = false; |
| 32 | + |
| 33 | + constructor(editor) { |
| 34 | + this.#editor = editor; |
| 35 | + this.toolbar = null; |
| 36 | + } |
| 37 | + |
| 38 | + render() { |
| 39 | + if (!this.#editor._uiManager.hasCommentManager()) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + const comment = (this.#commentButton = document.createElement("button")); |
| 43 | + comment.className = "comment"; |
| 44 | + comment.tabIndex = "0"; |
| 45 | + comment.setAttribute("data-l10n-id", "pdfjs-editor-edit-comment-button"); |
| 46 | + |
| 47 | + const signal = this.#editor._uiManager._signal; |
| 48 | + comment.addEventListener("contextmenu", noContextMenu, { signal }); |
| 49 | + comment.addEventListener("pointerdown", event => event.stopPropagation(), { |
| 50 | + signal, |
| 51 | + }); |
| 52 | + |
| 53 | + const onClick = event => { |
| 54 | + event.preventDefault(); |
| 55 | + this.edit(); |
| 56 | + }; |
| 57 | + comment.addEventListener("click", onClick, { capture: true, signal }); |
| 58 | + comment.addEventListener( |
| 59 | + "keydown", |
| 60 | + event => { |
| 61 | + if (event.target === comment && event.key === "Enter") { |
| 62 | + this.#commentWasFromKeyBoard = true; |
| 63 | + onClick(event); |
| 64 | + } |
| 65 | + }, |
| 66 | + { signal } |
| 67 | + ); |
| 68 | + |
| 69 | + return comment; |
| 70 | + } |
| 71 | + |
| 72 | + edit() { |
| 73 | + const { bottom, left, right } = this.#editor.getClientDimensions(); |
| 74 | + const position = { top: bottom }; |
| 75 | + if (this.#editor._uiManager.direction === "ltr") { |
| 76 | + position.right = right; |
| 77 | + } else { |
| 78 | + position.left = left; |
| 79 | + } |
| 80 | + this.#editor._uiManager.editComment(this.#editor, position); |
| 81 | + } |
| 82 | + |
| 83 | + finish() { |
| 84 | + if (!this.#commentButton) { |
| 85 | + return; |
| 86 | + } |
| 87 | + this.#commentButton.focus({ focusVisible: this.#commentWasFromKeyBoard }); |
| 88 | + this.#commentWasFromKeyBoard = false; |
| 89 | + } |
| 90 | + |
| 91 | + isDeleted() { |
| 92 | + return this.#deleted || this.#text === ""; |
| 93 | + } |
| 94 | + |
| 95 | + hasBeenEdited() { |
| 96 | + return this.isDeleted() || this.#text !== this.#initialText; |
| 97 | + } |
| 98 | + |
| 99 | + serialize() { |
| 100 | + return this.data; |
| 101 | + } |
| 102 | + |
| 103 | + get data() { |
| 104 | + return { |
| 105 | + text: this.#text, |
| 106 | + date: this.#date, |
| 107 | + deleted: this.#deleted, |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Set the comment data. |
| 113 | + */ |
| 114 | + set data(text) { |
| 115 | + if (text === null) { |
| 116 | + this.#text = ""; |
| 117 | + this.#deleted = true; |
| 118 | + return; |
| 119 | + } |
| 120 | + this.#text = text; |
| 121 | + this.#date = new Date(); |
| 122 | + this.#deleted = false; |
| 123 | + } |
| 124 | + |
| 125 | + setInitialText(text) { |
| 126 | + this.#initialText = text; |
| 127 | + this.data = text; |
| 128 | + } |
| 129 | + |
| 130 | + toggle(enabled = false) { |
| 131 | + if (!this.#commentButton) { |
| 132 | + return; |
| 133 | + } |
| 134 | + this.#commentButton.disabled = !enabled; |
| 135 | + } |
| 136 | + |
| 137 | + shown() {} |
| 138 | + |
| 139 | + destroy() { |
| 140 | + this.#commentButton?.remove(); |
| 141 | + this.#commentButton = null; |
| 142 | + this.#text = ""; |
| 143 | + this.#date = null; |
| 144 | + this.#editor = null; |
| 145 | + this.#commentWasFromKeyBoard = false; |
| 146 | + this.#deleted = false; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +export { Comment }; |
0 commit comments