content-ui-editor-web.js 58 KB

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