You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.1 KiB

  1. 'use strict';
  2. angular.module('app.write', ['ngRoute'])
  3. .config(['$routeProvider', function($routeProvider) {
  4. $routeProvider.when('/write', {
  5. templateUrl: 'views/write/write.html',
  6. controller: 'WriteCtrl'
  7. });
  8. }])
  9. .controller('WriteCtrl', function($scope, $rootScope, $http, toastr) {
  10. $scope.post = {
  11. title: "",
  12. content: "",
  13. summary: ""
  14. };
  15. var editor = new MediumEditor('.editable', {
  16. toolbar: {
  17. /* These are the default options for the toolbar,
  18. if nothing is passed this is what is used */
  19. allowMultiParagraphSelection: true,
  20. buttons: ['bold', 'italic', 'underline', 'anchor', 'h2', 'h3', 'quote'],
  21. diffLeft: 0,
  22. diffTop: -10,
  23. firstButtonClass: 'medium-editor-button-first',
  24. lastButtonClass: 'medium-editor-button-last',
  25. relativeContainer: null,
  26. standardizeSelectionStart: false,
  27. static: false,
  28. /* options which only apply when static is true */
  29. align: 'center',
  30. sticky: false,
  31. updateOnEmptySelection: false
  32. },
  33. autoLink: true,
  34. placeholder: {
  35. text: 'Start to writting your decentralized blog post...'
  36. }
  37. }).subscribe('editableInput', function(event, editable) {
  38. // Do some work
  39. $scope.post.content = editable.innerHTML;
  40. });
  41. $scope.publicate = function() {
  42. //add the thumbimg to ipfs
  43. var formdata = new FormData();
  44. formdata.append("file", $scope.file);
  45. $http({
  46. url: ipfsurl + 'add',
  47. method: "POST",
  48. headers: {
  49. "Content-Type": undefined
  50. },
  51. data: formdata
  52. })
  53. .then(function(data) {
  54. console.log("data: ");
  55. console.log(data.data);
  56. $scope.post.thumbimg = data.data.Hash;
  57. toastr.success("Thumb image added to IPFS");
  58. $scope.publicate2();
  59. },
  60. function(data) {
  61. console.log(data);
  62. toastr.error("Error adding Model to IPFS");
  63. });
  64. };
  65. $scope.publicate2 = function() {
  66. //$scope.post.thumbimg = hash;
  67. //send the post content to the Go api, that will add the html content to ipfs
  68. console.log("post", $scope.post);
  69. $http({
  70. url: apiurl + 'post',
  71. method: "POST",
  72. headers: {
  73. "Content-Type": undefined
  74. },
  75. data: $scope.post
  76. })
  77. .then(function(data) {
  78. console.log("data: ");
  79. console.log(data.data);
  80. window.location = "#!/main";
  81. },
  82. function(data) {
  83. console.log(data);
  84. });
  85. };
  86. }).directive('fileModel', ['$parse', function($parse) {
  87. //directive code from https://www.tutorialspoint.com/angularjs/angularjs_upload_file.htm
  88. return {
  89. restrict: 'A',
  90. link: function(scope, element, attrs) {
  91. var model = $parse(attrs.fileModel);
  92. var modelSetter = model.assign;
  93. element.bind('change', function() {
  94. scope.$apply(function() {
  95. modelSetter(scope, element[0].files[0]);
  96. });
  97. });
  98. }
  99. };
  100. }]);