Desktop markdown wiki app. Built with node, Electron Framework and AngularJS.

note-view-ctrl.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @ngdoc function
  3. * @name domainManagerApp.controller:AboutCtrl
  4. * @description
  5. * # AboutCtrl
  6. * Controller of the domainManagerApp
  7. */
  8. angular.module('codexApp.noteView', [])
  9. .controller('NoteViewCtrl',['$scope', '$rootScope', '$state', 'FileService', function ($scope, $rootScope, $state, FileService) {
  10. var marked = require('marked');
  11. marked.setOptions({
  12. renderer: new marked.Renderer(),
  13. gfm: true,
  14. tables: true,
  15. breaks: false,
  16. pedantic: true,
  17. sanitize: false,
  18. smartLists: true,
  19. smartypants: true
  20. });
  21. var filesystem = require("fs");
  22. console.log('-> Note View opened!')
  23. $scope.note = FileService.getCurrentNote();
  24. $scope.container = "note-container";
  25. $scope.html_data = "";
  26. $scope.loadNoteView = function() {
  27. filesystem.readFile($scope.note.path, function(err, data) {
  28. $scope.note.data = String.fromCharCode.apply(null, data)
  29. if(!$scope.$$phase) {
  30. $scope.$apply(function(){
  31. $scope.html_data = marked($scope.note.data);
  32. });
  33. } else {
  34. $scope.html_data = marked($scope.note.data);
  35. }
  36. //console.log($scope.raw_data);
  37. $scope.fixImgURLs($scope.note.path, $scope.html_data);
  38. var a = document.getElementsByTagName('a'), ajax;
  39. for (var i=0; i<a.length; ++i) {
  40. a[i].addEventListener('click', handleAnchor, false);
  41. }
  42. function handleAnchor(e){
  43. e.preventDefault();
  44. var r = new RegExp('^(?:[a-z]+:)?//', 'i');
  45. if(e.srcElement.protocol == "http:"){
  46. console.log("-> Prevented link from opening: " + e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
  47. }
  48. if(e.srcElement.protocol == "file:"){
  49. var current_note = FileService.getCurrentNote().path;
  50. var relative_path = e.srcElement.outerHTML.match(/href="([^"]*)/)[1];
  51. var fixed_url = $scope.fixRelativeURL(current_note, relative_path);
  52. var note = FileService.getNote(fixed_url);
  53. FileService.setCurrentNote(note);
  54. $scope.note = note;
  55. console.log("-> Opening Link: " + note.path)
  56. $scope.loadNoteView();
  57. }
  58. }
  59. function updateContent() {
  60. // Do something with `this.responseText`
  61. }
  62. });
  63. }
  64. $scope.loadNoteView();
  65. $rootScope.$on('note-view:reload', function() {
  66. $scope.note = FileService.getCurrentNote();
  67. $scope.html_data = "";
  68. $scope.loadNoteView();
  69. });
  70. $scope.fixImgURLs = function(current_note_path, html){
  71. // var images = html.getElementsByTagName('img');
  72. // var srcList = [];
  73. // for(var i = 0; i < images.length; i++) {
  74. // console.log(images[i]);
  75. // images[i].src = $scope.fixRelativeURL(current_note_path, images[i].src);
  76. // }
  77. // page = angular.element(html);
  78. // console.log(page.find("img"))
  79. // page.find("img").each(function() {
  80. // var obj = angular.element(this);
  81. // console.log(page)
  82. // console.log(obj)
  83. // obj.attr('src') = $scope.fixRelativeURL(current_note_path, obj.attr('src'));
  84. // console.log(obj.attr('src'));
  85. // });
  86. var imgs = angular.element(html).find("img");
  87. var img_urls = []
  88. for (var i = 0; i < imgs.length; i++) {
  89. img_urls.push($scope.fixRelativeURL(current_note_path, $scope.absoluteToRelativeURL(current_note_path, imgs[i].src)));
  90. }
  91. var page_images = document.getElementsByTagName('img');
  92. console.log("-> Changing "+ img_urls.length + " images")
  93. console.log(page_images)
  94. for(var i = 0; i < img_urls.length; i++) {
  95. console.log(page_images[i]);
  96. page_images[i].src = img_urls[i];
  97. }
  98. }
  99. $scope.fixRelativeURL = function(current_url, relative_url) {
  100. console.log("-> Fixing URL")
  101. console.log(" * Relative URL: " + relative_url)
  102. console.log(" * Note URL: " + current_url)
  103. // split urls and create arrays
  104. var current_path = current_url.split('/');
  105. var relative_path = relative_url.split('/');
  106. // remove the current note's filename from the url
  107. current_path.pop();
  108. // count how many folders the relative path goes back and erase '..'
  109. var count = 0;
  110. for (var i = 0; i < relative_path.length; i++) {
  111. if(relative_path[i] == ".."){
  112. count = count + 1;
  113. relative_path[i] = "";
  114. }
  115. }
  116. // make the relative path a string again
  117. relative_path = relative_path.join('/');
  118. // remove the same count of folders from the end of the current notes url
  119. for (var i = 0; i < count; i++) {
  120. current_path.pop();
  121. }
  122. // make the current note's url a string again
  123. current_path = current_path.join('/');
  124. // add a '/' if the relative url pointed to a file or folder above the current notes root
  125. if(count == 0){
  126. var fixed_url = current_path + "/" + relative_path;
  127. } else {
  128. var fixed_url = current_path + relative_path;
  129. }
  130. // return the fixed relative url
  131. console.log(" * Fixed URL: " + fixed_url)
  132. return fixed_url;
  133. }
  134. $scope.absoluteToRelativeURL = function(current_url, absolute_url) {
  135. console.log("-> Converting absolute URL to relative")
  136. console.log(" * Absolute URL: " + absolute_url)
  137. console.log(" * Note URL: " + current_url)
  138. // split urls and create arrays
  139. var current_path = current_url.split('/');
  140. var absolute_path = $scope.getUrlParts(absolute_url).pathname.split('/');
  141. // remove the current note's filename from the url and the image filename from the url
  142. current_path.pop();
  143. current_path.shift();
  144. absolute_path.shift();
  145. // count how many folders the current path has
  146. var current_path_count = 0;
  147. for (var i = 0; i < current_path.length; i++) {
  148. current_path_count = current_path_count + 1;
  149. }
  150. // count how many folders the absolute path has
  151. var absolute_path_count = 0;
  152. for (var i = 0; i < absolute_path.length; i++) {
  153. absolute_path_count = absolute_path_count + 1;
  154. }
  155. absolute_path_count = absolute_path_count - 1;
  156. console.log(" * Cleaned current URL (" + current_path_count + " parts): " + current_path.join('/'))
  157. console.log(" * Cleaned absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  158. dif = current_path_count - (absolute_path_count -1);
  159. for (var i = 0; i < absolute_path_count; i++) {
  160. absolute_path.shift();
  161. }
  162. console.log(" * Modified current URL (" + current_path_count + " parts): " + current_path.join('/'))
  163. console.log(" * Modified absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  164. // make the relative path a string again
  165. var relative_path = absolute_path.join('/');
  166. console.log(" * Converted relative URL: " + relative_path)
  167. return relative_path;
  168. }
  169. $scope.getUrlParts = function(url) {
  170. var a = document.createElement('a');
  171. a.href = url;
  172. return {
  173. href: a.href,
  174. host: a.host,
  175. hostname: a.hostname,
  176. port: a.port,
  177. pathname: a.pathname,
  178. protocol: a.protocol,
  179. hash: a.hash,
  180. search: a.search
  181. };
  182. }
  183. }]);