SignCreatePanel.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <template>
  2. <div class="signatures-popup">
  3. <div class="sign-popup-header">
  4. <div @click="changeActiveSignWay('trackpad')" :class="{ active: activeSignWay === 'trackpad' }">{{ $t('signatures.trackpad') }}</div>
  5. <div @click="changeActiveSignWay('keyboard')" :class="{ active: activeSignWay === 'keyboard' }">{{ $t('signatures.keyboard') }}</div>
  6. <div @click="changeActiveSignWay('image')" :class="{ active: activeSignWay === 'image' }">{{ $t('signatures.image') }}</div>
  7. </div>
  8. <div :class="{ mobile: isMobile }">
  9. <div class="paint-container" ref="paintContainer">
  10. <canvas v-show="activeSignWay === 'trackpad'" id="electronicTrackpadCanvas" :width="canvasWidth" :height="canvasHeight"></canvas>
  11. <div class="keyboard-paint" v-show="activeSignWay === 'keyboard'">
  12. <input type="text" :placeholder="$t('signatures.signHere')" v-model="textSgin" name="signature">
  13. </div>
  14. <div class="image-paint" v-show="activeSignWay === 'image'">
  15. <a>{{ $t('signatures.selectFile') }}<input type="file" id="signImageInput" @change="handleFile" accept=".png, .jpg, .jpeg" @click="clickUpload" name="signature"></a>
  16. </div>
  17. </div>
  18. <div class="property-container" v-show="['trackpad', 'keyboard'].includes(activeSignWay)">
  19. <div v-show="activeSignWay === 'trackpad'" class="width-tool">
  20. <span v-if="!isMobile">{{ $t('signatures.lineWidth') }}</span>
  21. <n-slider v-model:value="widthValue" :step="1" :max="10" :min="1" class="width-slider">
  22. <template #thumb>
  23. <Slider />
  24. </template>
  25. </n-slider>
  26. <input v-model="widthValue" type="text" pattern="\d*" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" @blur="handleBlur" name="signature">
  27. </div>
  28. <div v-show="activeSignWay === 'keyboard'" class="font-tool">
  29. <span v-if="!isMobile">{{ $t('font') }}</span>
  30. <select id="fontFamily" v-model="fontFamily" v-on:change="handleFontChange">
  31. <option value="Arial">Arial</option>
  32. <option value="Courier New">Courier New</option>
  33. <option value="Times New Roman">Times New Roman</option>
  34. </select>
  35. </div>
  36. <div v-show="activeSignWay === 'trackpad' || activeSignWay === 'keyboard'" class="color-tool">
  37. <span v-if="!isMobile">{{ $t('color') }}</span>
  38. <div class="colors-container">
  39. <span class="cell-container">
  40. <span class="cell-outer" :class="{ active: areColorsSimilar(fontColor, 'rgb(255, 0, 0)') }" @click="setActiveToolColor('rgb(255, 0, 0)')">
  41. <span class="cell red"></span></span>
  42. </span>
  43. <span class="cell-container">
  44. <span class="cell-outer" :class="{ active: areColorsSimilar(fontColor, 'rgb(0, 0, 0)') }" @click="setActiveToolColor('rgb(0, 0, 0)')">
  45. <span class="cell black"></span></span>
  46. </span>
  47. <span class="cell-container">
  48. <span class="cell-outer" :class="{ active: areColorsSimilar(fontColor, 'rgb(45, 119, 250)') }" @click="setActiveToolColor('rgb(45, 119, 250)')">
  49. <span class="cell blue"></span></span>
  50. </span>
  51. <div class="color-picker">
  52. <div class="preview" :style="{ backgroundColor: colorPickerValue }"></div>
  53. <n-color-picker
  54. to=".signatures-popup"
  55. @complete="onColorPickerComplete"
  56. @update:value="onColorPickerUpdate"
  57. :value="colorPickerValue"
  58. :show-alpha="false"
  59. >
  60. <template #label><img src="../Icon/colorful.svg" alt="colorPicker"></template>
  61. </n-color-picker>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. <div class="buttons-container">
  68. <div class="clear" :class="{ 'button': !isMobile }" @click="clearData">{{ $t('signatures.clear') }}</div>
  69. <div class="right-btns">
  70. <div class="button" @click="closePanel('cancel')">{{ $t('cancel') }}</div>
  71. <div class="button blue" @click="save">{{ $t('signatures.save') }}</div>
  72. </div>
  73. </div>
  74. </div>
  75. </template>
  76. <script setup>
  77. import { toRaw, ref, computed, h, watch, getCurrentInstance, onMounted, nextTick } from 'vue'
  78. import { useViewerStore } from '@/stores/modules/viewer'
  79. import { useDocumentStore } from '@/stores/modules/document'
  80. import core from '@/core'
  81. import MessageError from '@/assets/icons/icon-message-error.svg'
  82. import { NSlider, NColorPicker } from 'naive-ui'
  83. import { isMobile } from '@/helpers/device'
  84. import { areColorsSimilar } from '@/helpers/utils'
  85. const instance = getCurrentInstance().appContext.app.config.globalProperties
  86. const useViewer = useViewerStore()
  87. const useDocument = useDocumentStore()
  88. const show = computed(() => useViewer.isElementOpen('signCreatePanel'))
  89. const activeSignWay = computed(() => useViewer.getActiveElementTab('signPanel'))
  90. const toolMode = computed(() => useViewer.getToolMode)
  91. const activeTool = computed(() => useDocument.getActiveTool)
  92. const textSgin = ref('')
  93. const imageData = ref('')
  94. const widthValue = ref(5)
  95. const fontColor = ref('#000000')
  96. const fontFamily = ref('Courier New')
  97. const paintContainer = ref()
  98. const canvasWidth = ref(578)
  99. const canvasHeight = ref(270)
  100. const colorPickerValue = ref('rgb(0, 0, 0)')
  101. watch(() => toolMode.value, (newValue, oldValue) => {
  102. if (!(oldValue === 'sign' && newValue === 'view') && newValue !== oldValue) {
  103. const imgUi = document.getElementById("sign-image-save")
  104. if (imgUi) imgUi.remove()
  105. }
  106. })
  107. watch(() => widthValue.value, (newValue, oldValue) => {
  108. const regex = /^\d+$/
  109. if ((newValue !== '' && (newValue > 10 || newValue <= 0 || !regex.test(newValue)))) {
  110. widthValue.value = oldValue
  111. }
  112. if (activeSignWay.value === 'trackpad') {
  113. core.handleSign({
  114. flag: 'update',
  115. param: {
  116. width: widthValue.value,
  117. color: fontColor.value
  118. }
  119. })
  120. }
  121. })
  122. watch(() => fontColor.value, (newValue, oldValue) => {
  123. core.handleSign({
  124. flag: 'update',
  125. param: {
  126. width: widthValue.value,
  127. color: fontColor.value
  128. }
  129. })
  130. if (activeSignWay.value === "keyboard") {
  131. colorPickerValue.value = fontColor.value
  132. let inputElement = document.querySelector(".keyboard-paint input")
  133. if (inputElement) {
  134. inputElement.style.color = fontColor.value
  135. }
  136. }
  137. })
  138. watch(() => show.value, (newValue, oldValue) => {
  139. if (newValue) {
  140. if (isMobile) {
  141. nextTick(() => {
  142. const { clientWidth, clientHeight } = paintContainer.value
  143. clientWidth && canvasWidth.value !== clientWidth && (canvasWidth.value = clientWidth)
  144. clientHeight && canvasHeight.value !== clientHeight && (canvasHeight.value = clientHeight)
  145. })
  146. }
  147. useViewer.setActiveSignWay('keyboard')
  148. textSgin.value = ''
  149. core.handleSign({ flag: 'create' })
  150. }
  151. })
  152. const changeActiveSignWay = (way) => {
  153. useViewer.setActiveSignWay(way)
  154. clearData()
  155. clearProperty()
  156. }
  157. const closePanel = (flag) => {
  158. useViewer.toggleElement('signCreatePanel')
  159. if (flag === 'cancel' && activeSignWay.value === 'trackpad') {
  160. core.handleSign({ flag: 'reset' })
  161. }
  162. if (activeSignWay.value !== 'trackpad') {
  163. clearData()
  164. }
  165. clearProperty()
  166. document.body.style.overflow = 'auto'
  167. if (!activeTool.value && flag === 'cancel') useViewer.openElement('selectSignTypeDialog')
  168. }
  169. const clearData = () => {
  170. if (activeSignWay.value === 'keyboard') {
  171. textSgin.value = ''
  172. }
  173. if (activeSignWay.value === "image" || activeTool.value === 'image') {
  174. imageData.value = ''
  175. let imageDiv = document.querySelector('.image-paint')
  176. let imageInput = document.querySelector('.image-paint input')
  177. let imageLabel = document.querySelector('.image-paint a')
  178. let imageShow = document.querySelector('.image-paint img')
  179. if (imageDiv) {
  180. if (imageShow) {
  181. imageDiv.removeChild(imageShow)
  182. imageInput.value = ''
  183. }
  184. if (imageLabel) {
  185. imageLabel.style.display = "block"
  186. }
  187. }
  188. }
  189. if (activeSignWay.value === 'trackpad') {
  190. core.handleSign({ flag: 'clear' })
  191. }
  192. }
  193. const clearProperty = () => {
  194. if (fontColor.value !== '#000000') {
  195. fontColor.value = '#000000'
  196. }
  197. if (fontFamily.value !== 'Courier New') {
  198. fontFamily.value = 'Courier New'
  199. let inputElement = document.querySelector(".keyboard-paint input")
  200. if (inputElement) {
  201. inputElement.style.fontFamily = 'Courier New'
  202. }
  203. }
  204. if (widthValue.value !== 5) {
  205. widthValue.value = 5
  206. core.handleSign({
  207. flag: 'update',
  208. param: {
  209. width: widthValue.value,
  210. color: fontColor.value
  211. }
  212. })
  213. }
  214. }
  215. const handleFile = (evt) => {
  216. let file = evt.target.files[0]
  217. if (file.size > 1024 * 1024) {
  218. window.$message.error(instance.$t('signatures.uploadError'), {
  219. duration: 5000,
  220. icon: () => h('img', { src: MessageError })
  221. })
  222. evt.target.value = ''
  223. return
  224. }
  225. if (toolMode.value === 'annotation') {
  226. useDocument.setToolState('image')
  227. core.handleCreateStatus({
  228. bool: true,
  229. fileData: file
  230. })
  231. }
  232. const fileReader = new FileReader()
  233. fileReader.readAsDataURL(file)
  234. fileReader.onload = function () {
  235. imageData.value = fileReader.result
  236. let imageDiv = document.querySelector('.image-paint')
  237. let imageLabel = document.querySelector('.image-paint a')
  238. if (imageDiv) {
  239. let imgUi = document.createElement("img")
  240. imgUi.setAttribute("src", imageData.value)
  241. imgUi.style.maxHeight = 220 + "px"
  242. imgUi.style.maxWidth = 568 + "px"
  243. imageLabel.style.display = "none"
  244. imageDiv.append(imgUi)
  245. }
  246. if (activeTool.value === 'image') {
  247. const image = new Image()
  248. image.onload = function () {
  249. save()
  250. }
  251. image.src = imageData.value
  252. }
  253. }
  254. }
  255. const clickUpload = (e) => {
  256. if (toolMode.value === 'annotation') {
  257. useDocument.setToolState('image')
  258. }
  259. }
  260. const save = () => {
  261. let showData = ''
  262. let imgWidth = 0
  263. let imgHeight = 0
  264. const selectedSignatureField = useViewer.getSelectedSignatureField
  265. if ((activeSignWay.value === "image" && imageData.value !== '') || (activeTool.value === 'image' && imageData.value !== '')) {
  266. showData = imageData.value
  267. }
  268. if (activeSignWay.value === "keyboard" && textSgin.value !== '') {
  269. let canvasItem = document.createElement("canvas")
  270. let canvasDraw = document.createElement("canvas")
  271. let ctx = canvasItem.getContext("2d")
  272. ctx.font = "46px " + fontFamily.value
  273. let textWidth = ctx.measureText(textSgin.value).width
  274. canvasDraw.width = textWidth
  275. canvasDraw.height = 60
  276. ctx = canvasDraw.getContext("2d")
  277. ctx.font = "46px " + fontFamily.value
  278. ctx.fillStyle = fontColor.value
  279. ctx.fillText(textSgin.value, 0, 50)
  280. imgWidth = textWidth
  281. imgHeight = 60
  282. showData = canvasDraw.toDataURL()
  283. }
  284. if (showData) {
  285. let docDiv = document.querySelector('.content')
  286. if (docDiv && activeTool.value === 'image') {
  287. let imgUi = document.getElementById("sign-image-save")
  288. if (!imgUi) {
  289. imgUi = document.createElement("img")
  290. imgUi.style.position = "absolute"
  291. imgUi.style.zIndex = 10
  292. imgUi.style.pointerEvents = "none"
  293. imgUi.style.maxHeight = "220px"
  294. imgUi.style.maxWidth = "568px"
  295. imgUi.style.border = "2px solid blue"
  296. imgUi.style.cursor = "crosshair"
  297. imgUi.id = "sign-image-save"
  298. }
  299. imgUi.setAttribute("src", showData)
  300. imgUi.style.left = "50%"
  301. imgUi.style.top = "50%"
  302. imgUi.style.display = "hidden"
  303. docDiv.append(imgUi)
  304. if (imgUi.offsetWidth > 0 && imgUi.offsetHeight > 0) {
  305. imgWidth = imgUi.offsetWidth
  306. imgHeight = imgUi.offsetHeight
  307. }
  308. let offsetX = Math.min(parseInt(imgWidth / 2), 568 / 2)
  309. let offsetY = Math.min(parseInt(imgHeight / 2), 220 / 2)
  310. imgUi.style.left = "calc(50% -" + offsetX + ")"
  311. imgUi.style.top = "calc(50% -" + offsetY + ")"
  312. imgUi.style.display = "block"
  313. docDiv.onmousemove = function (evt) {
  314. imgUi.style.left = (evt.clientX - offsetX) + "px"
  315. imgUi.style.top = (evt.clientY - offsetY) + "px"
  316. }
  317. } else {
  318. core.handleSign({
  319. flag: 'save',
  320. param: {
  321. imageBase64: showData,
  322. annotation: toRaw(selectedSignatureField)
  323. }
  324. })
  325. }
  326. }
  327. if (activeSignWay.value === 'trackpad') {
  328. core.handleSign({
  329. flag: 'save',
  330. param: {
  331. width: widthValue.value,
  332. color: fontColor.value,
  333. annotation: toRaw(selectedSignatureField)
  334. }
  335. })
  336. }
  337. if (activeTool.value === 'image') {
  338. clearData()
  339. } else {
  340. closePanel()
  341. }
  342. }
  343. const handleBlur = (event) => {
  344. if (!event.target.value) {
  345. widthValue.value = 1
  346. }
  347. }
  348. const setActiveToolColor = (color) => {
  349. fontColor.value = color
  350. }
  351. const handleFontChange = (evt) => {
  352. let inputElement = document.querySelector(".keyboard-paint input")
  353. if (inputElement) {
  354. inputElement.style.fontFamily = evt.target.value
  355. }
  356. }
  357. // 颜色选择器选中颜色事件
  358. const onColorPickerComplete = (value) => {
  359. if (!areColorsSimilar(value, fontColor.value)) setActiveToolColor(value)
  360. }
  361. // 颜色选择器颜色改变事件
  362. const onColorPickerUpdate = (value) => {
  363. colorPickerValue.value = value
  364. }
  365. </script>
  366. <style lang="scss">
  367. .signatures-popup {
  368. width: 628px;
  369. background: #fff;
  370. box-shadow: 2px 6px 18px rgba(0, 0, 0, 0.2);
  371. .sign-popup-header {
  372. display: flex;
  373. padding: 12px;
  374. > div {
  375. margin: 10px 12px 0;
  376. height: 30px;
  377. font-size: 16px;
  378. font-weight: 400;
  379. line-height: 24px;
  380. color: #666;
  381. cursor: pointer;
  382. &.active {
  383. color: #000;
  384. border-bottom: 2px solid #4982E6;
  385. }
  386. }
  387. }
  388. .paint-container {
  389. display: flex;
  390. justify-content: center;
  391. align-items: center;
  392. margin: 0 24px;
  393. height: 240px;
  394. background-color: #F2F3F5;
  395. .keyboard-paint {
  396. flex: 1;
  397. input {
  398. width: 100%;
  399. font-size: 46px;
  400. line-height: 64px;
  401. font-weight: 400;
  402. color: #000;
  403. border: none;
  404. background: transparent;
  405. text-align: center;
  406. font-family: 'Courier New', Courier, monospace;
  407. }
  408. }
  409. .image-paint {
  410. a {
  411. position: relative;
  412. font-size: 14px;
  413. line-height: 20px;
  414. font-weight: 400;
  415. color: #1460F3;
  416. text-decoration: underline;
  417. input[type="file"] {
  418. position: absolute;
  419. left: 0;
  420. width: 100%;
  421. height: 100%;
  422. opacity: 0;
  423. font-size: 0;
  424. cursor: pointer;
  425. }
  426. }
  427. }
  428. }
  429. .property-container {
  430. display: flex;
  431. justify-content: space-between;
  432. margin: 18px 24px 38px;
  433. min-height: 30px;
  434. font-family: Segoe UI;
  435. font-size: 14px;
  436. line-height: 20px;
  437. color: #000;
  438. .width-tool {
  439. display: flex;
  440. align-items: center;
  441. .n-slider.width-slider {
  442. margin-left: 12px;
  443. margin-right: 8px;
  444. width: 96px;
  445. .n-slider-rail {
  446. border-radius: 0;
  447. .n-slider-rail__fill {
  448. background-color: #0078D7;
  449. border-radius: 0;
  450. }
  451. }
  452. .n-slider-handles {
  453. left: 0;
  454. right: 0;
  455. }
  456. }
  457. input {
  458. padding: 6px;
  459. width: 72px;
  460. height: 30px;
  461. border: 1px solid #B4B4B4;
  462. outline: none;
  463. border-radius: 0;
  464. &:focus {
  465. border-color: #1460F3;
  466. }
  467. }
  468. }
  469. .color-tool {
  470. display: flex;
  471. align-items: center;
  472. .colors-container {
  473. margin-left: 8px;
  474. padding: 0;
  475. border: none;
  476. .cell-container {
  477. .cell-outer {
  478. border-width: 1px;
  479. width: 30px;
  480. height: 30px;
  481. &.active {
  482. .cell {
  483. width: 22px;
  484. height: 22px;
  485. }
  486. }
  487. .cell {
  488. width: 24px;
  489. height: 24px;
  490. }
  491. .red {
  492. background-color: rgb(255, 0, 0);
  493. }
  494. .black {
  495. background-color: rgb(0, 0, 0);
  496. }
  497. .blue {
  498. background-color: rgb(45, 119, 250);
  499. }
  500. }
  501. & + .cell-container {
  502. margin-left: 8px;
  503. }
  504. }
  505. .box {
  506. position: relative;
  507. background-color: transparent;
  508. border-radius: 50%;
  509. overflow: hidden;
  510. .box-outer {
  511. width: 100%;
  512. height: 100%;
  513. position: absolute;
  514. border-radius: 50%;
  515. border: 1px solid #DADBDE;
  516. background-color: transparent;
  517. }
  518. .box-inner {
  519. height: 110%;
  520. left: 14px;
  521. position: absolute;
  522. border: 1px solid #FA1E5D;
  523. background-color: #FA1E5D;
  524. transform: rotate(45deg) translate(0%, 0%);
  525. }
  526. }
  527. }
  528. }
  529. .font-tool {
  530. select {
  531. margin-left: 8px;
  532. padding: 4px 4px 4px 8px;
  533. // width: 232px;
  534. height: 28px;
  535. color: #43474D;
  536. border: 1px solid rgba(0, 0, 0, 0.12);
  537. border-radius: 1px;
  538. }
  539. }
  540. }
  541. .buttons-container {
  542. display: flex;
  543. justify-content: space-between;
  544. margin: 12px;
  545. .button {
  546. padding: 0 20px;
  547. height: 32px;
  548. font-size: 14px;
  549. font-weight: 400;
  550. line-height: 20px;
  551. color: #000;
  552. background-color: #E2E3E6;
  553. border-radius: 0;
  554. margin-right: 0;
  555. &.disabled {
  556. color: #999;
  557. background-color: rgba(0, 0, 0, 0.2);
  558. opacity: 1;
  559. }
  560. &.blue {
  561. color: #fff;
  562. background-color: #1460F3;
  563. &:hover {
  564. background-color: rgba(20, 96, 243, 0.80);
  565. }
  566. }
  567. }
  568. .right-btns {
  569. display: flex;
  570. .button+.button {
  571. margin-left: 8px;
  572. }
  573. }
  574. }
  575. }
  576. @media screen and (max-width: 640px) {
  577. .signatures-popup {
  578. width: calc(100% - 40px);
  579. border-radius: 4px;
  580. .sign-popup-header {
  581. padding: 0;
  582. background-color: var(--c-bg);
  583. }
  584. .mobile {
  585. display: flex;
  586. flex-direction: column-reverse;
  587. }
  588. .property-container {
  589. margin: 24px 25px 0;
  590. flex-direction: row-reverse;
  591. .color-tool .colors-container {
  592. margin-left: 0;
  593. }
  594. }
  595. .paint-container {
  596. margin: 24px 20px;
  597. background-color: var(--c-side-bg);
  598. border: 1px solid var(--c-header-border);
  599. border-radius: 4px;
  600. .keyboard-paint input {
  601. font-family: Segoe UI;
  602. font-size: 30px;
  603. &::placeholder {
  604. color: rgba(0, 0, 0, 0.3);
  605. }
  606. }
  607. }
  608. .buttons-container {
  609. margin: 0 20px 16px;
  610. align-items: center;
  611. .clear {
  612. font-size: 14px;
  613. }
  614. }
  615. }
  616. }
  617. @media screen and (max-width: 450px) {
  618. .signatures-popup .property-container .color-tool .colors-container {
  619. span {
  620. display: none;
  621. }
  622. .color-picker {
  623. margin-left: 0;
  624. height: 30px;
  625. }
  626. }
  627. }
  628. </style>