content-ui-editor-web.js 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  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 globalThis, window, document, fetch, DOMParser, getComputedStyle, setTimeout, clearTimeout, NodeFilter, Readability, isProbablyReaderable, matchMedia, TextDecoder, Node */
  24. (globalThis => {
  25. const singlefile = globalThis.singlefile;
  26. const FORBIDDEN_TAG_NAMES = ["a", "area", "audio", "base", "br", "col", "command", "embed", "hr", "img", "iframe", "input", "keygen", "link", "meta", "param", "source", "track", "video", "wbr"];
  27. const BUTTON_ANCHOR_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAgMAAAAOFJJnAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TtaIVETuIOASsThZERRylikWwUNoKrTqYXPohNGlIUlwcBdeCgx+LVQcXZ10dXAVB8APEydFJ0UVK/F9SaBHjwXE/3t173L0DhFqJqWbbOKBqlpGMRcVMdkUMvKIbfQCG0SExU4+nFtLwHF/38PH1LsKzvM/9OXqUnMkAn0g8y3TDIl4nnt60dM77xCFWlBTic+Ixgy5I/Mh12eU3zgWHBZ4ZMtLJOeIQsVhoYbmFWdFQiaeIw4qqUb6QcVnhvMVZLVVY4578hcGctpziOs0hxLCIOBIQIaOCDZRgIUKrRoqJJO1HPfyDjj9BLplcG2DkmEcZKiTHD/4Hv7s185MTblIwCrS/2PbHCBDYBepV2/4+tu36CeB/Bq60pr9cA2Y+Sa82tfAR0LsNXFw3NXkPuNwBBp50yZAcyU9TyOeB9zP6pizQfwt0rbq9NfZx+gCkqaulG+DgEBgtUPaax7s7W3v790yjvx825XKP2aKCdAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+QLEQA4M3Y7LzIAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAACVBMVEUAAAAAAACKioqjwG1pAAAAAXRSTlMAQObYZgAAAAFiS0dEAmYLfGQAAABkSURBVBjThc47CsNADIThWfD0bnSfbdIroP/+V0mhsN5gTNToK0YPaSvnF9B9wGykG54j/2GF1/hauE4E1AOuNxrBdA5KUXIqdiCnqC1zIZ2mFJQzKJ3wesOhcwDM4+fo7cOuD9C4HTQ9HAAQAAAAAElFTkSuQmCC";
  28. const BUTTON_CLOSE_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAgMAAAAOFJJnAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TtaIVETuIOASsThZERRylikWwUNoKrTqYXPohNGlIUlwcBdeCgx+LVQcXZ10dXAVB8APEydFJ0UVK/F9SaBHjwXE/3t173L0DhFqJqWbbOKBqlpGMRcVMdkUMvKIbfQCG0SExU4+nFtLwHF/38PH1LsKzvM/9OXqUnMkAn0g8y3TDIl4nnt60dM77xCFWlBTic+Ixgy5I/Mh12eU3zgWHBZ4ZMtLJOeIQsVhoYbmFWdFQiaeIw4qqUb6QcVnhvMVZLVVY4578hcGctpziOs0hxLCIOBIQIaOCDZRgIUKrRoqJJO1HPfyDjj9BLplcG2DkmEcZKiTHD/4Hv7s185MTblIwCrS/2PbHCBDYBepV2/4+tu36CeB/Bq60pr9cA2Y+Sa82tfAR0LsNXFw3NXkPuNwBBp50yZAcyU9TyOeB9zP6pizQfwt0rbq9NfZx+gCkqaulG+DgEBgtUPaax7s7W3v790yjvx825XKP2aKCdAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+QLEQA6Na1u6IUAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAACVBMVEUAAAAAAACKioqjwG1pAAAAAXRSTlMAQObYZgAAAAFiS0dEAmYLfGQAAABlSURBVBhXTc/BEUQhCAPQ58ES6McSPED/rfwDI7vOMCoJIeGd6CvFgZXiwk47Ia5VUKdrVXcb39kfqxqmTg+I2xJ2tqhVTaGaQjTl7/GgIc/4CL4Vs3RsjLFndcxPnAn4iww8A3yQjRZjti1t6AAAAABJRU5ErkJggg==";
  29. const SHADOW_MODE_ATTRIBUTE_NAME = "shadowmode";
  30. const SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME = "delegatesfocus";
  31. const SCRIPT_TEMPLATE_SHADOW_ROOT = "data-template-shadow-root";
  32. const NOTE_TAGNAME = "single-file-note";
  33. const NOTE_CLASS = "note";
  34. const NOTE_MASK_CLASS = "note-mask";
  35. const NOTE_HIDDEN_CLASS = "note-hidden";
  36. const NOTE_ANCHORED_CLASS = "note-anchored";
  37. const NOTE_SELECTED_CLASS = "note-selected";
  38. const NOTE_MOVING_CLASS = "note-moving";
  39. const NOTE_MASK_MOVING_CLASS = "note-mask-moving";
  40. const PAGE_MASK_CLASS = "page-mask";
  41. const MASK_CLASS = "single-file-mask";
  42. const PAGE_MASK_CONTAINER_CLASS = "single-file-page-mask";
  43. const HIGHLIGHT_CLASS = "single-file-highlight";
  44. const REMOVED_CONTENT_CLASS = "single-file-removed";
  45. const HIGHLIGHT_HIDDEN_CLASS = "single-file-highlight-hidden";
  46. const PAGE_MASK_ACTIVE_CLASS = "page-mask-active";
  47. const CUT_HOVER_CLASS = "single-file-cut-hover";
  48. const CUT_OUTER_HOVER_CLASS = "single-file-cut-outer-hover";
  49. const CUT_SELECTED_CLASS = "single-file-cut-selected";
  50. const CUT_OUTER_SELECTED_CLASS = "single-file-cut-outer-selected";
  51. const NOTE_INITIAL_POSITION_X = 20;
  52. const NOTE_INITIAL_POSITION_Y = 20;
  53. const NOTE_INITIAL_WIDTH = 150;
  54. const NOTE_INITIAL_HEIGHT = 150;
  55. const NOTE_HEADER_HEIGHT = 25;
  56. const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-single-file-disabled-noscript";
  57. const COMMENT_HEADER = "Page saved with SingleFile";
  58. const COMMENT_HEADER_LEGACY = "Archive processed by SingleFile";
  59. const STYLE_FORMATTED_PAGE = `
  60. /* This Source Code Form is subject to the terms of the Mozilla Public
  61. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  62. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  63. /* Avoid adding ID selector rules in this style sheet, since they could
  64. * inadvertently match elements in the article content. */
  65. :root {
  66. --body-padding: 64px;
  67. --popup-border: rgba(0, 0, 0, 0.12);
  68. --opaque-popup-border: #e0e0e0;
  69. --popup-shadow: rgba(49, 49, 49, 0.3);
  70. --grey-90-a10: rgba(12, 12, 13, 0.1);
  71. --grey-90-a20: rgba(12, 12, 13, 0.2);
  72. --grey-90-a30: rgba(12, 12, 13, 0.3);
  73. --grey-90-a80: rgba(12, 12, 13, 0.8);
  74. --grey-30: #d7d7db;
  75. --blue-40: #45a1ff;
  76. --blue-40-a30: rgba(69, 161, 255, 0.3);
  77. --blue-60: #0060df;
  78. --active-color: #0B83FF;
  79. --font-size: 12;
  80. --content-width: 30em;
  81. --line-height: 1.6em;
  82. --tooltip-background: var(--grey-90-a80);
  83. --tooltip-foreground: white;
  84. --toolbar-button-hover: var(--grey-90-a10);
  85. --toolbar-button-active: var(--grey-90-a20);
  86. }
  87. body {
  88. --main-background: #fff;
  89. --main-foreground: #333;
  90. --toolbar-border: var(--grey-90-a20);
  91. --toolbar-box-shadow: var(--grey-90-a10);
  92. --popup-button-hover: hsla(0,0%,70%,.4);
  93. --popup-button-active: hsla(240,5%,5%,.15);
  94. --popup-bgcolor: white;
  95. --popup-button: #edecf0;
  96. --selected-background: var(--blue-40-a30);
  97. --selected-border: var(--blue-40);
  98. --popup-line: var(--grey-30);
  99. --font-value-border: var(--grey-30);
  100. --font-color: #000000;
  101. --icon-fill: #3b3b3c;
  102. --icon-disabled-fill: #8080807F;
  103. --link-foreground: var(--blue-60);
  104. --link-selected-foreground: #333;
  105. --visited-link-foreground: #b5007f;
  106. /* light colours */
  107. }
  108. body.sepia {
  109. --main-background: #f4ecd8;
  110. --main-foreground: #5b4636;
  111. --toolbar-border: #5b4636;
  112. }
  113. body.dark {
  114. --main-background: rgb(28, 27, 34);
  115. --main-foreground: #eee;
  116. --toolbar-border: #4a4a4b;
  117. --toolbar-box-shadow: black;
  118. --toolbar-button-hover: var(--grey-90-a30);
  119. --toolbar-button-active: var(--grey-90-a80);
  120. --popup-button-active: hsla(0,0%,70%,.6);
  121. --popup-bgcolor: rgb(66,65,77);
  122. --popup-button: #5c5c61;
  123. --popup-line: rgb(82, 82, 94);
  124. --selected-background: #3E6D9A;
  125. --link-foreground: #45a1ff;
  126. --visited-link-foreground: #e675fd;
  127. --link-selected-foreground: #fff;
  128. --opaque-popup-border: #434146;
  129. --font-value-border: #656468;
  130. --font-color: #fff;
  131. --icon-fill: #fff;
  132. --icon-disabled-fill: #ffffff66;
  133. --tooltip-background: black;
  134. --tooltip-foreground: white;
  135. /* dark colours */
  136. }
  137. body {
  138. margin: 0;
  139. padding: var(--body-padding);
  140. background-color: var(--main-background);
  141. color: var(--main-foreground);
  142. }
  143. body.loaded {
  144. transition: color 0.4s, background-color 0.4s;
  145. }
  146. body.dark *::-moz-selection {
  147. background-color: var(--selected-background);
  148. }
  149. a::-moz-selection {
  150. color: var(--link-selected-foreground);
  151. }
  152. body.sans-serif,
  153. body.sans-serif .remove-button {
  154. font-family: Helvetica, Arial, sans-serif;
  155. }
  156. body.serif,
  157. body.serif .remove-button {
  158. font-family: Georgia, "Times New Roman", serif;
  159. }
  160. .container {
  161. margin: 0 auto;
  162. font-size: var(--font-size);
  163. max-width: var(--content-width);
  164. line-height: var(--line-height);
  165. }
  166. /* Override some controls and content styles based on color scheme */
  167. body.light > .container > .header > .domain {
  168. border-bottom-color: #333333 !important;
  169. }
  170. body.sepia > .container > .header > .domain {
  171. border-bottom-color: #5b4636 !important;
  172. }
  173. body.dark > .container > .header > .domain {
  174. border-bottom-color: #eeeeee !important;
  175. }
  176. body.sepia > .container > .footer {
  177. background-color: #dedad4 !important;
  178. }
  179. body.light blockquote {
  180. border-inline-start: 2px solid #333333 !important;
  181. }
  182. body.sepia blockquote {
  183. border-inline-start: 2px solid #5b4636 !important;
  184. }
  185. body.dark blockquote {
  186. border-inline-start: 2px solid #eeeeee !important;
  187. }
  188. .light-button {
  189. color: #333333;
  190. background-color: #ffffff;
  191. }
  192. .dark-button {
  193. color: #eeeeee;
  194. background-color: #1c1b22;
  195. }
  196. .sepia-button {
  197. color: #5b4636;
  198. background-color: #f4ecd8;
  199. }
  200. /* Loading/error message */
  201. .reader-message {
  202. margin-top: 40px;
  203. display: none;
  204. text-align: center;
  205. width: 100%;
  206. font-size: 0.9em;
  207. }
  208. /* Detector element to see if we're at the top of the doc or not. */
  209. .top-anchor {
  210. position: absolute;
  211. top: 0;
  212. width: 0;
  213. height: 5px;
  214. pointer-events: none;
  215. }
  216. /* Header */
  217. .header {
  218. text-align: start;
  219. display: none;
  220. }
  221. .domain {
  222. font-size: 0.9em;
  223. line-height: 1.48em;
  224. padding-bottom: 4px;
  225. font-family: Helvetica, Arial, sans-serif;
  226. text-decoration: none;
  227. border-bottom: 1px solid;
  228. color: var(--link-foreground);
  229. }
  230. .header > h1 {
  231. font-size: 1.6em;
  232. line-height: 1.25em;
  233. width: 100%;
  234. margin: 30px 0;
  235. padding: 0;
  236. }
  237. .header > .credits {
  238. font-size: 0.9em;
  239. line-height: 1.48em;
  240. margin: 0 0 10px;
  241. padding: 0;
  242. font-style: italic;
  243. }
  244. .header > .meta-data {
  245. font-size: 0.65em;
  246. margin: 0 0 15px;
  247. }
  248. .reader-estimated-time {
  249. text-align: match-parent;
  250. }
  251. /*======= Controls toolbar =======*/
  252. .toolbar-container {
  253. position: sticky;
  254. z-index: 2;
  255. top: 32px;
  256. height: 0; /* take up no space, so body is at the top. */
  257. /* As a stick container, we're positioned relative to the body. Move us to
  258. * the edge of the viewport using margins, and take the width of
  259. * the body padding into account for calculating our width.
  260. */
  261. margin-inline-start: calc(-1 * var(--body-padding));
  262. width: max(var(--body-padding), calc((100% - var(--content-width)) / 2 + var(--body-padding)));
  263. font-size: var(--font-size); /* Needed to ensure em units match, is reset for .reader-controls */
  264. }
  265. .toolbar {
  266. padding-block: 16px;
  267. border: 1px solid var(--toolbar-border);
  268. border-radius: 6px;
  269. box-shadow: 0 2px 8px var(--toolbar-box-shadow);
  270. width: 32px; /* basic width, without padding/border */
  271. /* padding should be 16px, except if there's not enough space for that, in
  272. * which case use half the available space for padding (=25% on each side).
  273. * The 34px here is the width + borders. We use a variable because we need
  274. * to know this size for the margin calculation.
  275. */
  276. --inline-padding: min(16px, calc(25% - 0.25 * 34px));
  277. padding-inline: var(--inline-padding);
  278. /* Keep a maximum of 96px distance to the body, but center once the margin
  279. * gets too small. We need to set the start margin, however...
  280. * To center, we'd want 50% of the container, but we'd subtract half our
  281. * own width (16px) and half the border (1px) and the inline padding.
  282. * When the other margin would be 96px, we want 100% - 96px - the complete
  283. * width of the actual toolbar (34px + 2 * padding)
  284. */
  285. margin-inline-start: max(calc(50% - 17px - var(--inline-padding)), calc(100% - 96px - 34px - 2 * var(--inline-padding)));
  286. font-family: Helvetica, Arial, sans-serif;
  287. list-style: none;
  288. user-select: none;
  289. }
  290. @media (prefers-reduced-motion: no-preference) {
  291. .toolbar {
  292. transition-property: border-color, box-shadow;
  293. transition-duration: 250ms;
  294. }
  295. .toolbar .button {
  296. transition-property: opacity;
  297. transition-duration: 250ms;
  298. }
  299. .toolbar-container.scrolled .toolbar:not(:hover, :focus-within) {
  300. border-color: transparent;
  301. box-shadow: 0 2px 8px transparent;
  302. }
  303. .toolbar-container.scrolled .toolbar:not(:hover, :focus-within) .button {
  304. opacity: 0.6;
  305. }
  306. .toolbar-container.transition-location {
  307. transition-duration: 250ms;
  308. transition-property: width;
  309. }
  310. }
  311. .toolbar-container.overlaps .button {
  312. opacity: 0.1;
  313. }
  314. .dropdown-open .toolbar {
  315. border-color: transparent;
  316. box-shadow: 0 2px 8px transparent;
  317. }
  318. .reader-controls {
  319. /* We use ems above this node to get it to the right size. However,
  320. * the UI inside the toolbar should use a fixed, smaller size. */
  321. font-size: 11px;
  322. }
  323. .button {
  324. position: relative;
  325. width: 32px;
  326. height: 32px;
  327. padding: 0;
  328. border: none;
  329. border-radius: 4px;
  330. margin: 4px 0;
  331. background-color: transparent;
  332. background-size: 16px 16px;
  333. background-position: center;
  334. background-repeat: no-repeat;
  335. color: var(--font-color);
  336. }
  337. .button:hover,
  338. .button:-moz-focusring {
  339. background-color: var(--toolbar-button-hover);
  340. }
  341. .open .button,
  342. .button:active {
  343. background-color: var(--toolbar-button-active);
  344. color: var(--active-color);
  345. fill: var(--active-color);
  346. }
  347. .hover-label {
  348. position: absolute;
  349. top: 4px;
  350. inset-inline-start: 36px;
  351. line-height: 16px;
  352. white-space: pre; /* make sure we don't wrap */
  353. background-color: var(--tooltip-background);
  354. color: var(--tooltip-foreground);
  355. padding: 4px 8px;
  356. border-radius: 2px;
  357. visibility: hidden;
  358. pointer-events: none;
  359. /* Put above .dropdown .dropdown-popup, which has z-index: 1000. */
  360. z-index: 1001;
  361. }
  362. /* Show the hover tooltip on non-dropdown buttons. */
  363. .button:not(.dropdown-toggle):hover > .hover-label,
  364. .button:not(.dropdown-toggle):-moz-focusring > .hover-label,
  365. /* Show the hover tooltip for dropdown buttons unless its dropdown is open. */
  366. :not(.open) > li > .dropdown-toggle:hover > .hover-label,
  367. :not(.open) > li > .dropdown-toggle:-moz-focusring > .hover-label {
  368. visibility: visible;
  369. }
  370. button {
  371. -moz-context-properties: fill;
  372. color: var(--font-color);
  373. fill: var(--icon-fill);
  374. }
  375. button:disabled {
  376. fill: var(--icon-disabled-fill);
  377. }
  378. .toolbar button::-moz-focus-inner {
  379. border: 0;
  380. }
  381. .dropdown {
  382. text-align: center;
  383. list-style: none;
  384. margin: 0;
  385. padding: 0;
  386. position: relative;
  387. }
  388. .dropdown li {
  389. margin: 0;
  390. padding: 0;
  391. }
  392. /*======= Popup =======*/
  393. .dropdown .dropdown-popup {
  394. text-align: start;
  395. position: absolute;
  396. inset-inline-start: 40px;
  397. z-index: 1000;
  398. background-color: var(--popup-bgcolor);
  399. visibility: hidden;
  400. border-radius: 4px;
  401. border: 1px solid var(--popup-border);
  402. box-shadow: 0 0px 10px 0 var(--popup-shadow);
  403. border-bottom-width: 0;
  404. top: 0;
  405. }
  406. .open > .dropdown-popup {
  407. visibility: visible;
  408. }
  409. .dropdown-arrow {
  410. position: absolute;
  411. height: 24px;
  412. width: 16px;
  413. inset-inline-start: -16px;
  414. background-image: url("chrome://global/skin/reader/RM-Type-Controls-Arrow.svg");
  415. display: block;
  416. -moz-context-properties: fill, stroke;
  417. fill: var(--popup-bgcolor);
  418. stroke: var(--opaque-popup-border);
  419. pointer-events: none;
  420. }
  421. .dropdown-arrow:dir(rtl) {
  422. transform: scaleX(-1);
  423. }
  424. /* Align the style dropdown arrow (narrate does its own) */
  425. .style-dropdown .dropdown-arrow {
  426. top: 7px;
  427. }
  428. /*======= Font style popup =======*/
  429. .radio-button {
  430. /* We visually hide these, but we keep them around so they can be focused
  431. * and changed by interacting with them via the label, or the keyboard, or
  432. * assistive technology.
  433. */
  434. opacity: 0;
  435. pointer-events: none;
  436. position: absolute;
  437. }
  438. .radiorow,
  439. .buttonrow {
  440. display: flex;
  441. }
  442. .content-width-value,
  443. .font-size-value,
  444. .line-height-value {
  445. box-sizing: border-box;
  446. width: 36px;
  447. height: 15px;
  448. line-height: 15px;
  449. display: flex;
  450. justify-content: center;
  451. align-content: center;
  452. margin: auto;
  453. border-radius: 10px;
  454. border: 1px solid var(--font-value-border);
  455. background-color: var(--popup-button);
  456. }
  457. .buttonrow > button {
  458. border: 0;
  459. height: 60px;
  460. width: 90px;
  461. background-color: transparent;
  462. background-repeat: no-repeat;
  463. background-position: center;
  464. }
  465. .buttonrow > button:enabled:hover,
  466. .buttonrow > button:enabled:-moz-focusring {
  467. background-color: var(--popup-button-hover);
  468. }
  469. .buttonrow > button:enabled:active {
  470. background-color: var(--popup-button-active);
  471. }
  472. .radiorow:not(:last-child),
  473. .buttonrow:not(:last-child) {
  474. border-bottom: 1px solid var(--popup-line);
  475. }
  476. .radiorow > label {
  477. position: relative;
  478. box-sizing: border-box;
  479. border-radius: 2px;
  480. border: 2px solid var(--popup-border);
  481. }
  482. .radiorow > label[checked] {
  483. border-color: var(--selected-border);
  484. }
  485. /* For the hover style, we draw a line under the item by means of a
  486. * pseudo-element. Because these items are variable height, and
  487. * because their contents are variable height, position it absolutely,
  488. * and give it a width of 100% (the content width) + 4px for the 2 * 2px
  489. * border width.
  490. */
  491. .radiorow > input[type=radio]:-moz-focusring + label::after,
  492. .radiorow > label:hover::after {
  493. content: "";
  494. display: block;
  495. border-bottom: 2px solid var(--blue-40);
  496. width: calc(100% + 4px);
  497. position: absolute;
  498. /* to skip the 2 * 2px border + 2px spacing. */
  499. bottom: -6px;
  500. /* Match the start of the 2px border of the element: */
  501. inset-inline-start: -2px;
  502. }
  503. .color-scheme-buttons > label {
  504. height: 34px;
  505. width: 70px;
  506. font-size: 12px;
  507. /* Center the labels horizontally as well as vertically */
  508. display: inline-flex;
  509. align-items: center;
  510. justify-content: center;
  511. /* We want 10px between items, but there's no margin collapsing in flexbox. */
  512. margin: 10px 5px;
  513. }
  514. .color-scheme-buttons > input:first-child + label {
  515. margin-inline-start: 10px;
  516. }
  517. .color-scheme-buttons > label:last-child {
  518. margin-inline-end: 10px;
  519. }
  520. .font-type-buttons > label {
  521. height: 64px;
  522. width: 105px;
  523. /* Slightly more space between these items. */
  524. margin: 10px;
  525. /* Center the Sans-serif / Serif labels */
  526. text-align: center;
  527. background-size: 63px 39px;
  528. background-repeat: no-repeat;
  529. background-position: center 18px;
  530. background-color: var(--popup-button);
  531. fill: currentColor;
  532. -moz-context-properties: fill;
  533. /* This mostly matches baselines, but because of differences in font
  534. * baselines between serif and sans-serif, this isn't always enough. */
  535. line-height: 1;
  536. padding-top: 2px;
  537. }
  538. .font-type-buttons > label[checked] {
  539. background-color: var(--selected-background);
  540. }
  541. .sans-serif-button {
  542. font-family: Helvetica, Arial, sans-serif;
  543. background-image: url("chrome://global/skin/reader/RM-Sans-Serif.svg");
  544. }
  545. /* Tweak padding to match the baseline on mac */
  546. :root[platform=macosx] .sans-serif-button {
  547. padding-top: 3px;
  548. }
  549. .serif-button {
  550. font-family: Georgia, "Times New Roman", serif;
  551. background-image: url("chrome://global/skin/reader/RM-Serif.svg");
  552. }
  553. /*======= Toolbar icons =======*/
  554. .close-button {
  555. -moz-context-properties: fill;
  556. background-image: url("chrome://global/skin/icons/close.svg");
  557. }
  558. .style-button {
  559. background-image: url("chrome://global/skin/reader/RM-Type-Controls-24x24.svg");
  560. }
  561. .minus-button {
  562. background-size: 18px 18px;
  563. background-image: url("chrome://global/skin/reader/RM-Minus-24x24.svg");
  564. }
  565. .plus-button {
  566. background-size: 18px 18px;
  567. background-image: url("chrome://global/skin/reader/RM-Plus-24x24.svg");
  568. }
  569. .content-width-minus-button {
  570. background-size: 42px 16px;
  571. background-image: url("chrome://global/skin/reader/RM-Content-Width-Minus-42x16.svg");
  572. }
  573. .content-width-plus-button {
  574. background-size: 44px 16px;
  575. background-image: url("chrome://global/skin/reader/RM-Content-Width-Plus-44x16.svg");
  576. }
  577. .line-height-minus-button {
  578. background-size: 34px 14px;
  579. background-image: url("chrome://global/skin/reader/RM-Line-Height-Minus-38x14.svg");
  580. }
  581. .line-height-plus-button {
  582. background-size: 34px 24px;
  583. background-image: url("chrome://global/skin/reader/RM-Line-Height-Plus-38x24.svg");
  584. }
  585. /* Mirror the line height buttons if the article is RTL. */
  586. .reader-controls[articledir="rtl"] .line-height-minus-button,
  587. .reader-controls[articledir="rtl"] .line-height-plus-button {
  588. transform: scaleX(-1);
  589. }
  590. @media print {
  591. .toolbar {
  592. display: none !important;
  593. }
  594. }
  595. /*======= Article content =======*/
  596. /* Note that any class names from the original article that we want to match on
  597. * must be added to CLASSES_TO_PRESERVE in ReaderMode.jsm, so that
  598. * Readability.js doesn't strip them out */
  599. .moz-reader-content {
  600. display: none;
  601. font-size: 1em;
  602. }
  603. @media print {
  604. .moz-reader-content p,
  605. .moz-reader-content code,
  606. .moz-reader-content pre,
  607. .moz-reader-content blockquote,
  608. .moz-reader-content ul,
  609. .moz-reader-content ol,
  610. .moz-reader-content li,
  611. .moz-reader-content figure,
  612. .moz-reader-content .wp-caption {
  613. margin: 0 0 10px !important;
  614. padding: 0 !important;
  615. }
  616. }
  617. .moz-reader-content h1,
  618. .moz-reader-content h2,
  619. .moz-reader-content h3 {
  620. font-weight: bold;
  621. }
  622. .moz-reader-content h1 {
  623. font-size: 1.6em;
  624. line-height: 1.25em;
  625. }
  626. .moz-reader-content h2 {
  627. font-size: 1.2em;
  628. line-height: 1.51em;
  629. }
  630. .moz-reader-content h3 {
  631. font-size: 1em;
  632. line-height: 1.66em;
  633. }
  634. .moz-reader-content a:link {
  635. text-decoration: underline;
  636. font-weight: normal;
  637. }
  638. .moz-reader-content a:link,
  639. .moz-reader-content a:link:hover,
  640. .moz-reader-content a:link:active {
  641. color: var(--link-foreground);
  642. }
  643. .moz-reader-content a:visited {
  644. color: var(--visited-link-foreground);
  645. }
  646. .moz-reader-content * {
  647. max-width: 100%;
  648. height: auto;
  649. }
  650. .moz-reader-content p,
  651. .moz-reader-content p,
  652. .moz-reader-content code,
  653. .moz-reader-content pre,
  654. .moz-reader-content blockquote,
  655. .moz-reader-content ul,
  656. .moz-reader-content ol,
  657. .moz-reader-content li,
  658. .moz-reader-content figure,
  659. .moz-reader-content .wp-caption {
  660. margin: -10px -10px 20px;
  661. padding: 10px;
  662. border-radius: 5px;
  663. }
  664. .moz-reader-content li {
  665. margin-bottom: 0;
  666. }
  667. .moz-reader-content li > ul,
  668. .moz-reader-content li > ol {
  669. margin-bottom: -10px;
  670. }
  671. .moz-reader-content p > img:only-child,
  672. .moz-reader-content p > a:only-child > img:only-child,
  673. .moz-reader-content .wp-caption img,
  674. .moz-reader-content figure img {
  675. display: block;
  676. }
  677. .moz-reader-content img[moz-reader-center] {
  678. margin-inline: auto;
  679. }
  680. .moz-reader-content .caption,
  681. .moz-reader-content .wp-caption-text
  682. .moz-reader-content figcaption {
  683. font-size: 0.9em;
  684. line-height: 1.48em;
  685. font-style: italic;
  686. }
  687. .moz-reader-content code,
  688. .moz-reader-content pre {
  689. white-space: pre-wrap;
  690. }
  691. .moz-reader-content blockquote {
  692. padding: 0;
  693. padding-inline-start: 16px;
  694. }
  695. .moz-reader-content ul,
  696. .moz-reader-content ol {
  697. padding: 0;
  698. }
  699. .moz-reader-content ul {
  700. padding-inline-start: 30px;
  701. list-style: disc;
  702. }
  703. .moz-reader-content ol {
  704. padding-inline-start: 30px;
  705. }
  706. table,
  707. th,
  708. td {
  709. border: 1px solid currentColor;
  710. border-collapse: collapse;
  711. padding: 6px;
  712. vertical-align: top;
  713. }
  714. table {
  715. margin: 5px;
  716. }
  717. /* Visually hide (but don't display: none) screen reader elements */
  718. .moz-reader-content .visually-hidden,
  719. .moz-reader-content .visuallyhidden,
  720. .moz-reader-content .sr-only {
  721. display: inline-block;
  722. width: 1px;
  723. height: 1px;
  724. margin: -1px;
  725. overflow: hidden;
  726. padding: 0;
  727. border-width: 0;
  728. }
  729. /* Hide elements with common "hidden" class names */
  730. .moz-reader-content .hidden,
  731. .moz-reader-content .invisible {
  732. display: none;
  733. }
  734. /* Enforce wordpress and similar emoji/smileys aren't sized to be full-width,
  735. * see bug 1399616 for context. */
  736. .moz-reader-content img.wp-smiley,
  737. .moz-reader-content img.emoji {
  738. display: inline-block;
  739. border-width: 0;
  740. /* height: auto is implied from .moz-reader-content * rule. */
  741. width: 1em;
  742. margin: 0 .07em;
  743. padding: 0;
  744. }
  745. .reader-show-element {
  746. display: initial;
  747. }`;
  748. let NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET;
  749. let selectedNote, anchorElement, maskNoteElement, maskPageElement, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, highlightColor, collapseNoteTimeout, cuttingOuterMode, cuttingMode, cuttingPath, cuttingPathIndex, previousContent;
  750. let removedElements = [], removedElementIndex = 0, initScriptContent;
  751. window.onmessage = async event => {
  752. const message = JSON.parse(event.data);
  753. if (message.method == "init") {
  754. await init(message.content);
  755. }
  756. if (message.method == "addNote") {
  757. addNote(message);
  758. }
  759. if (message.method == "displayNotes") {
  760. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.remove(NOTE_HIDDEN_CLASS));
  761. }
  762. if (message.method == "hideNotes") {
  763. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.add(NOTE_HIDDEN_CLASS));
  764. }
  765. if (message.method == "enableHighlight") {
  766. if (highlightColor) {
  767. document.documentElement.classList.remove(highlightColor + "-mode");
  768. }
  769. highlightColor = message.color;
  770. highlightSelectionMode = true;
  771. document.documentElement.classList.add(message.color + "-mode");
  772. }
  773. if (message.method == "disableHighlight") {
  774. if (highlightColor) {
  775. document.documentElement.classList.remove(highlightColor + "-mode");
  776. }
  777. highlightSelectionMode = false;
  778. }
  779. if (message.method == "displayHighlights") {
  780. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  781. }
  782. if (message.method == "hideHighlights") {
  783. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.add(HIGHLIGHT_HIDDEN_CLASS));
  784. }
  785. if (message.method == "enableRemoveHighlights") {
  786. removeHighlightMode = true;
  787. document.documentElement.classList.add("single-file-remove-highlights-mode");
  788. }
  789. if (message.method == "disableRemoveHighlights") {
  790. removeHighlightMode = false;
  791. document.documentElement.classList.remove("single-file-remove-highlights-mode");
  792. }
  793. if (message.method == "enableEditPage") {
  794. document.body.contentEditable = true;
  795. onUpdate(false);
  796. }
  797. if (message.method == "formatPage") {
  798. formatPage(true);
  799. }
  800. if (message.method == "formatPageNoTheme") {
  801. formatPage(false);
  802. }
  803. if (message.method == "cancelFormatPage") {
  804. cancelFormatPage();
  805. }
  806. if (message.method == "disableEditPage") {
  807. document.body.contentEditable = false;
  808. }
  809. if (message.method == "enableCutInnerPage") {
  810. cuttingMode = true;
  811. document.documentElement.classList.add("single-file-cut-mode");
  812. }
  813. if (message.method == "enableCutOuterPage") {
  814. cuttingOuterMode = true;
  815. document.documentElement.classList.add("single-file-cut-mode");
  816. }
  817. if (message.method == "disableCutInnerPage") {
  818. cuttingMode = false;
  819. document.documentElement.classList.remove("single-file-cut-mode");
  820. resetSelectedElements();
  821. if (cuttingPath) {
  822. unhighlightCutElement();
  823. cuttingPath = null;
  824. }
  825. }
  826. if (message.method == "disableCutOuterPage") {
  827. cuttingOuterMode = false;
  828. document.documentElement.classList.remove("single-file-cut-mode");
  829. resetSelectedElements();
  830. if (cuttingPath) {
  831. unhighlightCutElement();
  832. cuttingPath = null;
  833. }
  834. }
  835. if (message.method == "undoCutPage") {
  836. undoCutPage();
  837. }
  838. if (message.method == "undoAllCutPage") {
  839. while (removedElementIndex) {
  840. removedElements[removedElementIndex - 1].forEach(element => element.classList.remove(REMOVED_CONTENT_CLASS));
  841. removedElementIndex--;
  842. }
  843. }
  844. if (message.method == "redoCutPage") {
  845. redoCutPage();
  846. }
  847. if (message.method == "getContent") {
  848. onUpdate(true);
  849. let content = getContent(message.compressHTML, message.updatedResources);
  850. if (initScriptContent) {
  851. content = content.replace(/<script data-template-shadow-root src.*?<\/script>/g, initScriptContent);
  852. }
  853. window.parent.postMessage(JSON.stringify({ method: "setContent", content }), "*");
  854. }
  855. if (message.method == "printPage") {
  856. printPage();
  857. }
  858. };
  859. window.onresize = reflowNotes;
  860. document.ondragover = event => event.preventDefault();
  861. document.ondrop = async event => {
  862. if (event.dataTransfer.files && event.dataTransfer.files[0]) {
  863. const file = event.dataTransfer.files[0];
  864. event.preventDefault();
  865. const content = new TextDecoder().decode(await file.arrayBuffer());
  866. await init(content, { filename: file.name });
  867. }
  868. };
  869. async function init(content, { filename, reset } = {}) {
  870. await initConstants();
  871. const initScriptContentMatch = content.match(/<script data-template-shadow-root.*<\/script>/);
  872. if (initScriptContentMatch && initScriptContentMatch[0]) {
  873. initScriptContent = initScriptContentMatch[0];
  874. }
  875. content = content.replace(/<script data-template-shadow-root.*<\/script>/g, "<script data-template-shadow-root src=/dist/web/editor/editor-init-web.js></script>");
  876. const contentDocument = (new DOMParser()).parseFromString(content, "text/html");
  877. if (detectSavedPage(contentDocument)) {
  878. if (contentDocument.doctype) {
  879. if (document.doctype) {
  880. document.replaceChild(contentDocument.doctype, document.doctype);
  881. } else {
  882. document.insertBefore(contentDocument.doctype, document.documentElement);
  883. }
  884. } else {
  885. document.doctype.remove();
  886. }
  887. contentDocument.querySelectorAll("noscript").forEach(element => {
  888. element.setAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME, element.innerHTML);
  889. element.textContent = "";
  890. });
  891. contentDocument.querySelectorAll("iframe").forEach(element => {
  892. const pointerEvents = "pointer-events";
  893. element.style.setProperty("-sf-" + pointerEvents, element.style.getPropertyValue(pointerEvents), element.style.getPropertyPriority(pointerEvents));
  894. element.style.setProperty(pointerEvents, "none", "important");
  895. });
  896. document.replaceChild(contentDocument.documentElement, document.documentElement);
  897. deserializeShadowRoots(document);
  898. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => attachNoteListeners(containerElement, true));
  899. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  900. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  901. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  902. document.documentElement.onmousedown = document.documentElement.ontouchstart = onMouseDown;
  903. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  904. document.documentElement.onmouseover = onMouseOver;
  905. document.documentElement.onmouseout = onMouseOut;
  906. document.documentElement.onkeydown = onKeyDown;
  907. window.onclick = event => event.preventDefault();
  908. const iconElement = document.querySelector("link[rel*=icon]");
  909. window.parent.postMessage(JSON.stringify({
  910. method: "onInit",
  911. title: document.title,
  912. icon: iconElement && iconElement.href,
  913. filename,
  914. reset,
  915. formatPageEnabled: isProbablyReaderable(document)
  916. }), "*");
  917. }
  918. }
  919. async function initConstants() {
  920. [NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET] = await Promise.all([
  921. minifyText(await ((await fetch("../pages/editor-note-web.css")).text())),
  922. minifyText(await ((await fetch("../pages/editor-mask-web.css")).text())),
  923. minifyText(await ((await fetch("../pages/editor-frame-web.css")).text()))
  924. ]);
  925. }
  926. function addNote({ color }) {
  927. const containerElement = document.createElement(NOTE_TAGNAME);
  928. const noteElement = document.createElement("div");
  929. const headerElement = document.createElement("header");
  930. const mainElement = document.createElement("textarea");
  931. const resizeElement = document.createElement("div");
  932. const removeNoteElement = document.createElement("img");
  933. const anchorIconElement = document.createElement("img");
  934. const noteShadow = containerElement.attachShadow({ mode: "open" });
  935. headerElement.appendChild(anchorIconElement);
  936. headerElement.appendChild(removeNoteElement);
  937. noteElement.appendChild(headerElement);
  938. noteElement.appendChild(mainElement);
  939. noteElement.appendChild(resizeElement);
  940. noteShadow.appendChild(getStyleElement(NOTES_WEB_STYLESHEET));
  941. noteShadow.appendChild(noteElement);
  942. const notesElements = Array.from(document.querySelectorAll(NOTE_TAGNAME));
  943. const noteId = Math.max.call(Math, 0, ...notesElements.map(noteElement => Number(noteElement.dataset.noteId))) + 1;
  944. noteElement.classList.add(NOTE_CLASS);
  945. noteElement.classList.add(NOTE_ANCHORED_CLASS);
  946. noteElement.classList.add(color);
  947. noteElement.dataset.color = color;
  948. mainElement.dir = "auto";
  949. const boundingRectDocument = document.documentElement.getBoundingClientRect();
  950. let positionX = NOTE_INITIAL_WIDTH + NOTE_INITIAL_POSITION_X - 1 - boundingRectDocument.x;
  951. let positionY = NOTE_INITIAL_HEIGHT + NOTE_INITIAL_POSITION_Y - 1 - boundingRectDocument.y;
  952. while (Array.from(document.elementsFromPoint(positionX - window.scrollX, positionY - window.scrollY)).find(element => element.tagName.toLowerCase() == NOTE_TAGNAME)) {
  953. positionX += NOTE_INITIAL_POSITION_X;
  954. positionY += NOTE_INITIAL_POSITION_Y;
  955. }
  956. noteElement.style.setProperty("left", (positionX - NOTE_INITIAL_WIDTH - 1) + "px");
  957. noteElement.style.setProperty("top", (positionY - NOTE_INITIAL_HEIGHT - 1) + "px");
  958. resizeElement.className = "note-resize";
  959. resizeElement.ondragstart = event => event.preventDefault();
  960. removeNoteElement.className = "note-remove";
  961. removeNoteElement.src = BUTTON_CLOSE_URL;
  962. removeNoteElement.ondragstart = event => event.preventDefault();
  963. anchorIconElement.className = "note-anchor";
  964. anchorIconElement.src = BUTTON_ANCHOR_URL;
  965. anchorIconElement.ondragstart = event => event.preventDefault();
  966. containerElement.dataset.noteId = noteId;
  967. addNoteRef(document.documentElement, noteId);
  968. attachNoteListeners(containerElement, true);
  969. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  970. noteElement.classList.add(NOTE_SELECTED_CLASS);
  971. selectedNote = noteElement;
  972. onUpdate(false);
  973. }
  974. function attachNoteListeners(containerElement, editable = false) {
  975. const SELECT_PX_THRESHOLD = 4;
  976. const COLLAPSING_NOTE_DELAY = 750;
  977. const noteShadow = containerElement.shadowRoot;
  978. const noteElement = noteShadow.childNodes[1];
  979. const headerElement = noteShadow.querySelector("header");
  980. const mainElement = noteShadow.querySelector("textarea");
  981. const noteId = containerElement.dataset.noteId;
  982. const resizeElement = noteShadow.querySelector(".note-resize");
  983. const anchorIconElement = noteShadow.querySelector(".note-anchor");
  984. const removeNoteElement = noteShadow.querySelector(".note-remove");
  985. mainElement.readOnly = !editable;
  986. if (!editable) {
  987. anchorIconElement.style.setProperty("display", "none", "important");
  988. } else {
  989. anchorIconElement.style.removeProperty("display");
  990. }
  991. headerElement.ontouchstart = headerElement.onmousedown = event => {
  992. if (event.target == headerElement) {
  993. collapseNoteTimeout = setTimeout(() => {
  994. noteElement.classList.toggle("note-collapsed");
  995. hideMaskNote();
  996. }, COLLAPSING_NOTE_DELAY);
  997. event.preventDefault();
  998. const position = getPosition(event);
  999. const clientX = position.clientX;
  1000. const clientY = position.clientY;
  1001. const boundingRect = noteElement.getBoundingClientRect();
  1002. const deltaX = clientX - boundingRect.left;
  1003. const deltaY = clientY - boundingRect.top;
  1004. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  1005. document.documentElement.style.setProperty("user-select", "none", "important");
  1006. anchorElement = getAnchorElement(containerElement);
  1007. displayMaskNote();
  1008. selectNote(noteElement);
  1009. moveNote(event, deltaX, deltaY);
  1010. movingNoteMode = { event, deltaX, deltaY };
  1011. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  1012. clearTimeout(collapseNoteTimeout);
  1013. if (!movingNoteMode) {
  1014. movingNoteMode = { deltaX, deltaY };
  1015. }
  1016. movingNoteMode.event = event;
  1017. moveNote(event, deltaX, deltaY);
  1018. };
  1019. }
  1020. };
  1021. resizeElement.ontouchstart = resizeElement.onmousedown = event => {
  1022. event.preventDefault();
  1023. resizingNoteMode = true;
  1024. selectNote(noteElement);
  1025. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  1026. document.documentElement.style.setProperty("user-select", "none", "important");
  1027. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  1028. event.preventDefault();
  1029. const { clientX, clientY } = getPosition(event);
  1030. const boundingRectNote = noteElement.getBoundingClientRect();
  1031. noteElement.style.width = clientX - boundingRectNote.left + "px";
  1032. noteElement.style.height = clientY - boundingRectNote.top + "px";
  1033. };
  1034. };
  1035. anchorIconElement.ontouchend = anchorIconElement.onclick = event => {
  1036. event.preventDefault();
  1037. noteElement.classList.toggle(NOTE_ANCHORED_CLASS);
  1038. if (!noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  1039. deleteNoteRef(containerElement, noteId);
  1040. addNoteRef(document.documentElement, noteId);
  1041. }
  1042. onUpdate(false);
  1043. };
  1044. removeNoteElement.ontouchend = removeNoteElement.onclick = event => {
  1045. event.preventDefault();
  1046. deleteNoteRef(containerElement, noteId);
  1047. containerElement.remove();
  1048. };
  1049. noteElement.onmousedown = () => {
  1050. selectNote(noteElement);
  1051. };
  1052. function moveNote(event, deltaX, deltaY) {
  1053. event.preventDefault();
  1054. const { clientX, clientY } = getPosition(event);
  1055. noteElement.classList.add(NOTE_MOVING_CLASS);
  1056. if (editable) {
  1057. if (noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  1058. deleteNoteRef(containerElement, noteId);
  1059. anchorElement = getTarget(clientX, clientY) || document.documentElement;
  1060. addNoteRef(anchorElement, noteId);
  1061. } else {
  1062. anchorElement = document.documentElement;
  1063. }
  1064. }
  1065. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  1066. noteElement.style.setProperty("left", (clientX - deltaX) + "px");
  1067. noteElement.style.setProperty("top", (clientY - deltaY) + "px");
  1068. noteElement.style.setProperty("position", "fixed");
  1069. displayMaskNote();
  1070. }
  1071. function displayMaskNote() {
  1072. if (anchorElement == document.documentElement || anchorElement == document.documentElement) {
  1073. hideMaskNote();
  1074. } else {
  1075. const boundingRectAnchor = anchorElement.getBoundingClientRect();
  1076. maskNoteElement.classList.add(NOTE_MASK_MOVING_CLASS);
  1077. if (selectedNote) {
  1078. maskNoteElement.classList.add(selectedNote.dataset.color);
  1079. }
  1080. maskNoteElement.style.setProperty("top", (boundingRectAnchor.y - 3) + "px");
  1081. maskNoteElement.style.setProperty("left", (boundingRectAnchor.x - 3) + "px");
  1082. maskNoteElement.style.setProperty("width", (boundingRectAnchor.width + 3) + "px");
  1083. maskNoteElement.style.setProperty("height", (boundingRectAnchor.height + 3) + "px");
  1084. }
  1085. }
  1086. function hideMaskNote() {
  1087. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  1088. if (selectedNote) {
  1089. maskNoteElement.classList.remove(selectedNote.dataset.color);
  1090. }
  1091. }
  1092. function selectNote(noteElement) {
  1093. if (selectedNote) {
  1094. selectedNote.classList.remove(NOTE_SELECTED_CLASS);
  1095. maskNoteElement.classList.remove(selectedNote.dataset.color);
  1096. }
  1097. noteElement.classList.add(NOTE_SELECTED_CLASS);
  1098. noteElement.classList.add(noteElement.dataset.color);
  1099. selectedNote = noteElement;
  1100. }
  1101. function getTarget(clientX, clientY) {
  1102. const targets = Array.from(document.elementsFromPoint(clientX, clientY)).filter(element => element.matches("html *:not(" + NOTE_TAGNAME + "):not(." + MASK_CLASS + ")"));
  1103. if (!targets.includes(document.documentElement)) {
  1104. targets.push(document.documentElement);
  1105. }
  1106. let newTarget, target = targets[0], boundingRect = target.getBoundingClientRect();
  1107. newTarget = determineTargetElement("floor", target, clientX - boundingRect.left, getMatchedParents(target, "left"));
  1108. if (newTarget == target) {
  1109. newTarget = determineTargetElement("ceil", target, boundingRect.left + boundingRect.width - clientX, getMatchedParents(target, "right"));
  1110. }
  1111. if (newTarget == target) {
  1112. newTarget = determineTargetElement("floor", target, clientY - boundingRect.top, getMatchedParents(target, "top"));
  1113. }
  1114. if (newTarget == target) {
  1115. newTarget = determineTargetElement("ceil", target, boundingRect.top + boundingRect.height - clientY, getMatchedParents(target, "bottom"));
  1116. }
  1117. target = newTarget;
  1118. while (boundingRect = target && target.getBoundingClientRect(), boundingRect && boundingRect.width <= SELECT_PX_THRESHOLD && boundingRect.height <= SELECT_PX_THRESHOLD) {
  1119. target = target.parentElement;
  1120. }
  1121. return target;
  1122. }
  1123. function getMatchedParents(target, property) {
  1124. let element = target, matchedParent, parents = [];
  1125. do {
  1126. const boundingRect = element.getBoundingClientRect();
  1127. if (element.parentElement && !element.parentElement.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS)) {
  1128. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  1129. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  1130. if (matchedParent) {
  1131. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  1132. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  1133. parents.push(element.parentElement);
  1134. }
  1135. element = element.parentElement;
  1136. }
  1137. } else {
  1138. matchedParent = false;
  1139. }
  1140. } while (matchedParent && element);
  1141. return parents;
  1142. }
  1143. function determineTargetElement(roundingMethod, target, widthDistance, parents) {
  1144. if (Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  1145. target = parents[parents.length - Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) - 1];
  1146. }
  1147. return target;
  1148. }
  1149. }
  1150. function onMouseDown(event) {
  1151. if ((cuttingMode || cuttingOuterMode) && cuttingPath) {
  1152. event.preventDefault();
  1153. }
  1154. }
  1155. function onMouseUp(event) {
  1156. if (highlightSelectionMode) {
  1157. highlightSelection();
  1158. onUpdate(false);
  1159. }
  1160. if (removeHighlightMode) {
  1161. let element = event.target, done;
  1162. while (element && !done) {
  1163. if (element.classList.contains(HIGHLIGHT_CLASS)) {
  1164. document.querySelectorAll("." + HIGHLIGHT_CLASS + "[data-singlefile-highlight-id=" + JSON.stringify(element.dataset.singlefileHighlightId) + "]").forEach(highlightedElement => {
  1165. resetHighlightedElement(highlightedElement);
  1166. onUpdate(false);
  1167. });
  1168. done = true;
  1169. }
  1170. element = element.parentElement;
  1171. }
  1172. }
  1173. if (resizingNoteMode) {
  1174. resizingNoteMode = false;
  1175. document.documentElement.style.removeProperty("user-select");
  1176. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1177. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1178. onUpdate(false);
  1179. }
  1180. if (movingNoteMode) {
  1181. anchorNote(movingNoteMode.event || event, selectedNote, movingNoteMode.deltaX, movingNoteMode.deltaY);
  1182. movingNoteMode = null;
  1183. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1184. onUpdate(false);
  1185. }
  1186. if (collapseNoteTimeout) {
  1187. clearTimeout(collapseNoteTimeout);
  1188. collapseNoteTimeout = null;
  1189. }
  1190. if ((cuttingMode || cuttingOuterMode) && cuttingPath) {
  1191. if (event.ctrlKey) {
  1192. const element = cuttingPath[cuttingPathIndex];
  1193. element.classList.toggle(cuttingMode ? CUT_SELECTED_CLASS : CUT_OUTER_SELECTED_CLASS);
  1194. } else {
  1195. validateCutElement(event.shiftKey);
  1196. }
  1197. }
  1198. }
  1199. function onMouseOver(event) {
  1200. if (cuttingMode || cuttingOuterMode) {
  1201. const target = event.target;
  1202. if (target.classList) {
  1203. let ancestorFound;
  1204. document.querySelectorAll("." + (cuttingMode ? CUT_SELECTED_CLASS : CUT_OUTER_SELECTED_CLASS)).forEach(element => {
  1205. if (element == target || isAncestor(element, target) || isAncestor(target, element)) {
  1206. ancestorFound = element;
  1207. }
  1208. });
  1209. if (ancestorFound) {
  1210. cuttingPath = [ancestorFound];
  1211. } else {
  1212. cuttingPath = getParents(event.target);
  1213. }
  1214. cuttingPathIndex = 0;
  1215. highlightCutElement();
  1216. }
  1217. }
  1218. }
  1219. function onMouseOut() {
  1220. if (cuttingMode || cuttingOuterMode) {
  1221. if (cuttingPath) {
  1222. unhighlightCutElement();
  1223. cuttingPath = null;
  1224. }
  1225. }
  1226. }
  1227. function onKeyDown(event) {
  1228. if (cuttingMode || cuttingOuterMode) {
  1229. if (event.code == "Tab") {
  1230. if (cuttingPath) {
  1231. const delta = event.shiftKey ? -1 : 1;
  1232. let element = cuttingPath[cuttingPathIndex];
  1233. let nextElement = cuttingPath[cuttingPathIndex + delta];
  1234. if (nextElement) {
  1235. let pathIndex = cuttingPathIndex + delta;
  1236. while (
  1237. nextElement &&
  1238. (
  1239. (delta == 1 &&
  1240. element.getBoundingClientRect().width >= nextElement.getBoundingClientRect().width &&
  1241. element.getBoundingClientRect().height >= nextElement.getBoundingClientRect().height) ||
  1242. (delta == -1 &&
  1243. element.getBoundingClientRect().width <= nextElement.getBoundingClientRect().width &&
  1244. element.getBoundingClientRect().height <= nextElement.getBoundingClientRect().height))) {
  1245. pathIndex += delta;
  1246. nextElement = cuttingPath[pathIndex];
  1247. }
  1248. if (nextElement && nextElement.classList && nextElement != document.body && nextElement != document.documentElement) {
  1249. unhighlightCutElement();
  1250. cuttingPathIndex = pathIndex;
  1251. highlightCutElement();
  1252. }
  1253. }
  1254. }
  1255. event.preventDefault();
  1256. }
  1257. if (event.code == "Space") {
  1258. if (cuttingPath) {
  1259. if (event.ctrlKey) {
  1260. const element = cuttingPath[cuttingPathIndex];
  1261. element.classList.add(cuttingMode ? CUT_SELECTED_CLASS : CUT_OUTER_SELECTED_CLASS);
  1262. } else {
  1263. validateCutElement(event.shiftKey);
  1264. }
  1265. event.preventDefault();
  1266. }
  1267. }
  1268. if (event.code == "Escape") {
  1269. resetSelectedElements();
  1270. event.preventDefault();
  1271. }
  1272. if (event.key.toLowerCase() == "z" && event.ctrlKey) {
  1273. if (event.shiftKey) {
  1274. redoCutPage();
  1275. } else {
  1276. undoCutPage();
  1277. }
  1278. event.preventDefault();
  1279. }
  1280. }
  1281. if (event.key.toLowerCase() == "s" && event.ctrlKey) {
  1282. window.parent.postMessage(JSON.stringify({ "method": "savePage" }), "*");
  1283. event.preventDefault();
  1284. }
  1285. if (event.key.toLowerCase() == "p" && event.ctrlKey) {
  1286. printPage();
  1287. event.preventDefault();
  1288. }
  1289. }
  1290. function printPage() {
  1291. unhighlightCutElement();
  1292. resetSelectedElements();
  1293. window.print();
  1294. }
  1295. function highlightCutElement() {
  1296. const element = cuttingPath[cuttingPathIndex];
  1297. element.classList.add(cuttingMode ? CUT_HOVER_CLASS : CUT_OUTER_HOVER_CLASS);
  1298. }
  1299. function unhighlightCutElement() {
  1300. if (cuttingPath) {
  1301. const element = cuttingPath[cuttingPathIndex];
  1302. element.classList.remove(CUT_HOVER_CLASS);
  1303. element.classList.remove(CUT_OUTER_HOVER_CLASS);
  1304. }
  1305. }
  1306. function undoCutPage() {
  1307. if (removedElementIndex) {
  1308. removedElements[removedElementIndex - 1].forEach(element => element.classList.remove(REMOVED_CONTENT_CLASS));
  1309. removedElementIndex--;
  1310. }
  1311. }
  1312. function redoCutPage() {
  1313. if (removedElementIndex < removedElements.length) {
  1314. removedElements[removedElementIndex].forEach(element => element.classList.add(REMOVED_CONTENT_CLASS));
  1315. removedElementIndex++;
  1316. }
  1317. }
  1318. function validateCutElement(invert) {
  1319. const selectedElement = cuttingPath[cuttingPathIndex];
  1320. if ((cuttingMode && !invert) || (cuttingOuterMode && invert)) {
  1321. if (document.documentElement != selectedElement && selectedElement.tagName.toLowerCase() != NOTE_TAGNAME) {
  1322. const elementsRemoved = [selectedElement].concat(...document.querySelectorAll("." + CUT_SELECTED_CLASS + ",." + CUT_SELECTED_CLASS + " *,." + CUT_HOVER_CLASS + " *"));
  1323. resetSelectedElements();
  1324. if (elementsRemoved.length) {
  1325. elementsRemoved.forEach(element => {
  1326. if (element.tagName.toLowerCase() == NOTE_TAGNAME) {
  1327. resetAnchorNote(element);
  1328. } else {
  1329. element.classList.add(REMOVED_CONTENT_CLASS);
  1330. }
  1331. });
  1332. removedElements[removedElementIndex] = elementsRemoved;
  1333. removedElementIndex++;
  1334. removedElements.length = removedElementIndex;
  1335. onUpdate(false);
  1336. }
  1337. }
  1338. } else {
  1339. if (document.documentElement != selectedElement && selectedElement.tagName.toLowerCase() != NOTE_TAGNAME) {
  1340. const elements = [];
  1341. const searchSelector = "*:not(style):not(meta):not(." + REMOVED_CONTENT_CLASS + ")";
  1342. const elementsKept = [selectedElement].concat(...document.querySelectorAll("." + CUT_OUTER_SELECTED_CLASS));
  1343. document.body.querySelectorAll(searchSelector).forEach(element => {
  1344. let removed = true;
  1345. elementsKept.forEach(elementKept => removed = removed && (elementKept != element && !isAncestor(elementKept, element) && !isAncestor(element, elementKept)));
  1346. if (removed) {
  1347. if (element.tagName.toLowerCase() == NOTE_TAGNAME) {
  1348. resetAnchorNote(element);
  1349. } else {
  1350. elements.push(element);
  1351. }
  1352. }
  1353. });
  1354. elementsKept.forEach(elementKept => {
  1355. const elementKeptRect = elementKept.getBoundingClientRect();
  1356. elementKept.querySelectorAll(searchSelector).forEach(descendant => {
  1357. const descendantRect = descendant.getBoundingClientRect();
  1358. if (descendantRect.width && descendantRect.height && (
  1359. descendantRect.left + descendantRect.width < elementKeptRect.left ||
  1360. descendantRect.right > elementKeptRect.right + elementKeptRect.width ||
  1361. descendantRect.top + descendantRect.height < elementKeptRect.top ||
  1362. descendantRect.bottom > elementKeptRect.bottom + elementKeptRect.height
  1363. )) {
  1364. elements.push(descendant);
  1365. }
  1366. });
  1367. });
  1368. resetSelectedElements();
  1369. if (elements.length) {
  1370. elements.forEach(element => element.classList.add(REMOVED_CONTENT_CLASS));
  1371. removedElements[removedElementIndex] = elements;
  1372. removedElementIndex++;
  1373. removedElements.length = removedElementIndex;
  1374. onUpdate(false);
  1375. }
  1376. }
  1377. }
  1378. }
  1379. function resetSelectedElements(doc = document) {
  1380. doc.querySelectorAll("." + CUT_OUTER_SELECTED_CLASS + ",." + CUT_SELECTED_CLASS).forEach(element => {
  1381. element.classList.remove(CUT_OUTER_SELECTED_CLASS);
  1382. element.classList.remove(CUT_SELECTED_CLASS);
  1383. });
  1384. }
  1385. function anchorNote(event, noteElement, deltaX, deltaY) {
  1386. event.preventDefault();
  1387. const { clientX, clientY } = getPosition(event);
  1388. document.documentElement.style.removeProperty("user-select");
  1389. noteElement.classList.remove(NOTE_MOVING_CLASS);
  1390. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  1391. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1392. maskNoteElement.classList.remove(noteElement.dataset.color);
  1393. const headerElement = noteElement.querySelector("header");
  1394. headerElement.ontouchmove = document.documentElement.onmousemove = null;
  1395. let currentElement = anchorElement;
  1396. let positionedElement;
  1397. while (currentElement.parentElement && !positionedElement) {
  1398. if (!FORBIDDEN_TAG_NAMES.includes(currentElement.tagName.toLowerCase())) {
  1399. const currentElementStyle = getComputedStyle(currentElement);
  1400. if (currentElementStyle.position != "static") {
  1401. positionedElement = currentElement;
  1402. }
  1403. }
  1404. currentElement = currentElement.parentElement;
  1405. }
  1406. if (!positionedElement) {
  1407. positionedElement = document.documentElement;
  1408. }
  1409. const containerElement = noteElement.getRootNode().host;
  1410. if (positionedElement == document.documentElement) {
  1411. const firstMaskElement = document.querySelector("." + MASK_CLASS);
  1412. firstMaskElement.parentElement.insertBefore(containerElement, firstMaskElement);
  1413. } else {
  1414. positionedElement.appendChild(containerElement);
  1415. }
  1416. const boundingRectPositionedElement = positionedElement.getBoundingClientRect();
  1417. const stylePositionedElement = window.getComputedStyle(positionedElement);
  1418. const borderX = parseInt(stylePositionedElement.getPropertyValue("border-left-width"));
  1419. const borderY = parseInt(stylePositionedElement.getPropertyValue("border-top-width"));
  1420. noteElement.style.setProperty("position", "absolute");
  1421. noteElement.style.setProperty("left", (clientX - boundingRectPositionedElement.x - deltaX - borderX) + "px");
  1422. noteElement.style.setProperty("top", (clientY - boundingRectPositionedElement.y - deltaY - borderY) + "px");
  1423. }
  1424. function resetAnchorNote(containerElement) {
  1425. const noteId = containerElement.dataset.noteId;
  1426. const noteElement = containerElement.shadowRoot.childNodes[1];
  1427. noteElement.classList.remove(NOTE_ANCHORED_CLASS);
  1428. deleteNoteRef(containerElement, noteId);
  1429. addNoteRef(document.documentElement, noteId);
  1430. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  1431. }
  1432. function getPosition(event) {
  1433. if (event.touches && event.touches.length) {
  1434. const touch = event.touches[0];
  1435. return touch;
  1436. } else {
  1437. return event;
  1438. }
  1439. }
  1440. function highlightSelection() {
  1441. let highlightId = 0;
  1442. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(highlightedElement => highlightId = Math.max(highlightId, highlightedElement.dataset.singlefileHighlightId));
  1443. highlightId++;
  1444. const selection = window.getSelection();
  1445. const highlightedNodes = new Set();
  1446. for (let indexRange = 0; indexRange < selection.rangeCount; indexRange++) {
  1447. const range = selection.getRangeAt(indexRange);
  1448. if (!range.collapsed) {
  1449. if (range.commonAncestorContainer.nodeType == range.commonAncestorContainer.TEXT_NODE) {
  1450. let contentText = range.startContainer.splitText(range.startOffset);
  1451. contentText = contentText.splitText(range.endOffset);
  1452. addHighLightedNode(contentText.previousSibling);
  1453. } else {
  1454. const treeWalker = document.createTreeWalker(range.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
  1455. let highlightNodes;
  1456. while (treeWalker.nextNode()) {
  1457. if (highlightNodes && !treeWalker.currentNode.contains(range.endContainer)) {
  1458. addHighLightedNode(treeWalker.currentNode);
  1459. }
  1460. if (treeWalker.currentNode == range.startContainer) {
  1461. if (range.startContainer.nodeType == range.startContainer.TEXT_NODE) {
  1462. const contentText = range.startContainer.splitText(range.startOffset);
  1463. treeWalker.nextNode();
  1464. addHighLightedNode(contentText);
  1465. } else {
  1466. addHighLightedNode(range.startContainer.childNodes[range.startOffset]);
  1467. }
  1468. highlightNodes = true;
  1469. }
  1470. if (treeWalker.currentNode == range.endContainer) {
  1471. if (range.endContainer.nodeType == range.endContainer.TEXT_NODE) {
  1472. const contentText = range.endContainer.splitText(range.endOffset);
  1473. treeWalker.nextNode();
  1474. addHighLightedNode(contentText.previousSibling);
  1475. } else {
  1476. addHighLightedNode(range.endContainer.childNodes[range.endOffset]);
  1477. }
  1478. highlightNodes = false;
  1479. }
  1480. }
  1481. range.collapse();
  1482. }
  1483. }
  1484. }
  1485. highlightedNodes.forEach(node => highlightNode(node));
  1486. function addHighLightedNode(node) {
  1487. if (node && node.textContent.trim()) {
  1488. if (node.nodeType == node.TEXT_NODE && node.parentElement.childNodes.length == 1 && node.parentElement.classList.contains(HIGHLIGHT_CLASS)) {
  1489. highlightedNodes.add(node.parentElement);
  1490. } else {
  1491. highlightedNodes.add(node);
  1492. }
  1493. }
  1494. }
  1495. function highlightNode(node) {
  1496. if (node.nodeType == node.ELEMENT_NODE) {
  1497. resetHighlightedElement(node);
  1498. node.classList.add(HIGHLIGHT_CLASS);
  1499. node.classList.add(highlightColor);
  1500. node.dataset.singlefileHighlightId = highlightId;
  1501. } else if (node.parentElement) {
  1502. highlightTextNode(node);
  1503. }
  1504. }
  1505. function highlightTextNode(node) {
  1506. const spanElement = document.createElement("span");
  1507. spanElement.classList.add(HIGHLIGHT_CLASS);
  1508. spanElement.classList.add(highlightColor);
  1509. spanElement.textContent = node.textContent;
  1510. spanElement.dataset.singlefileHighlightId = highlightId;
  1511. node.parentNode.replaceChild(spanElement, node);
  1512. return spanElement;
  1513. }
  1514. }
  1515. function getParents(element) {
  1516. const path = [];
  1517. while (element) {
  1518. path.push(element);
  1519. element = element.parentElement;
  1520. }
  1521. return path;
  1522. }
  1523. function formatPage(applySystemTheme) {
  1524. previousContent = getContent(false, []);
  1525. const shadowRoots = {};
  1526. const classesToPreserve = ["single-file-highlight", "single-file-highlight-yellow", "single-file-highlight-green", "single-file-highlight-pink", "single-file-highlight-blue"];
  1527. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1528. shadowRoots[containerElement.dataset.noteId] = containerElement.shadowRoot;
  1529. const className = "singlefile-note-id-" + containerElement.dataset.noteId;
  1530. containerElement.classList.add(className);
  1531. classesToPreserve.push(className);
  1532. });
  1533. const article = new Readability(document, { classesToPreserve }).parse();
  1534. removedElements = [];
  1535. removedElementIndex = 0;
  1536. document.body.innerHTML = "";
  1537. const domParser = new DOMParser();
  1538. const doc = domParser.parseFromString(article.content, "text/html");
  1539. const contentEditable = document.body.contentEditable;
  1540. document.documentElement.replaceChild(doc.body, document.body);
  1541. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1542. const noteId = (Array.from(containerElement.classList).find(className => /singlefile-note-id-\d+/.test(className))).split("singlefile-note-id-")[1];
  1543. containerElement.classList.remove("singlefile-note-id-" + noteId);
  1544. containerElement.dataset.noteId = noteId;
  1545. if (!containerElement.shadowRoot) {
  1546. containerElement.attachShadow({ mode: "open" });
  1547. containerElement.shadowRoot.appendChild(shadowRoots[noteId]);
  1548. }
  1549. });
  1550. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => containerElement.shadowRoot = shadowRoots[containerElement.dataset.noteId]);
  1551. document.body.contentEditable = contentEditable;
  1552. document.head.querySelectorAll("style").forEach(styleElement => styleElement.remove());
  1553. const styleElement = document.createElement("style");
  1554. styleElement.textContent = STYLE_FORMATTED_PAGE;
  1555. document.head.appendChild(styleElement);
  1556. document.body.classList.add("moz-reader-content");
  1557. document.body.classList.add("content-width6");
  1558. document.body.classList.add("reader-show-element");
  1559. document.body.classList.add("sans-serif");
  1560. document.body.classList.add("container");
  1561. document.body.classList.add("line-height4");
  1562. const prefersColorSchemeDark = matchMedia("(prefers-color-scheme: dark)");
  1563. if (applySystemTheme && prefersColorSchemeDark && prefersColorSchemeDark.matches) {
  1564. document.body.classList.add("dark");
  1565. }
  1566. document.body.style.setProperty("display", "block");
  1567. document.body.style.setProperty("padding", "24px");
  1568. const titleElement = document.createElement("h1");
  1569. titleElement.classList.add("reader-title");
  1570. titleElement.textContent = article.title;
  1571. document.body.insertBefore(titleElement, document.body.firstChild);
  1572. document.querySelectorAll("a[href]").forEach(element => {
  1573. const href = element.getAttribute("href").trim();
  1574. if (href.startsWith(document.baseURI + "#")) {
  1575. element.setAttribute("href", href.substring(document.baseURI.length));
  1576. }
  1577. });
  1578. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  1579. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  1580. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  1581. reflowNotes();
  1582. onUpdate(false);
  1583. }
  1584. async function cancelFormatPage() {
  1585. if (previousContent) {
  1586. const contentEditable = document.body.contentEditable;
  1587. await init(previousContent, { reset: true });
  1588. document.body.contentEditable = contentEditable;
  1589. onUpdate(false);
  1590. previousContent = null;
  1591. }
  1592. }
  1593. function getContent(compressHTML, updatedResources) {
  1594. unhighlightCutElement();
  1595. serializeShadowRoots(document);
  1596. const doc = document.cloneNode(true);
  1597. resetSelectedElements(doc);
  1598. deserializeShadowRoots(doc);
  1599. deserializeShadowRoots(document);
  1600. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  1601. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1602. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1603. });
  1604. doc.querySelectorAll("." + MASK_CLASS + ", ." + REMOVED_CONTENT_CLASS).forEach(maskElement => maskElement.remove());
  1605. doc.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  1606. doc.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(templateElement => {
  1607. const noteElement = templateElement.querySelector("." + NOTE_CLASS);
  1608. if (noteElement) {
  1609. noteElement.classList.remove(NOTE_HIDDEN_CLASS);
  1610. }
  1611. const mainElement = templateElement.querySelector("textarea");
  1612. if (mainElement) {
  1613. mainElement.textContent = mainElement.value;
  1614. }
  1615. });
  1616. doc.querySelectorAll("iframe").forEach(element => {
  1617. const pointerEvents = "pointer-events";
  1618. element.style.setProperty(pointerEvents, element.style.getPropertyValue("-sf-" + pointerEvents), element.style.getPropertyPriority("-sf-" + pointerEvents));
  1619. element.style.removeProperty("-sf-" + pointerEvents);
  1620. });
  1621. doc.body.removeAttribute("contentEditable");
  1622. const scriptElement = doc.createElement("script");
  1623. scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
  1624. scriptElement.textContent = getEmbedScript();
  1625. doc.body.appendChild(scriptElement);
  1626. const newResources = Object.keys(updatedResources).filter(url => updatedResources[url].type == "stylesheet").map(url => updatedResources[url]);
  1627. newResources.forEach(resource => {
  1628. const element = doc.createElement("style");
  1629. doc.body.appendChild(element);
  1630. element.textContent = resource.content;
  1631. });
  1632. return singlefile.helper.serialize(doc, compressHTML);
  1633. }
  1634. function onUpdate(saved) {
  1635. window.parent.postMessage(JSON.stringify({ "method": "onUpdate", saved }), "*");
  1636. }
  1637. function reflowNotes() {
  1638. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1639. const noteElement = containerElement.shadowRoot.querySelector("." + NOTE_CLASS);
  1640. const noteBoundingRect = noteElement.getBoundingClientRect();
  1641. const anchorElement = getAnchorElement(containerElement);
  1642. const anchorBoundingRect = anchorElement.getBoundingClientRect();
  1643. const maxX = anchorBoundingRect.x + Math.max(0, anchorBoundingRect.width - noteBoundingRect.width);
  1644. const minX = anchorBoundingRect.x;
  1645. const maxY = anchorBoundingRect.y + Math.max(0, anchorBoundingRect.height - NOTE_HEADER_HEIGHT);
  1646. const minY = anchorBoundingRect.y;
  1647. let left = parseInt(noteElement.style.getPropertyValue("left"));
  1648. let top = parseInt(noteElement.style.getPropertyValue("top"));
  1649. if (noteBoundingRect.x > maxX) {
  1650. left -= noteBoundingRect.x - maxX;
  1651. }
  1652. if (noteBoundingRect.x < minX) {
  1653. left += minX - noteBoundingRect.x;
  1654. }
  1655. if (noteBoundingRect.y > maxY) {
  1656. top -= noteBoundingRect.y - maxY;
  1657. }
  1658. if (noteBoundingRect.y < minY) {
  1659. top += minY - noteBoundingRect.y;
  1660. }
  1661. noteElement.style.setProperty("position", "absolute");
  1662. noteElement.style.setProperty("left", left + "px");
  1663. noteElement.style.setProperty("top", top + "px");
  1664. });
  1665. }
  1666. function resetHighlightedElement(element) {
  1667. element.classList.remove(HIGHLIGHT_CLASS);
  1668. element.classList.remove("single-file-highlight-yellow");
  1669. element.classList.remove("single-file-highlight-pink");
  1670. element.classList.remove("single-file-highlight-blue");
  1671. element.classList.remove("single-file-highlight-green");
  1672. delete element.dataset.singlefileHighlightId;
  1673. }
  1674. function serializeShadowRoots(node) {
  1675. node.querySelectorAll("*").forEach(element => {
  1676. const shadowRoot = getShadowRoot(element);
  1677. if (shadowRoot) {
  1678. serializeShadowRoots(shadowRoot);
  1679. const templateElement = document.createElement("template");
  1680. templateElement.setAttribute(SHADOW_MODE_ATTRIBUTE_NAME, "open");
  1681. templateElement.appendChild(shadowRoot);
  1682. element.appendChild(templateElement);
  1683. }
  1684. });
  1685. }
  1686. function deserializeShadowRoots(node) {
  1687. node.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(element => {
  1688. if (element.parentElement) {
  1689. let shadowRoot = getShadowRoot(element.parentElement);
  1690. if (shadowRoot) {
  1691. Array.from(element.childNodes).forEach(node => shadowRoot.appendChild(node));
  1692. element.remove();
  1693. } else {
  1694. try {
  1695. shadowRoot = element.parentElement.attachShadow({ mode: "open" });
  1696. const contentDocument = (new DOMParser()).parseFromString(element.innerHTML, "text/html");
  1697. Array.from(contentDocument.head.childNodes).forEach(node => shadowRoot.appendChild(node));
  1698. Array.from(contentDocument.body.childNodes).forEach(node => shadowRoot.appendChild(node));
  1699. } catch (error) {
  1700. // ignored
  1701. }
  1702. }
  1703. if (shadowRoot) {
  1704. deserializeShadowRoots(shadowRoot);
  1705. }
  1706. }
  1707. });
  1708. }
  1709. function getMaskElement(className, containerClassName) {
  1710. let maskElement = document.documentElement.querySelector("." + className);
  1711. if (!maskElement) {
  1712. maskElement = document.createElement("div");
  1713. const maskContainerElement = document.createElement("div");
  1714. if (containerClassName) {
  1715. maskContainerElement.classList.add(containerClassName);
  1716. }
  1717. maskContainerElement.classList.add(MASK_CLASS);
  1718. const firstNote = document.querySelector(NOTE_TAGNAME);
  1719. if (firstNote && firstNote.parentElement == document.documentElement) {
  1720. document.documentElement.insertBefore(maskContainerElement, firstNote);
  1721. } else {
  1722. document.documentElement.appendChild(maskContainerElement);
  1723. }
  1724. maskElement.classList.add(className);
  1725. const maskShadow = maskContainerElement.attachShadow({ mode: "open" });
  1726. maskShadow.appendChild(getStyleElement(MASK_WEB_STYLESHEET));
  1727. maskShadow.appendChild(maskElement);
  1728. return maskElement;
  1729. }
  1730. }
  1731. function getEmbedScript() {
  1732. return minifyText(`(() => {
  1733. document.currentScript.remove();
  1734. const processNode = node => {
  1735. node.querySelectorAll("template[${SHADOW_MODE_ATTRIBUTE_NAME}]").forEach(element=>{
  1736. let shadowRoot = getShadowRoot(element.parentElement);
  1737. if (!shadowRoot) {
  1738. try {
  1739. shadowRoot = element.parentElement.attachShadow({mode:element.getAttribute("${SHADOW_MODE_ATTRIBUTE_NAME}"),delegatesFocus:Boolean(element.getAttribute("${SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME}"))});
  1740. shadowRoot.innerHTML = element.innerHTML;
  1741. element.remove();
  1742. } catch (error) {}
  1743. if (shadowRoot) {
  1744. processNode(shadowRoot);
  1745. }
  1746. }
  1747. })
  1748. };
  1749. const FORBIDDEN_TAG_NAMES = ${JSON.stringify(FORBIDDEN_TAG_NAMES)};
  1750. const NOTE_TAGNAME = ${JSON.stringify(NOTE_TAGNAME)};
  1751. const NOTE_CLASS = ${JSON.stringify(NOTE_CLASS)};
  1752. const NOTE_ANCHORED_CLASS = ${JSON.stringify(NOTE_ANCHORED_CLASS)};
  1753. const NOTE_SELECTED_CLASS = ${JSON.stringify(NOTE_SELECTED_CLASS)};
  1754. const NOTE_MOVING_CLASS = ${JSON.stringify(NOTE_MOVING_CLASS)};
  1755. const NOTE_MASK_MOVING_CLASS = ${JSON.stringify(NOTE_MASK_MOVING_CLASS)};
  1756. const MASK_CLASS = ${JSON.stringify(MASK_CLASS)};
  1757. const HIGHLIGHT_CLASS = ${JSON.stringify(HIGHLIGHT_CLASS)};
  1758. const NOTES_WEB_STYLESHEET = ${JSON.stringify(NOTES_WEB_STYLESHEET)};
  1759. const MASK_WEB_STYLESHEET = ${JSON.stringify(MASK_WEB_STYLESHEET)};
  1760. const NOTE_HEADER_HEIGHT = ${JSON.stringify(NOTE_HEADER_HEIGHT)};
  1761. const PAGE_MASK_ACTIVE_CLASS = ${JSON.stringify(PAGE_MASK_ACTIVE_CLASS)};
  1762. const REMOVED_CONTENT_CLASS = ${JSON.stringify(REMOVED_CONTENT_CLASS)};
  1763. const reflowNotes = ${minifyText(reflowNotes.toString())};
  1764. const addNoteRef = ${minifyText(addNoteRef.toString())};
  1765. const deleteNoteRef = ${minifyText(deleteNoteRef.toString())};
  1766. const getNoteRefs = ${minifyText(getNoteRefs.toString())};
  1767. const setNoteRefs = ${minifyText(setNoteRefs.toString())};
  1768. const getAnchorElement = ${minifyText(getAnchorElement.toString())};
  1769. const getMaskElement = ${minifyText(getMaskElement.toString())};
  1770. const getStyleElement = ${minifyText(getStyleElement.toString())};
  1771. const attachNoteListeners = ${minifyText(attachNoteListeners.toString())};
  1772. const anchorNote = ${minifyText(anchorNote.toString())};
  1773. const getPosition = ${minifyText(getPosition.toString())};
  1774. const onMouseUp = ${minifyText(onMouseUp.toString())};
  1775. const getShadowRoot = ${minifyText(getShadowRoot.toString())};
  1776. const maskNoteElement = getMaskElement(${JSON.stringify(NOTE_MASK_CLASS)});
  1777. const maskPageElement = getMaskElement(${JSON.stringify(PAGE_MASK_CLASS)}, ${JSON.stringify(PAGE_MASK_CONTAINER_CLASS)});
  1778. let selectedNote, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, collapseNoteTimeout, cuttingMode, cuttingOuterMode;
  1779. window.onresize = reflowNotes;
  1780. window.onUpdate = () => {};
  1781. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  1782. window.addEventListener("DOMContentLoaded", () => {
  1783. processNode(document);
  1784. reflowNotes();
  1785. document.querySelectorAll(${JSON.stringify(NOTE_TAGNAME)}).forEach(noteElement => attachNoteListeners(noteElement));
  1786. });
  1787. })()`);
  1788. }
  1789. function getStyleElement(stylesheet) {
  1790. const linkElement = document.createElement("style");
  1791. linkElement.textContent = stylesheet;
  1792. return linkElement;
  1793. }
  1794. function getAnchorElement(containerElement) {
  1795. 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 + ",") + "]")
  1796. || document.documentElement;
  1797. }
  1798. function addNoteRef(anchorElement, noteId) {
  1799. const noteRefs = getNoteRefs(anchorElement);
  1800. noteRefs.push(noteId);
  1801. setNoteRefs(anchorElement, noteRefs);
  1802. }
  1803. function deleteNoteRef(containerElement, noteId) {
  1804. const anchorElement = getAnchorElement(containerElement);
  1805. const noteRefs = getNoteRefs(anchorElement).filter(noteRefs => noteRefs != noteId);
  1806. if (noteRefs.length) {
  1807. setNoteRefs(anchorElement, noteRefs);
  1808. } else {
  1809. delete anchorElement.dataset.singleFileNoteRefs;
  1810. }
  1811. }
  1812. function getNoteRefs(anchorElement) {
  1813. return JSON.parse("[" + (anchorElement.dataset.singleFileNoteRefs || "") + "]");
  1814. }
  1815. function setNoteRefs(anchorElement, noteRefs) {
  1816. anchorElement.dataset.singleFileNoteRefs = noteRefs.toString();
  1817. }
  1818. function minifyText(text) {
  1819. return text.replace(/[\n\t\s]+/g, " ");
  1820. }
  1821. function isAncestor(element, otherElement) {
  1822. return otherElement.parentElement && (element == otherElement.parentElement || isAncestor(element, otherElement.parentElement));
  1823. }
  1824. function getShadowRoot(element) {
  1825. const chrome = window.chrome;
  1826. if (element.openOrClosedShadowRoot) {
  1827. return element.openOrClosedShadowRoot;
  1828. } else if (chrome && chrome.dom && chrome.dom.openOrClosedShadowRoot) {
  1829. try {
  1830. return chrome.dom.openOrClosedShadowRoot(element);
  1831. } catch (error) {
  1832. return element.shadowRoot;
  1833. }
  1834. } else {
  1835. return element.shadowRoot;
  1836. }
  1837. }
  1838. function detectSavedPage(document) {
  1839. const firstDocumentChild = document.documentElement.firstChild;
  1840. return firstDocumentChild.nodeType == Node.COMMENT_NODE &&
  1841. (firstDocumentChild.textContent.includes(COMMENT_HEADER) || firstDocumentChild.textContent.includes(COMMENT_HEADER_LEGACY));
  1842. }
  1843. })(typeof globalThis == "object" ? globalThis : window);