utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import _ from 'lodash';
  2. import baseX from 'base-x';
  3. import PAKO from 'pako';
  4. import {Buffer} from 'safe-buffer';
  5. import sjclWrapper from './sjclWrapper';
  6. export const pako = PAKO;
  7. const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  8. const bs58 = baseX(BASE58);
  9. export function sleep(ms) {
  10. return new Promise(resolve => setTimeout(resolve, ms));
  11. }
  12. export function toHex(buf) {
  13. return Buffer.from(buf).toString('hex');
  14. }
  15. export function stringToHex(str) {
  16. return Buffer.from(str).toString('hex');
  17. }
  18. export function hexToString(str) {
  19. return Buffer.from(str, 'hex').toString();
  20. }
  21. export function randomArray(len) {
  22. const a = new Uint8Array(len);
  23. window.crypto.getRandomValues(a);
  24. return a;
  25. }
  26. export function randomHexString(len) {
  27. return Buffer.from(randomArray(len)).toString('hex');
  28. }
  29. export function formatDate(d, format) {
  30. if (!format)
  31. format = 'normal';
  32. switch (format) {
  33. case 'normal':
  34. return `${d.getDate().toString().padStart(2, '0')}.${(d.getMonth() + 1).toString().padStart(2, '0')}.${d.getFullYear()} ` +
  35. `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`;
  36. case 'coDate':
  37. return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
  38. case 'noDate':
  39. return `${d.getDate().toString().padStart(2, '0')}.${(d.getMonth() + 1).toString().padStart(2, '0')}.${d.getFullYear()}`;
  40. }
  41. }
  42. export function fallbackCopyTextToClipboard(text) {
  43. let textArea = document.createElement('textarea');
  44. textArea.value = text;
  45. document.body.appendChild(textArea);
  46. textArea.focus();
  47. textArea.select();
  48. let result = false;
  49. try {
  50. result = document.execCommand('copy');
  51. } catch (e) {
  52. //
  53. }
  54. document.body.removeChild(textArea);
  55. return result;
  56. }
  57. export async function copyTextToClipboard(text) {
  58. if (!navigator.clipboard) {
  59. return fallbackCopyTextToClipboard(text);
  60. }
  61. let result = false;
  62. try {
  63. await navigator.clipboard.writeText(text);
  64. result = true;
  65. } catch (e) {
  66. //
  67. }
  68. return result;
  69. }
  70. export function toBase58(data) {
  71. return bs58.encode(Buffer.from(data));
  72. }
  73. export function fromBase58(data) {
  74. return Buffer.from(bs58.decode(data));
  75. }
  76. //base-x слишком тормозит, используем sjcl
  77. export function toBase64(data) {
  78. return sjclWrapper.codec.base64.fromBits(
  79. sjclWrapper.codec.bytes.toBits(Buffer.from(data))
  80. );
  81. }
  82. //base-x слишком тормозит, используем sjcl
  83. export function fromBase64(data) {
  84. return Buffer.from(sjclWrapper.codec.bytes.fromBits(
  85. sjclWrapper.codec.base64.toBits(data)
  86. ));
  87. }
  88. export function hasProp(obj, prop) {
  89. return Object.prototype.hasOwnProperty.call(obj, prop);
  90. }
  91. export function getObjDiff(oldObj, newObj, opts = {}) {
  92. const {
  93. exclude = [],
  94. excludeAdd = [],
  95. excludeDel = [],
  96. } = opts;
  97. const ex = new Set(exclude);
  98. const exAdd = new Set(excludeAdd);
  99. const exDel = new Set(excludeDel);
  100. const makeObjDiff = (oldObj, newObj, keyPath) => {
  101. const result = {__isDiff: true, change: {}, add: {}, del: []};
  102. keyPath = `${keyPath}${keyPath ? '/' : ''}`;
  103. for (const key of Object.keys(oldObj)) {
  104. const kp = `${keyPath}${key}`;
  105. if (Object.prototype.hasOwnProperty.call(newObj, key)) {
  106. if (ex.has(kp))
  107. continue;
  108. if (!_.isEqual(oldObj[key], newObj[key])) {
  109. if (_.isObject(oldObj[key]) && _.isObject(newObj[key])) {
  110. result.change[key] = makeObjDiff(oldObj[key], newObj[key], kp);
  111. } else {
  112. result.change[key] = _.cloneDeep(newObj[key]);
  113. }
  114. }
  115. } else {
  116. if (exDel.has(kp))
  117. continue;
  118. result.del.push(key);
  119. }
  120. }
  121. for (const key of Object.keys(newObj)) {
  122. const kp = `${keyPath}${key}`;
  123. if (exAdd.has(kp))
  124. continue;
  125. if (!Object.prototype.hasOwnProperty.call(oldObj, key)) {
  126. result.add[key] = _.cloneDeep(newObj[key]);
  127. }
  128. }
  129. return result;
  130. }
  131. return makeObjDiff(oldObj, newObj, '');
  132. }
  133. export function isObjDiff(diff) {
  134. return (_.isObject(diff) && diff.__isDiff && diff.change && diff.add && diff.del);
  135. }
  136. export function isEmptyObjDiff(diff) {
  137. return (!isObjDiff(diff) ||
  138. !(Object.keys(diff.change).length ||
  139. Object.keys(diff.add).length ||
  140. diff.del.length
  141. )
  142. );
  143. }
  144. export function isEmptyObjDiffDeep(diff, opts = {}) {
  145. if (!isObjDiff(diff))
  146. return true;
  147. const {
  148. isApplyChange = true,
  149. isApplyAdd = true,
  150. isApplyDel = true,
  151. } = opts;
  152. let notEmptyDeep = false;
  153. const change = diff.change;
  154. for (const key of Object.keys(change)) {
  155. if (_.isObject(change[key]))
  156. notEmptyDeep |= !isEmptyObjDiffDeep(change[key], opts);
  157. else if (isApplyChange)
  158. notEmptyDeep = true;
  159. }
  160. return !(
  161. notEmptyDeep ||
  162. (isApplyAdd && Object.keys(diff.add).length) ||
  163. (isApplyDel && diff.del.length)
  164. );
  165. }
  166. export function applyObjDiff(obj, diff, opts = {}) {
  167. const {
  168. isAddChanged = false,
  169. isApplyChange = true,
  170. isApplyAdd = true,
  171. isApplyDel = true,
  172. } = opts;
  173. let result = _.cloneDeep(obj);
  174. if (!diff.__isDiff)
  175. return result;
  176. const change = diff.change;
  177. for (const key of Object.keys(change)) {
  178. if (Object.prototype.hasOwnProperty.call(result, key)) {
  179. if (_.isObject(change[key])) {
  180. result[key] = applyObjDiff(result[key], change[key], opts);
  181. } else {
  182. if (isApplyChange)
  183. result[key] = _.cloneDeep(change[key]);
  184. }
  185. } else if (isAddChanged) {
  186. result[key] = _.cloneDeep(change[key]);
  187. }
  188. }
  189. if (isApplyAdd) {
  190. for (const key of Object.keys(diff.add)) {
  191. result[key] = _.cloneDeep(diff.add[key]);
  192. }
  193. }
  194. if (isApplyDel && diff.del.length) {
  195. for (const key of diff.del) {
  196. delete result[key];
  197. }
  198. if (_.isArray(result))
  199. result = result.filter(v => v);
  200. }
  201. return result;
  202. }
  203. export function parseQuery(str) {
  204. if (typeof str != 'string' || str.length == 0)
  205. return {};
  206. let s = str.split('&');
  207. let s_length = s.length;
  208. let bit, query = {}, first, second;
  209. for (let i = 0; i < s_length; i++) {
  210. bit = s[i].split('=');
  211. first = decodeURIComponent(bit[0]);
  212. if (first.length == 0)
  213. continue;
  214. second = decodeURIComponent(bit[1]);
  215. if (typeof query[first] == 'undefined')
  216. query[first] = second;
  217. else
  218. if (query[first] instanceof Array)
  219. query[first].push(second);
  220. else
  221. query[first] = [query[first], second];
  222. }
  223. return query;
  224. }
  225. export function escapeXml(str) {
  226. return str.replace(/&/g, '&amp;')
  227. .replace(/</g, '&lt;')
  228. .replace(/>/g, '&gt;')
  229. .replace(/"/g, '&quot;')
  230. .replace(/'/g, '&apos;')
  231. ;
  232. }
  233. export function keyEventToCode(event) {
  234. let result = [];
  235. let code = event.code;
  236. const modCode = code.substring(0, 3);
  237. if (event.metaKey && modCode != 'Met')
  238. result.push('Meta');
  239. if (event.ctrlKey && modCode != 'Con')
  240. result.push('Ctrl');
  241. if (event.shiftKey && modCode != 'Shi')
  242. result.push('Shift');
  243. if (event.altKey && modCode != 'Alt')
  244. result.push('Alt');
  245. if (modCode == 'Dig') {
  246. code = code.substring(5, 6);
  247. } else if (modCode == 'Key') {
  248. code = code.substring(3, 4);
  249. }
  250. result.push(code);
  251. return result.join('+');
  252. }
  253. export function userHotKeysObjectSwap(userHotKeys) {
  254. let result = {};
  255. for (const [name, codes] of Object.entries(userHotKeys)) {
  256. for (const code of codes) {
  257. result[code] = name;
  258. }
  259. }
  260. return result;
  261. }
  262. export function removeHtmlTags(s) {
  263. return s.replace(/(<([^>]+)>)/ig, '');
  264. }
  265. export function makeValidFilename(filename, repl = '_') {
  266. let f = filename.replace(/[\x00\\/:*"<>|]/g, repl); // eslint-disable-line no-control-regex
  267. f = f.trim();
  268. while (f.length && (f[f.length - 1] == '.' || f[f.length - 1] == '_')) {
  269. f = f.substring(0, f.length - 1);
  270. }
  271. if (f)
  272. return f;
  273. else
  274. throw new Error('Invalid filename');
  275. }
  276. export function getBookTitle(fb2) {
  277. fb2 = (fb2 ? fb2 : {});
  278. const result = {};
  279. if (fb2.author) {
  280. const authorNames = fb2.author.map(a => _.compact([
  281. a.lastName,
  282. a.firstName,
  283. a.middleName
  284. ]).join(' '));
  285. result.author = authorNames.join(', ');
  286. }
  287. if (fb2.sequence) {
  288. const seqs = fb2.sequence.map(s => _.compact([
  289. s.name,
  290. (s.number ? `#${s.number}` : null),
  291. ]).join(' '));
  292. result.sequence = seqs.join(', ');
  293. if (result.sequence)
  294. result.sequenceTitle = `(${result.sequence})`;
  295. }
  296. result.bookTitle = _.compact([result.sequenceTitle, fb2.bookTitle]).join(' ');
  297. result.fullTitle = _.compact([
  298. result.author,
  299. result.bookTitle
  300. ]).join(' - ');
  301. return result;
  302. }
  303. export function resizeImage(dataUrl, toWidth, toHeight, quality = 0.9) {
  304. return new Promise ((resolve, reject) => { (async() => {
  305. const img = new Image();
  306. let resolved = false;
  307. img.onload = () => {
  308. try {
  309. let width = img.width;
  310. let height = img.height;
  311. if (width > height) {
  312. if (width > toWidth) {
  313. height = height * (toWidth / width);
  314. width = toWidth;
  315. }
  316. } else {
  317. if (height > toHeight) {
  318. width = width * (toHeight / height);
  319. height = toHeight;
  320. }
  321. }
  322. const canvas = document.createElement('canvas');
  323. canvas.width = width;
  324. canvas.height = height;
  325. const ctx = canvas.getContext('2d');
  326. ctx.drawImage(img, 0, 0, width, height);
  327. const result = canvas.toDataURL('image/jpeg', quality);
  328. resolved = true;
  329. resolve(result);
  330. } catch (e) {
  331. reject(e);
  332. return;
  333. }
  334. };
  335. img.onerror = reject;
  336. img.src = dataUrl;
  337. await sleep(1000);
  338. if (!resolved)
  339. reject('Не удалось изменить размер');
  340. })().catch(reject); });
  341. }