content-ui-editor-web.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global singlefile, window, document, fetch, DOMParser, getComputedStyle, setTimeout, clearTimeout, NodeFilter, Readability, isProbablyReaderable, matchMedia */
  24. (() => {
  25. const FORBIDDEN_TAG_NAMES = ["a", "area", "audio", "base", "br", "col", "command", "embed", "hr", "img", "iframe", "input", "keygen", "link", "meta", "param", "source", "track", "video", "wbr"];
  26. const BUTTON_ANCHOR_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAHdElNRQfjCh0VAjZTpsLvAAAA1klEQVQoz3XQMS8EURQF4O9txN9QSCjoNYRiy01EomDb3UbnR+j8BpSKrbaaQvwAyTZ2Qyg0WiwJiZjmKmasmWTnvObe8849971DFSe+/TjVgL6wpyMczxcMXYIzV/9kqyIY27Vsyb5J05KREMa1sQoS3r2W1Vxkcrms6Tph6q3usFARBJJUVjPBgZ6Je49u5ELCog0r1qy7YFMI4RxMfZRZFOxOa/bn28oi8rK7K6hrITw48uVT37MQBn9vOcS2l9K0OE9W0atHsqWtIwxlRk1ZdHXrxC+ueUcydrdI6QAAAABJRU5ErkJggg==";
  27. const BUTTON_CLOSE_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wodFQUaLj84ywAAANxJREFUOMuN08tKAzEUgOGPSrU6fTL3PpuuperCSr22OFX0hQSXYkfHzRkIcW4Hskg4/5/kJAf2cIZ77BuOORa4aBZOUcfYDEjmuIncXSO5SwQ11h2SIoFrVHgRyU8Dkjb4GdMmoU9SYJnBZQqnknUmKXE1Bu6T1EnRtn1wKikz+AfvbfCkRTDFR7ZW47Mj/99TLbOdq2T+iNlYeBfHvs2u89AmKXCdVbsp2EHsnEsO0+/ZBTcx65OcD8Bdkl+sJtGNVRTsDcdx/zy+cBL/BL5DBC7xOrKdj6L1V/AHRf5yO+i79cQAAAAASUVORK5CYII=";
  28. const SHADOW_MODE_ATTRIBUTE_NAME = "shadowmode";
  29. const SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME = "delegatesfocus";
  30. const SCRIPT_TEMPLATE_SHADOW_ROOT = "data-template-shadow-root";
  31. const NOTE_TAGNAME = "single-file-note";
  32. const NOTE_CLASS = "note";
  33. const NOTE_MASK_CLASS = "note-mask";
  34. const NOTE_HIDDEN_CLASS = "note-hidden";
  35. const NOTE_ANCHORED_CLASS = "note-anchored";
  36. const NOTE_SELECTED_CLASS = "note-selected";
  37. const NOTE_MOVING_CLASS = "note-moving";
  38. const NOTE_MASK_MOVING_CLASS = "note-mask-moving";
  39. const PAGE_MASK_CLASS = "page-mask";
  40. const MASK_CLASS = "single-file-mask";
  41. const PAGE_MASK_CONTAINER_CLASS = "single-file-page-mask";
  42. const HIGHLIGHT_CLASS = "single-file-highlight";
  43. const REMOVED_CONTENT_CLASS = "single-file-removed";
  44. const HIGHLIGHT_HIDDEN_CLASS = "single-file-highlight-hidden";
  45. const PAGE_MASK_ACTIVE_CLASS = "page-mask-active";
  46. const NOTE_INITIAL_POSITION_X = 20;
  47. const NOTE_INITIAL_POSITION_Y = 20;
  48. const NOTE_INITIAL_WIDTH = 150;
  49. const NOTE_INITIAL_HEIGHT = 150;
  50. const NOTE_HEADER_HEIGHT = 25;
  51. const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-single-file-disabled-noscript";
  52. const STYLE_FORMATTED_PAGE = `
  53. /* This Source Code Form is subject to the terms of the Mozilla Public
  54. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  55. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  56. /* Avoid adding ID selector rules in this style sheet, since they could
  57. * inadvertently match elements in the article content. */
  58. :root {
  59. --close-button-hover: #d94141;
  60. }
  61. body {
  62. --toolbar-bgcolor: #fbfbfb;
  63. --toolbar-border: #b5b5b5;
  64. --toolbar-hover: #ebebeb;
  65. --popup-bgcolor: #fbfbfb;
  66. --popup-border: #b5b5b5;
  67. --font-color: #4c4c4c;
  68. --icon-fill: #808080;
  69. /* light colours */
  70. }
  71. body.dark {
  72. --toolbar-bgcolor: #2a2a2d;
  73. --toolbar-border: #4B4A50;
  74. --toolbar-hover: #737373;
  75. --popup-bgcolor: #4b4a50;
  76. --popup-border: #65646a;
  77. --font-color: #fff;
  78. --icon-fill: #fff;
  79. /* dark colours */
  80. }
  81. body {
  82. padding: 64px 51px;
  83. }
  84. body.loaded {
  85. transition: color 0.4s, background-color 0.4s;
  86. }
  87. body.light {
  88. color: #333333;
  89. background-color: #ffffff;
  90. }
  91. body.dark {
  92. color: #eeeeee;
  93. background-color: #333333;
  94. }
  95. body.dark *::-moz-selection {
  96. background-color: #FFFFFF;
  97. color: #0095DD;
  98. }
  99. body.dark a::-moz-selection {
  100. color: #DD4800;
  101. }
  102. body.sepia {
  103. color: #5b4636;
  104. background-color: #f4ecd8;
  105. }
  106. body.sans-serif,
  107. body.sans-serif .remove-button {
  108. font-family: Helvetica, Arial, sans-serif;
  109. }
  110. body.serif,
  111. body.serif .remove-button {
  112. font-family: Georgia, "Times New Roman", serif;
  113. }
  114. .container {
  115. --font-size: 12;
  116. max-width: 30em;
  117. margin: 0 auto;
  118. font-size: var(--font-size);
  119. }
  120. .container.content-width1 {
  121. max-width: 20em;
  122. }
  123. .container.content-width2 {
  124. max-width: 25em;
  125. }
  126. .container.content-width3 {
  127. max-width: 30em;
  128. }
  129. .container.content-width4 {
  130. max-width: 35em;
  131. }
  132. .container.content-width5 {
  133. max-width: 40em;
  134. }
  135. .container.content-width6 {
  136. max-width: 45em;
  137. }
  138. .container.content-width7 {
  139. max-width: 50em;
  140. }
  141. .container.content-width8 {
  142. max-width: 55em;
  143. }
  144. .container.content-width9 {
  145. max-width: 60em;
  146. }
  147. /* Override some controls and content styles based on color scheme */
  148. body.light > .container > .header > .domain {
  149. border-bottom-color: #333333 !important;
  150. }
  151. body.sepia > .container > .header > .domain {
  152. border-bottom-color: #5b4636 !important;
  153. }
  154. body.dark > .container > .header > .domain {
  155. border-bottom-color: #eeeeee !important;
  156. }
  157. body.sepia > .container > .footer {
  158. background-color: #dedad4 !important;
  159. }
  160. body.light blockquote {
  161. border-inline-start: 2px solid #333333 !important;
  162. }
  163. body.sepia blockquote {
  164. border-inline-start: 2px solid #5b4636 !important;
  165. }
  166. body.dark blockquote {
  167. border-inline-start: 2px solid #eeeeee !important;
  168. }
  169. /* Add toolbar transition base on loaded class */
  170. body.loaded .toolbar {
  171. transition: transform 0.3s ease-out;
  172. }
  173. body:not(.loaded) .toolbar:-moz-locale-dir(ltr) {
  174. transform: translateX(-100%);
  175. }
  176. body:not(.loaded) .toolbar:-moz-locale-dir(rtl) {
  177. transform: translateX(100%);
  178. }
  179. .light-button {
  180. color: #333333;
  181. background-color: #ffffff;
  182. }
  183. .dark-button {
  184. color: #eeeeee;
  185. background-color: #333333;
  186. }
  187. .sepia-button {
  188. color: #5b4636;
  189. background-color: #f4ecd8;
  190. }
  191. .sans-serif-button {
  192. font-family: Helvetica, Arial, sans-serif;
  193. }
  194. .serif-button {
  195. font-family: Georgia, "Times New Roman", serif;
  196. }
  197. /* Loading/error message */
  198. .reader-message {
  199. margin-top: 40px;
  200. display: none;
  201. text-align: center;
  202. width: 100%;
  203. font-size: 0.9em;
  204. }
  205. /* Header */
  206. .header {
  207. text-align: start;
  208. display: none;
  209. }
  210. .domain {
  211. font-size: 0.9em;
  212. line-height: 1.48em;
  213. padding-bottom: 4px;
  214. font-family: Helvetica, Arial, sans-serif;
  215. text-decoration: none;
  216. border-bottom: 1px solid;
  217. color: #0095dd;
  218. }
  219. .header > h1 {
  220. font-size: 1.6em;
  221. line-height: 1.25em;
  222. width: 100%;
  223. margin: 30px 0;
  224. padding: 0;
  225. }
  226. .header > .credits {
  227. font-size: 0.9em;
  228. line-height: 1.48em;
  229. margin: 0 0 10px 0;
  230. padding: 0;
  231. font-style: italic;
  232. }
  233. .header > .meta-data {
  234. font-size: 0.65em;
  235. margin: 0 0 15px 0;
  236. }
  237. /*======= Controls toolbar =======*/
  238. .toolbar {
  239. font-family: Helvetica, Arial, sans-serif;
  240. position: fixed;
  241. height: 100%;
  242. top: 0;
  243. left: 0;
  244. margin: 0;
  245. padding: 0;
  246. list-style: none;
  247. background-color: var(--toolbar-bgcolor);
  248. -moz-user-select: none;
  249. border-right: 1px solid var(--toolbar-border);
  250. z-index: 1;
  251. }
  252. .button {
  253. display: block;
  254. background-size: 24px 24px;
  255. background-repeat: no-repeat;
  256. color: #333;
  257. background-color: var(--toolbar-bgcolor);
  258. height: 40px;
  259. padding: 0;
  260. }
  261. button {
  262. -moz-context-properties: fill;
  263. color: var(--font-color);
  264. fill: var(--icon-fill);
  265. }
  266. .toolbar .button {
  267. width: 40px;
  268. background-position: center;
  269. margin-right: -1px;
  270. border-top: 0;
  271. border-left: 0;
  272. border-right: 1px solid var(--toolbar-border);
  273. border-bottom: 1px solid var(--toolbar-border);
  274. background-color: var(--toolbar-bgcolor);
  275. }
  276. .button[hidden] {
  277. display: none;
  278. }
  279. .dropdown {
  280. text-align: center;
  281. list-style: none;
  282. margin: 0;
  283. padding: 0;
  284. }
  285. .dropdown li {
  286. margin: 0;
  287. padding: 0;
  288. }
  289. /*======= Popup =======*/
  290. .dropdown-popup {
  291. min-width: 300px;
  292. text-align: start;
  293. position: absolute;
  294. left: 48px; /* offset to account for toolbar width */
  295. z-index: 1000;
  296. background-color: var(--popup-bgcolor);
  297. visibility: hidden;
  298. border-radius: 4px;
  299. border: 1px solid var(--popup-border);
  300. border-bottom-width: 0;
  301. box-shadow: 0 1px 3px #c1c1c1;
  302. }
  303. .keep-open .dropdown-popup {
  304. z-index: initial;
  305. }
  306. .dropdown-popup > hr {
  307. display: none;
  308. }
  309. .open > .dropdown-popup {
  310. visibility: visible;
  311. }
  312. .dropdown-arrow {
  313. position: absolute;
  314. top: 30px; /* offset arrow from top of popup */
  315. left: -16px;
  316. width: 16px;
  317. height: 24px;
  318. background-image: url("chrome://global/skin/reader/RM-Type-Controls-Arrow.svg");
  319. display: block;
  320. -moz-context-properties: fill, stroke;
  321. fill: var(--popup-bgcolor);
  322. stroke: var(--popup-border);
  323. }
  324. /*======= Font style popup =======*/
  325. .font-type-buttons,
  326. .font-size-buttons,
  327. .color-scheme-buttons,
  328. .content-width-buttons,
  329. .line-height-buttons {
  330. display: flex;
  331. flex-direction: row;
  332. }
  333. .font-type-buttons > button:first-child {
  334. border-top-left-radius: 3px;
  335. }
  336. .font-type-buttons > button:last-child {
  337. border-top-right-radius: 3px;
  338. }
  339. .color-scheme-buttons > button:first-child {
  340. border-bottom-left-radius: 3px;
  341. }
  342. .color-scheme-buttons > button:last-child {
  343. border-bottom-right-radius: 3px;
  344. }
  345. .font-type-buttons > button,
  346. .font-size-buttons > button,
  347. .color-scheme-buttons > button,
  348. .content-width-buttons > button,
  349. .line-height-buttons > button {
  350. text-align: center;
  351. border: 0;
  352. }
  353. .font-type-buttons > button,
  354. .font-size-buttons > button,
  355. .content-width-buttons > button,
  356. .line-height-buttons > button {
  357. width: 50%;
  358. background-color: transparent;
  359. border-left: 1px solid var(--popup-border);
  360. border-bottom: 1px solid var(--popup-border);
  361. }
  362. .color-scheme-buttons > button {
  363. width: 33.33%;
  364. font-size: 14px;
  365. }
  366. .color-scheme-buttons > .dark-button {
  367. margin-top: -1px;
  368. height: 61px;
  369. }
  370. .font-type-buttons > button:first-child,
  371. .font-size-buttons > button:first-child,
  372. .content-width-buttons > button:first-child,
  373. .line-height-buttons > button:first-child {
  374. border-left: 0;
  375. }
  376. .font-type-buttons > button {
  377. display: inline-block;
  378. font-size: 62px;
  379. height: 100px;
  380. }
  381. .font-size-buttons > button,
  382. .color-scheme-buttons > button,
  383. .content-width-buttons > button,
  384. .line-height-buttons > button {
  385. height: 60px;
  386. }
  387. .font-type-buttons > button:active:hover,
  388. .font-type-buttons > button.selected,
  389. .color-scheme-buttons > button:active:hover,
  390. .color-scheme-buttons > button.selected {
  391. box-shadow: inset 0 -3px 0 0 #fc6420;
  392. }
  393. .font-type-buttons > button:active:hover,
  394. .font-type-buttons > button.selected {
  395. border-bottom: 1px solid #FC6420;
  396. }
  397. /* Make the serif button content the same size as the sans-serif button content. */
  398. .font-type-buttons > button > .description {
  399. font-size: 12px;
  400. margin-top: -5px;
  401. }
  402. /* Font sizes are different per-platform, so we need custom CSS to line them up. */
  403. .font-type-buttons > .sans-serif-button > .name {
  404. margin-top: 2px;
  405. }
  406. .font-type-buttons > .sans-serif-button > .description {
  407. margin-top: -4px;
  408. }
  409. .font-type-buttons > .serif-button > .name {
  410. font-size: 63px;
  411. }
  412. .button:hover,
  413. .font-size-buttons > button:hover,
  414. .font-type-buttons > button:hover,
  415. .content-width-buttons > button:hover,
  416. .line-height-buttons > button:hover {
  417. background-color: var(--toolbar-hover);
  418. }
  419. .dropdown.open,
  420. .button:active,
  421. .font-size-buttons > button:active,
  422. .font-size-buttons > button.selected,
  423. .content-width-buttons > button:active,
  424. .content-width-buttons > button.selected,
  425. .line-height-buttons > button:active,
  426. .line-height-buttons > button.selected {
  427. background-color: #dadada;
  428. }
  429. /* Only used on Android */
  430. .font-size-sample {
  431. display: none;
  432. }
  433. .minus-button,
  434. .plus-button,
  435. .content-width-minus-button,
  436. .content-width-plus-button,
  437. .line-height-minus-button,
  438. .line-height-plus-button {
  439. background-color: transparent;
  440. border: 0;
  441. background-size: 18px 18px;
  442. background-repeat: no-repeat;
  443. background-position: center;
  444. }
  445. /*======= Toolbar icons =======*/
  446. .close-button {
  447. background-image: url("chrome://global/skin/reader/RM-Close-24x24.svg");
  448. height: 68px;
  449. background-position: center 8px;
  450. }
  451. .close-button:hover {
  452. fill: #fff;
  453. background-color: var(--close-button-hover);
  454. border-bottom: 1px solid var(--close-button-hover);
  455. border-right: 1px solid var(--close-button-hover);
  456. }
  457. .close-button:hover:active {
  458. background-color: #AE2325;
  459. border-bottom: 1px solid #AE2325;
  460. border-right: 1px solid #AE2325;
  461. }
  462. .style-button {
  463. background-image: url("chrome://global/skin/reader/RM-Type-Controls-24x24.svg");
  464. }
  465. .minus-button {
  466. background-image: url("chrome://global/skin/reader/RM-Minus-24x24.svg");
  467. }
  468. .plus-button {
  469. background-image: url("chrome://global/skin/reader/RM-Plus-24x24.svg");
  470. }
  471. .content-width-minus-button {
  472. background-size: 42px 16px;
  473. background-image: url("chrome://global/skin/reader/RM-Content-Width-Minus-42x16.svg");
  474. }
  475. .content-width-plus-button {
  476. background-size: 44px 16px;
  477. background-image: url("chrome://global/skin/reader/RM-Content-Width-Plus-44x16.svg");
  478. }
  479. .line-height-minus-button {
  480. background-size: 34px 14px;
  481. background-image: url("chrome://global/skin/reader/RM-Line-Height-Minus-38x14.svg");
  482. }
  483. .line-height-plus-button {
  484. background-size: 34px 24px;
  485. background-image: url("chrome://global/skin/reader/RM-Line-Height-Plus-38x24.svg");
  486. }
  487. @media print {
  488. .toolbar {
  489. display: none !important;
  490. }
  491. }
  492. /*======= Article content =======*/
  493. /* Note that any class names from the original article that we want to match on
  494. * must be added to CLASSES_TO_PRESERVE in ReaderMode.jsm, so that
  495. * Readability.js doesn't strip them out */
  496. .moz-reader-content {
  497. display: none;
  498. font-size: 1em;
  499. line-height: 1.6em;
  500. }
  501. .moz-reader-content.line-height1 {
  502. line-height: 1em;
  503. }
  504. .moz-reader-content.line-height2 {
  505. line-height: 1.2em;
  506. }
  507. .moz-reader-content.line-height3 {
  508. line-height: 1.4em;
  509. }
  510. .moz-reader-content.line-height4 {
  511. line-height: 1.6em;
  512. }
  513. .moz-reader-content.line-height5 {
  514. line-height: 1.8em;
  515. }
  516. .moz-reader-content.line-height6 {
  517. line-height: 2.0em;
  518. }
  519. .moz-reader-content.line-height7 {
  520. line-height: 2.2em;
  521. }
  522. .moz-reader-content.line-height8 {
  523. line-height: 2.4em;
  524. }
  525. .moz-reader-content.line-height9 {
  526. line-height: 2.6em;
  527. }
  528. @media print {
  529. .moz-reader-content p,
  530. .moz-reader-content code,
  531. .moz-reader-content pre,
  532. .moz-reader-content blockquote,
  533. .moz-reader-content ul,
  534. .moz-reader-content ol,
  535. .moz-reader-content li,
  536. .moz-reader-content figure,
  537. .moz-reader-content .wp-caption {
  538. margin: 0 0 10px 0 !important;
  539. padding: 0 !important;
  540. }
  541. }
  542. .moz-reader-content h1,
  543. .moz-reader-content h2,
  544. .moz-reader-content h3 {
  545. font-weight: bold;
  546. }
  547. .moz-reader-content h1 {
  548. font-size: 1.6em;
  549. line-height: 1.25em;
  550. }
  551. .moz-reader-content h2 {
  552. font-size: 1.2em;
  553. line-height: 1.51em;
  554. }
  555. .moz-reader-content h3 {
  556. font-size: 1em;
  557. line-height: 1.66em;
  558. }
  559. .moz-reader-content a:link {
  560. text-decoration: underline;
  561. font-weight: normal;
  562. }
  563. .moz-reader-content a:link,
  564. .moz-reader-content a:link:hover,
  565. .moz-reader-content a:link:active {
  566. color: #0095dd;
  567. }
  568. .moz-reader-content a:visited {
  569. color: #c2e;
  570. }
  571. .moz-reader-content * {
  572. max-width: 100%;
  573. height: auto;
  574. }
  575. .moz-reader-content p,
  576. .moz-reader-content p,
  577. .moz-reader-content code,
  578. .moz-reader-content pre,
  579. .moz-reader-content blockquote,
  580. .moz-reader-content ul,
  581. .moz-reader-content ol,
  582. .moz-reader-content li,
  583. .moz-reader-content figure,
  584. .moz-reader-content .wp-caption {
  585. margin: -10px -10px 20px -10px;
  586. padding: 10px;
  587. border-radius: 5px;
  588. }
  589. .moz-reader-content li {
  590. margin-bottom: 0;
  591. }
  592. .moz-reader-content li > ul,
  593. .moz-reader-content li > ol {
  594. margin-bottom: -10px;
  595. }
  596. .moz-reader-content p > img:only-child,
  597. .moz-reader-content p > a:only-child > img:only-child,
  598. .moz-reader-content .wp-caption img,
  599. .moz-reader-content figure img {
  600. display: block;
  601. }
  602. .moz-reader-content img[moz-reader-center] {
  603. margin-left: auto;
  604. margin-right: auto;
  605. }
  606. .moz-reader-content .caption,
  607. .moz-reader-content .wp-caption-text
  608. .moz-reader-content figcaption {
  609. font-size: 0.9em;
  610. line-height: 1.48em;
  611. font-style: italic;
  612. }
  613. .moz-reader-content code,
  614. .moz-reader-content pre {
  615. white-space: pre-wrap;
  616. }
  617. .moz-reader-content blockquote {
  618. padding: 0;
  619. padding-inline-start: 16px;
  620. }
  621. .moz-reader-content ul,
  622. .moz-reader-content ol {
  623. padding: 0;
  624. }
  625. .moz-reader-content ul {
  626. padding-inline-start: 30px;
  627. list-style: disc;
  628. }
  629. .moz-reader-content ol {
  630. padding-inline-start: 30px;
  631. list-style: decimal;
  632. }
  633. table,
  634. th,
  635. td {
  636. border: 1px solid currentColor;
  637. border-collapse: collapse;
  638. padding: 6px;
  639. vertical-align: top;
  640. }
  641. table {
  642. margin: 5px;
  643. }
  644. /* Visually hide (but don't display: none) screen reader elements */
  645. .moz-reader-content .visually-hidden,
  646. .moz-reader-content .visuallyhidden,
  647. .moz-reader-content .sr-only {
  648. display: inline-block;
  649. width: 1px;
  650. height: 1px;
  651. margin: -1px;
  652. overflow: hidden;
  653. padding: 0;
  654. border-width: 0;
  655. }
  656. /* Hide elements with common "hidden" class names */
  657. .moz-reader-content .hidden,
  658. .moz-reader-content .invisible {
  659. display: none;
  660. }
  661. /* Enforce wordpress and similar emoji/smileys aren't sized to be full-width,
  662. * see bug 1399616 for context. */
  663. .moz-reader-content img.wp-smiley,
  664. .moz-reader-content img.emoji {
  665. display: inline-block;
  666. border-width: 0;
  667. /* height: auto is implied from .moz-reader-content * rule. */
  668. width: 1em;
  669. margin: 0 .07em;
  670. padding: 0;
  671. }
  672. .reader-show-element {
  673. display: initial;
  674. }`;
  675. let NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET;
  676. let selectedNote, anchorElement, maskNoteElement, maskPageElement, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, highlightColor, collapseNoteTimeout, cuttingMode;
  677. let removedElements = [];
  678. window.onmessage = async event => {
  679. const message = JSON.parse(event.data);
  680. if (message.method == "init") {
  681. await initConstants();
  682. const contentDocument = (new DOMParser()).parseFromString(message.content, "text/html");
  683. if (contentDocument.doctype) {
  684. if (document.doctype) {
  685. document.replaceChild(contentDocument.doctype, document.doctype);
  686. } else {
  687. document.insertBefore(contentDocument.doctype, document.documentElement);
  688. }
  689. } else {
  690. document.doctype.remove();
  691. }
  692. contentDocument.querySelectorAll("noscript").forEach(element => {
  693. element.setAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME, element.innerHTML);
  694. element.textContent = "";
  695. });
  696. document.replaceChild(contentDocument.documentElement, document.documentElement);
  697. deserializeShadowRoots(document);
  698. const iconElement = document.querySelector("link[rel*=icon]");
  699. window.parent.postMessage(JSON.stringify({ "method": "setMetadata", title: document.title, icon: iconElement && iconElement.href }), "*");
  700. if (!isProbablyReaderable(document)) {
  701. window.parent.postMessage(JSON.stringify({ "method": "disableFormatPage" }), "*");
  702. }
  703. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => attachNoteListeners(containerElement, true));
  704. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  705. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  706. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  707. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  708. window.onclick = event => event.preventDefault();
  709. }
  710. if (message.method == "addNote") {
  711. addNote(message);
  712. }
  713. if (message.method == "displayNotes") {
  714. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.remove(NOTE_HIDDEN_CLASS));
  715. }
  716. if (message.method == "hideNotes") {
  717. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.add(NOTE_HIDDEN_CLASS));
  718. }
  719. if (message.method == "enableHighlight") {
  720. highlightColor = message.color;
  721. highlightSelectionMode = true;
  722. }
  723. if (message.method == "disableHighlight") {
  724. highlightSelectionMode = false;
  725. }
  726. if (message.method == "displayHighlights") {
  727. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  728. }
  729. if (message.method == "hideHighlights") {
  730. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.add(HIGHLIGHT_HIDDEN_CLASS));
  731. }
  732. if (message.method == "enableRemoveHighlights") {
  733. removeHighlightMode = true;
  734. }
  735. if (message.method == "disableRemoveHighlights") {
  736. removeHighlightMode = false;
  737. }
  738. if (message.method == "enableEditPage") {
  739. document.body.contentEditable = true;
  740. }
  741. if (message.method == "formatPage") {
  742. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => containerElement.remove());
  743. const article = new Readability(document).parse();
  744. document.body.innerHTML = "";
  745. const domParser = new DOMParser();
  746. const doc = domParser.parseFromString(article.content, "text/html");
  747. const contentEditable = document.body.contentEditable;
  748. document.documentElement.replaceChild(doc.body, document.body);
  749. document.body.contentEditable = contentEditable;
  750. document.head.querySelectorAll("style").forEach(styleElement => styleElement.remove());
  751. const styleElement = document.createElement("style");
  752. styleElement.textContent = STYLE_FORMATTED_PAGE;
  753. document.head.appendChild(styleElement);
  754. document.body.classList.add("moz-reader-content");
  755. document.body.classList.add("content-width6");
  756. document.body.classList.add("reader-show-element");
  757. document.body.classList.add("sans-serif");
  758. document.body.classList.add("container");
  759. document.body.classList.add("line-height4");
  760. if (matchMedia("prefers-color-scheme: dark")) {
  761. document.body.classList.add("dark");
  762. }
  763. document.body.style.setProperty("display", "block");
  764. document.body.style.setProperty("padding", "24px");
  765. const titleElement = document.createElement("h1");
  766. titleElement.classList.add("reader-title");
  767. titleElement.textContent = article.title;
  768. document.body.insertBefore(titleElement, document.body.firstChild);
  769. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  770. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  771. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  772. }
  773. if (message.method == "disableEditPage") {
  774. document.body.contentEditable = false;
  775. }
  776. if (message.method == "enableCutPage") {
  777. cuttingMode = true;
  778. document.body.addEventListener("mouseover", highlightElementToCut);
  779. document.body.addEventListener("mouseout", highlightElementToCut);
  780. }
  781. if (message.method == "disableCutPage") {
  782. cuttingMode = false;
  783. document.body.removeEventListener("mouseover", highlightElementToCut);
  784. document.body.removeEventListener("mouseout", highlightElementToCut);
  785. }
  786. if (message.method == "undoCutPage") {
  787. if (removedElements.length) {
  788. removedElements.pop().classList.remove(REMOVED_CONTENT_CLASS);
  789. }
  790. }
  791. if (message.method == "undoAllCutPage") {
  792. while (removedElements.length) {
  793. removedElements.pop().classList.remove(REMOVED_CONTENT_CLASS);
  794. }
  795. }
  796. if (message.method == "getContent") {
  797. serializeShadowRoots(document);
  798. const doc = document.cloneNode(true);
  799. deserializeShadowRoots(doc);
  800. deserializeShadowRoots(document);
  801. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  802. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  803. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  804. });
  805. doc.querySelectorAll("." + MASK_CLASS + ", ." + REMOVED_CONTENT_CLASS).forEach(maskElement => maskElement.remove());
  806. doc.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  807. doc.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(templateElement => {
  808. const noteElement = templateElement.querySelector("." + NOTE_CLASS);
  809. if (noteElement) {
  810. noteElement.classList.remove(NOTE_HIDDEN_CLASS);
  811. }
  812. const mainElement = templateElement.querySelector("textarea");
  813. if (mainElement) {
  814. mainElement.textContent = mainElement.value;
  815. }
  816. });
  817. delete doc.body.contentEditable;
  818. const scriptElement = doc.createElement("script");
  819. scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
  820. scriptElement.textContent = getEmbedScript();
  821. doc.body.appendChild(scriptElement);
  822. window.parent.postMessage(JSON.stringify({ "method": "setContent", content: singlefile.lib.modules.serializer.process(doc, message.compressHTML) }), "*");
  823. }
  824. };
  825. window.onresize = reflowNotes;
  826. async function initConstants() {
  827. [NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET] = await Promise.all([
  828. minifyText(await ((await fetch("editor-note-web.css")).text())),
  829. minifyText(await ((await fetch("editor-mask-web.css")).text())),
  830. minifyText(await ((await fetch("editor-frame-web.css")).text()))
  831. ]);
  832. }
  833. function addNote({ color }) {
  834. const containerElement = document.createElement(NOTE_TAGNAME);
  835. const noteElement = document.createElement("div");
  836. const headerElement = document.createElement("header");
  837. const mainElement = document.createElement("textarea");
  838. const resizeElement = document.createElement("div");
  839. const removeNoteElement = document.createElement("img");
  840. const anchorIconElement = document.createElement("img");
  841. const noteShadow = containerElement.attachShadow({ mode: "open" });
  842. headerElement.appendChild(anchorIconElement);
  843. headerElement.appendChild(removeNoteElement);
  844. noteElement.appendChild(headerElement);
  845. noteElement.appendChild(mainElement);
  846. noteElement.appendChild(resizeElement);
  847. noteShadow.appendChild(getStyleElement(NOTES_WEB_STYLESHEET));
  848. noteShadow.appendChild(noteElement);
  849. const notesElements = Array.from(document.querySelectorAll(NOTE_TAGNAME));
  850. const noteId = Math.max.call(Math, 0, ...notesElements.map(noteElement => Number(noteElement.dataset.noteId))) + 1;
  851. noteElement.classList.add(NOTE_CLASS);
  852. noteElement.classList.add(NOTE_ANCHORED_CLASS);
  853. noteElement.classList.add(color);
  854. const boundingRectDocument = document.documentElement.getBoundingClientRect();
  855. let positionX = NOTE_INITIAL_WIDTH + NOTE_INITIAL_POSITION_X - 1 - boundingRectDocument.x;
  856. let positionY = NOTE_INITIAL_HEIGHT + NOTE_INITIAL_POSITION_Y - 1 - boundingRectDocument.y;
  857. while (Array.from(document.elementsFromPoint(positionX - window.scrollX, positionY - window.scrollY)).find(element => element.tagName.toLowerCase() == NOTE_TAGNAME)) {
  858. positionX += NOTE_INITIAL_POSITION_X;
  859. positionY += NOTE_INITIAL_POSITION_Y;
  860. }
  861. noteElement.style.setProperty("left", (positionX - NOTE_INITIAL_WIDTH - 1) + "px");
  862. noteElement.style.setProperty("top", (positionY - NOTE_INITIAL_HEIGHT - 1) + "px");
  863. resizeElement.className = "note-resize";
  864. resizeElement.ondragstart = event => event.preventDefault();
  865. removeNoteElement.className = "note-remove";
  866. removeNoteElement.src = BUTTON_CLOSE_URL;
  867. removeNoteElement.ondragstart = event => event.preventDefault();
  868. anchorIconElement.className = "note-anchor";
  869. anchorIconElement.src = BUTTON_ANCHOR_URL;
  870. anchorIconElement.ondragstart = event => event.preventDefault();
  871. containerElement.dataset.noteId = noteId;
  872. addNoteRef(document.documentElement, noteId);
  873. attachNoteListeners(containerElement, true);
  874. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  875. noteElement.classList.add(NOTE_SELECTED_CLASS);
  876. selectedNote = noteElement;
  877. }
  878. function attachNoteListeners(containerElement, editable = false) {
  879. const SELECT_PX_THRESHOLD = 4;
  880. const COLLAPSING_NOTE_DELAY = 750;
  881. const noteShadow = containerElement.shadowRoot;
  882. const noteElement = noteShadow.childNodes[1];
  883. const headerElement = noteShadow.querySelector("header");
  884. const mainElement = noteShadow.querySelector("textarea");
  885. const noteId = containerElement.dataset.noteId;
  886. const resizeElement = noteShadow.querySelector(".note-resize");
  887. const anchorIconElement = noteShadow.querySelector(".note-anchor");
  888. const removeNoteElement = noteShadow.querySelector(".note-remove");
  889. mainElement.readOnly = !editable;
  890. if (!editable) {
  891. anchorIconElement.style.setProperty("display", "none", "important");
  892. } else {
  893. anchorIconElement.style.removeProperty("display");
  894. }
  895. headerElement.ontouchstart = headerElement.onmousedown = event => {
  896. if (event.target == headerElement) {
  897. collapseNoteTimeout = setTimeout(() => noteElement.classList.toggle("note-collapsed"), COLLAPSING_NOTE_DELAY);
  898. event.preventDefault();
  899. const position = getPosition(event);
  900. const clientX = position.clientX;
  901. const clientY = position.clientY;
  902. const boundingRect = noteElement.getBoundingClientRect();
  903. const deltaX = clientX - boundingRect.left;
  904. const deltaY = clientY - boundingRect.top;
  905. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  906. document.documentElement.style.setProperty("user-select", "none", "important");
  907. anchorElement = getAnchorElement(containerElement);
  908. displayMaskNote();
  909. selectNote(noteElement);
  910. moveNote(event, deltaX, deltaY);
  911. movingNoteMode = { event, deltaX, deltaY };
  912. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  913. clearTimeout(collapseNoteTimeout);
  914. if (!movingNoteMode) {
  915. movingNoteMode = { deltaX, deltaY };
  916. }
  917. movingNoteMode.event = event;
  918. moveNote(event, deltaX, deltaY);
  919. };
  920. }
  921. };
  922. resizeElement.ontouchstart = resizeElement.onmousedown = event => {
  923. event.preventDefault();
  924. resizingNoteMode = true;
  925. selectNote(noteElement);
  926. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  927. document.documentElement.style.setProperty("user-select", "none", "important");
  928. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  929. event.preventDefault();
  930. const { clientX, clientY } = getPosition(event);
  931. const boundingRectNote = noteElement.getBoundingClientRect();
  932. noteElement.style.width = clientX - boundingRectNote.left + "px";
  933. noteElement.style.height = clientY - boundingRectNote.top + "px";
  934. };
  935. };
  936. anchorIconElement.ontouchend = anchorIconElement.onclick = event => {
  937. event.preventDefault();
  938. noteElement.classList.toggle(NOTE_ANCHORED_CLASS);
  939. if (!noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  940. deleteNoteRef(containerElement, noteId);
  941. addNoteRef(document.documentElement, noteId);
  942. }
  943. };
  944. removeNoteElement.ontouchend = removeNoteElement.onclick = event => {
  945. event.preventDefault();
  946. deleteNoteRef(containerElement, noteId);
  947. containerElement.remove();
  948. };
  949. noteElement.onmousedown = () => {
  950. selectNote(noteElement);
  951. };
  952. function moveNote(event, deltaX, deltaY) {
  953. event.preventDefault();
  954. const { clientX, clientY } = getPosition(event);
  955. noteElement.classList.add(NOTE_MOVING_CLASS);
  956. if (editable) {
  957. if (noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  958. deleteNoteRef(containerElement, noteId);
  959. anchorElement = getTarget(clientX, clientY) || document.documentElement;
  960. addNoteRef(anchorElement, noteId);
  961. } else {
  962. anchorElement = document.documentElement;
  963. }
  964. }
  965. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  966. noteElement.style.setProperty("left", (clientX - deltaX) + "px");
  967. noteElement.style.setProperty("top", (clientY - deltaY) + "px");
  968. noteElement.style.setProperty("position", "fixed");
  969. displayMaskNote();
  970. }
  971. function displayMaskNote() {
  972. if (anchorElement == document.documentElement || anchorElement == document.documentElement) {
  973. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  974. } else {
  975. const boundingRectAnchor = anchorElement.getBoundingClientRect();
  976. maskNoteElement.classList.add(NOTE_MASK_MOVING_CLASS);
  977. maskNoteElement.style.setProperty("top", boundingRectAnchor.y + "px");
  978. maskNoteElement.style.setProperty("left", boundingRectAnchor.x + "px");
  979. maskNoteElement.style.setProperty("width", boundingRectAnchor.width + "px");
  980. maskNoteElement.style.setProperty("height", boundingRectAnchor.height + "px");
  981. }
  982. }
  983. function selectNote(noteElement) {
  984. if (selectedNote) {
  985. selectedNote.classList.remove(NOTE_SELECTED_CLASS);
  986. }
  987. noteElement.classList.add(NOTE_SELECTED_CLASS);
  988. selectedNote = noteElement;
  989. }
  990. function getTarget(clientX, clientY) {
  991. const targets = Array.from(document.elementsFromPoint(clientX, clientY)).filter(element => element.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS));
  992. if (!targets.includes(document.documentElement)) {
  993. targets.push(document.documentElement);
  994. }
  995. let newTarget, target = targets[0], boundingRect = target.getBoundingClientRect();
  996. newTarget = determineTargetElement("floor", target, clientX - boundingRect.left, getMatchedParents(target, "left"));
  997. if (newTarget == target) {
  998. newTarget = determineTargetElement("ceil", target, boundingRect.left + boundingRect.width - clientX, getMatchedParents(target, "right"));
  999. }
  1000. if (newTarget == target) {
  1001. newTarget = determineTargetElement("floor", target, clientY - boundingRect.top, getMatchedParents(target, "top"));
  1002. }
  1003. if (newTarget == target) {
  1004. newTarget = determineTargetElement("ceil", target, boundingRect.top + boundingRect.height - clientY, getMatchedParents(target, "bottom"));
  1005. }
  1006. target = newTarget;
  1007. while (boundingRect = target && target.getBoundingClientRect(), boundingRect && boundingRect.width <= SELECT_PX_THRESHOLD && boundingRect.height <= SELECT_PX_THRESHOLD) {
  1008. target = target.parentElement;
  1009. }
  1010. return target;
  1011. }
  1012. function getMatchedParents(target, property) {
  1013. let element = target, matchedParent, parents = [];
  1014. do {
  1015. const boundingRect = element.getBoundingClientRect();
  1016. if (element.parentElement && !element.parentElement.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS)) {
  1017. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  1018. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  1019. if (matchedParent) {
  1020. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  1021. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  1022. parents.push(element.parentElement);
  1023. }
  1024. element = element.parentElement;
  1025. }
  1026. } else {
  1027. matchedParent = false;
  1028. }
  1029. } while (matchedParent && element);
  1030. return parents;
  1031. }
  1032. function determineTargetElement(roundingMethod, target, widthDistance, parents) {
  1033. if (Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  1034. target = parents[parents.length - Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) - 1];
  1035. }
  1036. return target;
  1037. }
  1038. }
  1039. function onMouseUp(event) {
  1040. if (highlightSelectionMode) {
  1041. highlightSelection();
  1042. }
  1043. if (removeHighlightMode) {
  1044. let element = event.target, done;
  1045. while (element && !done) {
  1046. if (element.classList.contains(HIGHLIGHT_CLASS)) {
  1047. document.querySelectorAll("." + HIGHLIGHT_CLASS + "[data-singlefile-highlight-id=" + JSON.stringify(element.dataset.singlefileHighlightId) + "]").forEach(highlightedElement => {
  1048. resetHighlightedElement(highlightedElement);
  1049. });
  1050. done = true;
  1051. }
  1052. element = element.parentElement;
  1053. }
  1054. }
  1055. if (resizingNoteMode) {
  1056. resizingNoteMode = false;
  1057. document.documentElement.style.removeProperty("user-select");
  1058. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1059. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1060. }
  1061. if (movingNoteMode) {
  1062. anchorNote(movingNoteMode.event || event, selectedNote, movingNoteMode.deltaX, movingNoteMode.deltaY);
  1063. movingNoteMode = null;
  1064. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1065. }
  1066. if (collapseNoteTimeout) {
  1067. clearTimeout(collapseNoteTimeout);
  1068. collapseNoteTimeout = null;
  1069. }
  1070. if (cuttingMode) {
  1071. let element = event.target;
  1072. if (document.documentElement != element && element.tagName.toLowerCase() != NOTE_TAGNAME) {
  1073. element.classList.add(REMOVED_CONTENT_CLASS);
  1074. removedElements.push(element);
  1075. }
  1076. }
  1077. }
  1078. function anchorNote(event, noteElement, deltaX, deltaY) {
  1079. event.preventDefault();
  1080. const { clientX, clientY } = getPosition(event);
  1081. document.documentElement.style.removeProperty("user-select");
  1082. noteElement.classList.remove(NOTE_MOVING_CLASS);
  1083. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  1084. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1085. const headerElement = noteElement.querySelector("header");
  1086. headerElement.ontouchmove = document.documentElement.onmousemove = null;
  1087. let currentElement = anchorElement;
  1088. let positionedElement;
  1089. while (currentElement.parentElement && !positionedElement) {
  1090. if (!FORBIDDEN_TAG_NAMES.includes(currentElement.tagName.toLowerCase())) {
  1091. const currentElementStyle = getComputedStyle(currentElement);
  1092. if (currentElementStyle.position != "static") {
  1093. positionedElement = currentElement;
  1094. }
  1095. }
  1096. currentElement = currentElement.parentElement;
  1097. }
  1098. if (!positionedElement) {
  1099. positionedElement = document.documentElement;
  1100. }
  1101. const containerElement = noteElement.getRootNode().host;
  1102. if (positionedElement == document.documentElement) {
  1103. const firstMaskElement = document.querySelector("." + MASK_CLASS);
  1104. document.documentElement.insertBefore(containerElement, firstMaskElement);
  1105. } else {
  1106. positionedElement.appendChild(containerElement);
  1107. }
  1108. const boundingRectPositionedElement = positionedElement.getBoundingClientRect();
  1109. const stylePositionedElement = window.getComputedStyle(positionedElement);
  1110. const borderX = parseInt(stylePositionedElement.getPropertyValue("border-left-width"));
  1111. const borderY = parseInt(stylePositionedElement.getPropertyValue("border-top-width"));
  1112. noteElement.style.setProperty("position", "absolute");
  1113. noteElement.style.setProperty("left", (clientX - boundingRectPositionedElement.x - deltaX - borderX) + "px");
  1114. noteElement.style.setProperty("top", (clientY - boundingRectPositionedElement.y - deltaY - borderY) + "px");
  1115. }
  1116. function getPosition(event) {
  1117. if (event.touches && event.touches.length) {
  1118. const touch = event.touches[0];
  1119. return touch;
  1120. } else {
  1121. return event;
  1122. }
  1123. }
  1124. function highlightSelection() {
  1125. let highlightId = 0;
  1126. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(highlightedElement => highlightId = Math.max(highlightId, highlightedElement.dataset.singlefileHighlightId));
  1127. highlightId++;
  1128. const selection = window.getSelection();
  1129. const highlightedNodes = new Set();
  1130. for (let indexRange = 0; indexRange < selection.rangeCount; indexRange++) {
  1131. const range = selection.getRangeAt(indexRange);
  1132. if (!range.collapsed) {
  1133. if (range.commonAncestorContainer.nodeType == range.commonAncestorContainer.TEXT_NODE) {
  1134. let contentText = range.startContainer.splitText(range.startOffset);
  1135. contentText = contentText.splitText(range.endOffset);
  1136. addHighLightedNode(contentText.previousSibling);
  1137. } else {
  1138. const treeWalker = document.createTreeWalker(range.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
  1139. let highlightNodes;
  1140. while (treeWalker.nextNode()) {
  1141. if (highlightNodes && !treeWalker.currentNode.contains(range.endContainer)) {
  1142. addHighLightedNode(treeWalker.currentNode);
  1143. }
  1144. if (treeWalker.currentNode == range.startContainer) {
  1145. if (range.startContainer.nodeType == range.startContainer.TEXT_NODE) {
  1146. const contentText = range.startContainer.splitText(range.startOffset);
  1147. treeWalker.nextNode();
  1148. addHighLightedNode(contentText);
  1149. } else {
  1150. addHighLightedNode(range.startContainer.childNodes[range.startOffset]);
  1151. }
  1152. highlightNodes = true;
  1153. }
  1154. if (treeWalker.currentNode == range.endContainer) {
  1155. if (range.endContainer.nodeType == range.endContainer.TEXT_NODE) {
  1156. const contentText = range.endContainer.splitText(range.endOffset);
  1157. treeWalker.nextNode();
  1158. addHighLightedNode(contentText.previousSibling);
  1159. } else {
  1160. addHighLightedNode(range.endContainer.childNodes[range.endOffset]);
  1161. }
  1162. highlightNodes = false;
  1163. }
  1164. }
  1165. range.collapse();
  1166. }
  1167. }
  1168. }
  1169. highlightedNodes.forEach(node => highlightNode(node));
  1170. function addHighLightedNode(node) {
  1171. if (node && node.textContent.trim()) {
  1172. if (node.nodeType == node.TEXT_NODE && node.parentElement.childNodes.length == 1 && node.parentElement.classList.contains(HIGHLIGHT_CLASS)) {
  1173. highlightedNodes.add(node.parentElement);
  1174. } else {
  1175. highlightedNodes.add(node);
  1176. }
  1177. }
  1178. }
  1179. function highlightNode(node) {
  1180. if (node.nodeType == node.ELEMENT_NODE) {
  1181. resetHighlightedElement(node);
  1182. node.classList.add(HIGHLIGHT_CLASS);
  1183. node.classList.add(highlightColor);
  1184. node.dataset.singlefileHighlightId = highlightId;
  1185. } else if (node.parentElement) {
  1186. highlightTextNode(node);
  1187. }
  1188. }
  1189. function highlightTextNode(node) {
  1190. const spanElement = document.createElement("span");
  1191. spanElement.classList.add(HIGHLIGHT_CLASS);
  1192. spanElement.classList.add(highlightColor);
  1193. spanElement.textContent = node.textContent;
  1194. spanElement.dataset.singlefileHighlightId = highlightId;
  1195. node.parentNode.replaceChild(spanElement, node);
  1196. return spanElement;
  1197. }
  1198. }
  1199. function highlightElementToCut(event) {
  1200. if (event.type != "mouseover" && event.type != "mouseout") return;
  1201. var target = event.target;
  1202. if ("classList" in target) {
  1203. target.classList.toggle("single-file-hover");
  1204. }
  1205. event.stopPropagation();
  1206. }
  1207. function reflowNotes() {
  1208. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1209. const noteElement = containerElement.shadowRoot.querySelector("." + NOTE_CLASS);
  1210. const noteBoundingRect = noteElement.getBoundingClientRect();
  1211. const anchorElement = getAnchorElement(containerElement);
  1212. const anchorBoundingRect = anchorElement.getBoundingClientRect();
  1213. const maxX = anchorBoundingRect.x + Math.max(0, anchorBoundingRect.width - noteBoundingRect.width);
  1214. const minX = anchorBoundingRect.x;
  1215. const maxY = anchorBoundingRect.y + Math.max(0, anchorBoundingRect.height - NOTE_HEADER_HEIGHT);
  1216. const minY = anchorBoundingRect.y;
  1217. let left = parseInt(noteElement.style.getPropertyValue("left"));
  1218. let top = parseInt(noteElement.style.getPropertyValue("top"));
  1219. if (noteBoundingRect.x > maxX) {
  1220. left -= noteBoundingRect.x - maxX;
  1221. }
  1222. if (noteBoundingRect.x < minX) {
  1223. left += minX - noteBoundingRect.x;
  1224. }
  1225. if (noteBoundingRect.y > maxY) {
  1226. top -= noteBoundingRect.y - maxY;
  1227. }
  1228. if (noteBoundingRect.y < minY) {
  1229. top += minY - noteBoundingRect.y;
  1230. }
  1231. noteElement.style.setProperty("position", "absolute");
  1232. noteElement.style.setProperty("left", left + "px");
  1233. noteElement.style.setProperty("top", top + "px");
  1234. });
  1235. }
  1236. function resetHighlightedElement(element) {
  1237. element.classList.remove(HIGHLIGHT_CLASS);
  1238. element.classList.remove("single-file-highlight-yellow");
  1239. element.classList.remove("single-file-highlight-pink");
  1240. element.classList.remove("single-file-highlight-blue");
  1241. element.classList.remove("single-file-highlight-green");
  1242. delete element.dataset.singlefileHighlightId;
  1243. }
  1244. function serializeShadowRoots(node) {
  1245. node.querySelectorAll("*").forEach(element => {
  1246. if (element.shadowRoot) {
  1247. serializeShadowRoots(element.shadowRoot);
  1248. const templateElement = document.createElement("template");
  1249. templateElement.setAttribute(SHADOW_MODE_ATTRIBUTE_NAME, "open");
  1250. templateElement.appendChild(element.shadowRoot);
  1251. element.appendChild(templateElement);
  1252. }
  1253. });
  1254. }
  1255. function deserializeShadowRoots(node) {
  1256. node.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(element => {
  1257. if (element.parentElement) {
  1258. let shadowRoot = element.parentElement.shadowRoot;
  1259. if (shadowRoot) {
  1260. Array.from(element.childNodes).forEach(node => shadowRoot.appendChild(node));
  1261. element.remove();
  1262. } else {
  1263. shadowRoot = element.parentElement.attachShadow({ mode: "open" });
  1264. const contentDocument = (new DOMParser()).parseFromString(element.innerHTML, "text/html");
  1265. Array.from(contentDocument.head.childNodes).forEach(node => shadowRoot.appendChild(node));
  1266. Array.from(contentDocument.body.childNodes).forEach(node => shadowRoot.appendChild(node));
  1267. }
  1268. deserializeShadowRoots(shadowRoot);
  1269. }
  1270. });
  1271. }
  1272. function getMaskElement(className, containerClassName) {
  1273. let maskElement = document.documentElement.querySelector("." + className);
  1274. if (!maskElement) {
  1275. maskElement = document.createElement("div");
  1276. const maskContainerElement = document.createElement("div");
  1277. if (containerClassName) {
  1278. maskContainerElement.classList.add(containerClassName);
  1279. }
  1280. maskContainerElement.classList.add(MASK_CLASS);
  1281. const firstNote = document.querySelector(NOTE_TAGNAME);
  1282. if (firstNote && firstNote.parentElement == document.documentElement) {
  1283. document.documentElement.insertBefore(maskContainerElement, firstNote);
  1284. } else {
  1285. document.documentElement.appendChild(maskContainerElement);
  1286. }
  1287. maskElement.classList.add(className);
  1288. const maskShadow = maskContainerElement.attachShadow({ mode: "open" });
  1289. maskShadow.appendChild(getStyleElement(MASK_WEB_STYLESHEET));
  1290. maskShadow.appendChild(maskElement);
  1291. return maskElement;
  1292. }
  1293. }
  1294. function getEmbedScript() {
  1295. return minifyText(`(() => {
  1296. document.currentScript.remove();
  1297. const processNode = node => {
  1298. node.querySelectorAll("template[${SHADOW_MODE_ATTRIBUTE_NAME}]").forEach(element=>{
  1299. if (!element.parentElement.shadowRoot) {
  1300. const shadowRoot = element.parentElement.attachShadow({mode:element.getAttribute("${SHADOW_MODE_ATTRIBUTE_NAME}"),delegatesFocus:Boolean(element.getAttribute("${SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME}"))});
  1301. shadowRoot.innerHTML = element.innerHTML;
  1302. element.remove();
  1303. processNode(shadowRoot);
  1304. }
  1305. })
  1306. };
  1307. const FORBIDDEN_TAG_NAMES = ${JSON.stringify(FORBIDDEN_TAG_NAMES)};
  1308. const NOTE_TAGNAME = ${JSON.stringify(NOTE_TAGNAME)};
  1309. const NOTE_CLASS = ${JSON.stringify(NOTE_CLASS)};
  1310. const NOTE_ANCHORED_CLASS = ${JSON.stringify(NOTE_ANCHORED_CLASS)};
  1311. const NOTE_SELECTED_CLASS = ${JSON.stringify(NOTE_SELECTED_CLASS)};
  1312. const NOTE_MOVING_CLASS = ${JSON.stringify(NOTE_MOVING_CLASS)};
  1313. const NOTE_MASK_MOVING_CLASS = ${JSON.stringify(NOTE_MASK_MOVING_CLASS)};
  1314. const MASK_CLASS = ${JSON.stringify(MASK_CLASS)};
  1315. const HIGHLIGHT_CLASS = ${JSON.stringify(HIGHLIGHT_CLASS)};
  1316. const NOTES_WEB_STYLESHEET = ${JSON.stringify(NOTES_WEB_STYLESHEET)};
  1317. const MASK_WEB_STYLESHEET = ${JSON.stringify(MASK_WEB_STYLESHEET)};
  1318. const NOTE_HEADER_HEIGHT = ${JSON.stringify(NOTE_HEADER_HEIGHT)};
  1319. const PAGE_MASK_ACTIVE_CLASS = ${JSON.stringify(PAGE_MASK_ACTIVE_CLASS)};
  1320. const REMOVED_CONTENT_CLASS = ${JSON.stringify(REMOVED_CONTENT_CLASS)};
  1321. const reflowNotes = ${minifyText(reflowNotes.toString())};
  1322. const addNoteRef = ${minifyText(addNoteRef.toString())};
  1323. const deleteNoteRef = ${minifyText(deleteNoteRef.toString())};
  1324. const getNoteRefs = ${minifyText(getNoteRefs.toString())};
  1325. const setNoteRefs = ${minifyText(setNoteRefs.toString())};
  1326. const getAnchorElement = ${minifyText(getAnchorElement.toString())};
  1327. const getMaskElement = ${minifyText(getMaskElement.toString())};
  1328. const getStyleElement = ${minifyText(getStyleElement.toString())};
  1329. const attachNoteListeners = ${minifyText(attachNoteListeners.toString())};
  1330. const anchorNote = ${minifyText(anchorNote.toString())};
  1331. const getPosition = ${minifyText(getPosition.toString())};
  1332. const onMouseUp = ${minifyText(onMouseUp.toString())};
  1333. const maskNoteElement = getMaskElement(${JSON.stringify(NOTE_MASK_CLASS)});
  1334. const maskPageElement = getMaskElement(${JSON.stringify(PAGE_MASK_CLASS)}, ${JSON.stringify(PAGE_MASK_CONTAINER_CLASS)});
  1335. let selectedNote, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, collapseNoteTimeout, cuttingMode;
  1336. window.onresize = reflowNotes;
  1337. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  1338. window.addEventListener("DOMContentLoaded", () => {
  1339. processNode(document);
  1340. reflowNotes();
  1341. document.querySelectorAll(${JSON.stringify(NOTE_TAGNAME)}).forEach(noteElement => attachNoteListeners(noteElement));
  1342. });
  1343. })()`);
  1344. }
  1345. function getStyleElement(stylesheet) {
  1346. const linkElement = document.createElement("style");
  1347. linkElement.textContent = stylesheet;
  1348. return linkElement;
  1349. }
  1350. function getAnchorElement(containerElement) {
  1351. return document.querySelector("[data-single-file-note-refs^=" + JSON.stringify(containerElement.dataset.noteId) + "], [data-single-file-note-refs$=" + JSON.stringify(containerElement.dataset.noteId) + "], [data-single-file-note-refs*=" + JSON.stringify("," + containerElement.dataset.noteId + ",") + "]");
  1352. }
  1353. function addNoteRef(anchorElement, noteId) {
  1354. const noteRefs = getNoteRefs(anchorElement);
  1355. noteRefs.push(noteId);
  1356. setNoteRefs(anchorElement, noteRefs);
  1357. }
  1358. function deleteNoteRef(containerElement, noteId) {
  1359. const anchorElement = getAnchorElement(containerElement);
  1360. const noteRefs = getNoteRefs(anchorElement).filter(noteRefs => noteRefs != noteId);
  1361. if (noteRefs.length) {
  1362. setNoteRefs(anchorElement, noteRefs);
  1363. } else {
  1364. delete anchorElement.dataset.singleFileNoteRefs;
  1365. }
  1366. }
  1367. function getNoteRefs(anchorElement) {
  1368. return JSON.parse("[" + (anchorElement.dataset.singleFileNoteRefs || "") + "]");
  1369. }
  1370. function setNoteRefs(anchorElement, noteRefs) {
  1371. anchorElement.dataset.singleFileNoteRefs = noteRefs.toString();
  1372. }
  1373. function minifyText(text) {
  1374. return text.replace(/[\n\t\s]+/g, " ");
  1375. }
  1376. })();