O2MindMapCanvasController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. //
  2. // O2MindMapCanvasController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/12/17.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. import Promises
  11. class O2MindMapCanvasController: UIViewController {
  12. private lazy var viewModel: O2MindMapViewModel = {
  13. return O2MindMapViewModel()
  14. }()
  15. var id: String? // 脑图id
  16. private var mindMapItem: MindMapItem? // 脑图对象
  17. private var root: MindRootNode? // 脑图具体内容的对象
  18. private var canvas: O2MindMapCanvasView? // 脑图画布
  19. // 底部工具栏
  20. private var bottomBar: O2MindMapCanvasBottomBar?
  21. // 点击画布
  22. private var selectedNode: MindNodeData? = nil
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. self.title = "脑图"
  26. self.view.backgroundColor = O2MindMapCanvasView.canvasBgColor // 背景色
  27. self.loadMindMap()
  28. }
  29. private func addSavebtn() {
  30. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(saveMindMap))
  31. }
  32. // MARK: - 事件
  33. // 双指缩放
  34. @objc private func scale(sender: UIPinchGestureRecognizer?) {
  35. if let scale = sender?.scale {
  36. if self.canvas != nil {
  37. self.canvas!.transform = self.canvas!.transform.scaledBy(x: scale, y: scale)
  38. }
  39. // 缩放完成后重置
  40. sender?.scale = 1
  41. }
  42. }
  43. // 移动画布
  44. @objc private func move(sender: UIPanGestureRecognizer? ) {
  45. if let transation = sender?.translation(in: self.view), let oldCenter = sender?.view?.center {
  46. let newCenter = CGPoint(x: oldCenter.x + transation.x, y: oldCenter.y + transation.y)
  47. let canvansH = self.canvas?.frame.height ?? 0
  48. var maxY:CGFloat = 0
  49. if canvansH >= self.view.frame.height {
  50. maxY = (canvansH - self.view.frame.height) / 2
  51. }
  52. // let maxY = (canvansH / 2) + self.view.frame.height
  53. var newCenterY = CGFloat.minimum(newCenter.y, maxY + (self.view.frame.height / 2))
  54. newCenterY = CGFloat.maximum((self.view.frame.height / 2) - maxY, newCenterY)
  55. let canvansW = self.canvas?.frame.width ?? 0
  56. var maxX:CGFloat = 0
  57. if canvansW > self.view.frame.width {
  58. maxX = (canvansW - self.view.frame.width) / 2
  59. }
  60. // let maxX = (canvansW / 2) + self.view.frame.width
  61. var newCenterX = CGFloat.minimum(newCenter.x, maxX + (self.view.frame.width / 2))
  62. newCenterX = CGFloat.maximum((self.view.frame.width / 2) - maxX , newCenterX)
  63. sender?.view?.center = CGPoint(x: newCenterX, y: newCenterY)
  64. // 移动完成后归零
  65. sender?.setTranslation(CGPoint.zero, in: self.view)
  66. }
  67. }
  68. @objc private func clickCanvas(sender: UITapGestureRecognizer?) {
  69. if let point = sender?.location(in: sender?.view) {
  70. self.selectedNode = self.canvas?.clickCanvasWithPoint(point: point)
  71. // 当前脑图是否可编辑
  72. if self.mindMapItem?.editable == true {
  73. if self.selectedNode != nil {
  74. self.bottomBar?.show(isRoot: self.selectedNode?.isRoot() ?? false)
  75. } else {
  76. self.bottomBar?.hide()
  77. }
  78. } else {
  79. DDLogError("当前用户不可编辑")
  80. }
  81. }
  82. }
  83. // MARK: - 画布相关处理
  84. ///
  85. /// 绘画脑图
  86. private func painMindMap() {
  87. DDLogDebug("开始绘画脑图。。。\(SCREEN_WIDTH) \(SCREEN_HEIGHT)")
  88. if let root = root {
  89. self.canvas = O2MindMapCanvasView() // 这里直接new ??
  90. if let paint = self.canvas!.resolveData(json: root) {
  91. let size = paint.1
  92. DDLogDebug("canvas size \(size)")
  93. var x: CGFloat = 0
  94. var y: CGFloat = 0
  95. if size.width > SCREEN_WIDTH {
  96. x = 1 - ((size.width - SCREEN_WIDTH ) / 2)
  97. }
  98. if size.height > SCREEN_HEIGHT {
  99. y = 1 - ((size.height - SCREEN_HEIGHT) / 2)
  100. }
  101. self.view.addSubview(canvas!)
  102. canvas!.backgroundColor = O2MindMapCanvasView.canvasBgColor // 背景色
  103. canvas!.frame = CGRect(x: x, y: y, width: size.width, height: size.height)
  104. canvas!.repaintContent(node: paint.0)
  105. let pinch = UIPinchGestureRecognizer(target: self, action: #selector(scale))
  106. canvas!.addGestureRecognizer(pinch)
  107. let move = UIPanGestureRecognizer(target: self, action: #selector(move))
  108. canvas!.addGestureRecognizer(move)
  109. canvas!.addTapGesture(target: self, action: #selector(clickCanvas))
  110. }
  111. // 添加底部工具栏
  112. self.bottomBar = O2MindMapCanvasBottomBar.newBar(y: self.view.frame.height)
  113. self.view.addSubview(self.bottomBar!)
  114. self.bottomBar?.delegate = self
  115. } else {
  116. DDLogError("脑图内容为空!!!!")
  117. }
  118. }
  119. // 数据变化 重新绘制
  120. private func notifyDataChanged() {
  121. if let root = root {
  122. // 重新计算大小
  123. if let paint = self.canvas!.resolveData(json: root) {
  124. let size = paint.1
  125. var x: CGFloat = 0
  126. var y: CGFloat = 0
  127. if size.width > SCREEN_WIDTH {
  128. x = 1 - ((size.width - SCREEN_WIDTH ) / 2)
  129. }
  130. if size.height > SCREEN_HEIGHT {
  131. y = 1 - ((size.height - SCREEN_HEIGHT) / 2)
  132. }
  133. // 修改画布大小
  134. canvas!.frame = CGRect(x: x, y: y, width: size.width, height: size.height)
  135. // 重新绘画脑图
  136. canvas!.repaintContent(node: paint.0)
  137. if self.selectedNode != nil {
  138. self.moveSelectedNode2Center()
  139. }
  140. }
  141. }
  142. }
  143. // 移动画布 让选中的节点移动到中心
  144. private func moveSelectedNode2Center() {
  145. if self.selectedNode == nil {
  146. DDLogError("没有选中的节点,无法移动")
  147. return
  148. }
  149. if let selectP = self.canvas?.getSelectNodePosition(), let selectNodeP = selectP.0, let rootNodeP = selectP.1 {
  150. let selectPoint = CGPoint(x: (selectNodeP.nodeBoxRect.origin.x + selectNodeP.nodeBoxRect.width / 2), y: selectNodeP.nodeBoxRect.origin.y + selectNodeP.nodeBoxRect.height / 2)
  151. let selectViewPoint = self.canvas!.convert(selectPoint, to: self.view)
  152. let rootPoint = CGPoint(x: (rootNodeP.nodeBoxRect.origin.x + rootNodeP.nodeBoxRect.width / 2), y: rootNodeP.nodeBoxRect.origin.y + rootNodeP.nodeBoxRect.height / 2)
  153. let rootViewPoint = self.canvas!.convert(rootPoint, to: self.view)
  154. var newX = (2 * rootViewPoint.x - selectViewPoint.x)
  155. var newY = (2 * rootViewPoint.y - selectViewPoint.y)
  156. DDLogInfo("新的 x : \(newX) y: \(newY)")
  157. let maxY = self.view.frame.height / 2 + (self.canvas!.frame.height - self.view.frame.height) / 2
  158. let minY = self.view.frame.height / 2 - (self.canvas!.frame.height - self.view.frame.height) / 2
  159. let maxX = self.view.frame.width / 2 + (self.canvas!.frame.width - self.view.frame.width) / 2
  160. let minX = self.view.frame.width / 2 - (self.canvas!.frame.width - self.view.frame.width) / 2
  161. if newX > maxX {
  162. newX = maxX
  163. }
  164. if newX < minX {
  165. newX = minX
  166. }
  167. if newY > maxY {
  168. newY = maxY
  169. }
  170. if newY < minY {
  171. newY = minY
  172. }
  173. let newCenter = CGPoint(x: newX, y: newY)
  174. DDLogInfo("新的 root节点的中心点 外部位置 : \(newCenter)")
  175. self.canvas?.center = newCenter
  176. } else {
  177. DDLogError("没有选中的节点或root节点")
  178. }
  179. }
  180. // MARK: - 后端请求相关
  181. /// 请求脑图数据
  182. private func loadMindMap() {
  183. if let viewId = id, !viewId.isBlank {
  184. self.showLoading()
  185. self.viewModel.loadMindMapView(id: viewId).then { (item, node) in
  186. self.hideLoading()
  187. self.mindMapItem = item
  188. self.title = item.name // 修改标题
  189. self.root = node
  190. self.painMindMap()
  191. if self.mindMapItem?.editable == true {
  192. self.addSavebtn()
  193. }
  194. }.catch { err in
  195. self.showError(title: "请求脑图数据错误!")
  196. }
  197. } else {
  198. DDLogError("错误,没有脑图id,无法获取数据!!!!")
  199. }
  200. }
  201. // 保存脑图 结束后更新预览图
  202. @objc private func saveMindMap() {
  203. if let root = self.root,let item = self.mindMapItem, let id = item.id {
  204. // 需要先保存缩略图 然后把id放入MindMapItem中 然后保存对象
  205. self.showLoading()
  206. if let image = self.canvas?.saveAsImage() {
  207. self.viewModel.saveMindMapThumb(image: image, id: id).then { imgId -> Promise<String> in
  208. DDLogInfo("保存缩略图成功!\(imgId)")
  209. let content = root.toJSONString() ?? ""
  210. item.content = content
  211. item.fileVersion = item.fileVersion ?? 0 + 1
  212. item.icon = imgId
  213. return self.viewModel.saveMindMap(mind: item)
  214. }.then { _ in
  215. self.showSuccess(title: "保存成功!")
  216. self.mindMapItem = item
  217. }.catch { err in
  218. DDLogError(err.localizedDescription)
  219. self.showError(title: "保存失败!")
  220. }
  221. }
  222. }
  223. }
  224. // MARK: - 节点工具栏操作
  225. // 创建子节点
  226. private func createSubNode() {
  227. DDLogDebug("创建子节点")
  228. if self.selectedNode == nil {
  229. DDLogError("请先选择节点!!")
  230. return
  231. }
  232. self.showPromptAlert(title: "创建子节点", message: "请输入节点内容", inputText: "") { action, result in
  233. if result == "" || result.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
  234. self.showError(title: "请输入节点内容!")
  235. return
  236. }
  237. if let node = self.root?.root {
  238. let newNode = self.createSubNodeWithTextRecursion(node: node, text: result)
  239. self.root?.root = newNode
  240. self.notifyDataChanged()
  241. } else {
  242. DDLogError("脑图数据对象不存在,无法创建。。。")
  243. }
  244. }
  245. }
  246. // 创建新的子节点到对象中
  247. private func createSubNodeWithTextRecursion(node: MindNode, text:String)-> MindNode {
  248. if node.data?.id != nil && node.data?.id == self.selectedNode?.id {
  249. let newNode = MindNode()
  250. let newData = MindNodeData()
  251. newData.text = text
  252. let time = Date().milliStamp
  253. newData.id = "mind_\(time)"
  254. newNode.data = newData
  255. newNode.children = []
  256. node.children?.append(newNode)
  257. self.selectedNode = newData
  258. self.canvas?.reSelected(newSelected: newData)
  259. } else {
  260. if let nChild = node.children {
  261. var children: [MindNode] = []
  262. for item in nChild {
  263. let newChild = self.createSubNodeWithTextRecursion(node: item, text: text)
  264. children.append(newChild)
  265. }
  266. node.children = children
  267. }
  268. }
  269. return node
  270. }
  271. // 创建同级节点
  272. private func createSameLevelNode() {
  273. DDLogDebug("创建同级节点")
  274. if self.selectedNode == nil {
  275. DDLogError("请先选择节点!!")
  276. return
  277. }
  278. self.showPromptAlert(title: "创建同级节点", message: "请输入节点内容", inputText: "") { action, result in
  279. if result == "" || result.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
  280. self.showError(title: "请输入节点内容!")
  281. return
  282. }
  283. if let node = self.root?.root {
  284. let newNode = self.createSameLevelNodeWithTextRecursion(node: node, text: result)
  285. self.root?.root = newNode
  286. self.notifyDataChanged()
  287. } else {
  288. DDLogError("脑图数据对象不存在,无法创建同级节点。。。")
  289. }
  290. }
  291. }
  292. // 创建新的同级节点到对象中
  293. private func createSameLevelNodeWithTextRecursion(node: MindNode, text:String)-> MindNode {
  294. if let nChild = node.children {
  295. var children: [MindNode] = []
  296. for item in nChild {
  297. if item.data?.id != nil && item.data?.id == self.selectedNode?.id {
  298. let newNode = MindNode()
  299. let newData = MindNodeData()
  300. newData.text = text
  301. let time = Date().milliStamp
  302. newData.id = "mind_\(time)"
  303. newNode.data = newData
  304. newNode.children = []
  305. children.append(newNode)
  306. self.selectedNode = newData
  307. self.canvas?.reSelected(newSelected: newData)
  308. // 忘了添加兄弟节点
  309. children.append(item)
  310. } else {
  311. let newChild = self.createSubNodeWithTextRecursion(node: item, text: text)
  312. children.append(newChild)
  313. }
  314. }
  315. node.children = children
  316. }
  317. return node
  318. }
  319. // 修改节点文字
  320. private func updateNodeText() {
  321. DDLogDebug("修改节点文字内容")
  322. if self.selectedNode == nil {
  323. DDLogError("请先选择节点!!")
  324. return
  325. }
  326. self.showPromptAlert(title: "修改节点内容", message: "请输入节点内容", inputText: self.selectedNode?.text ?? "") { action, result in
  327. if result == "" || result.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
  328. self.showError(title: "请输入节点内容!")
  329. return
  330. }
  331. if let node = self.root?.root {
  332. let newNode = self.updateNodeTextRecursion(node: node, text: result)
  333. self.root?.root = newNode
  334. self.notifyDataChanged()
  335. } else {
  336. DDLogError("脑图数据对象不存在,无法修改节点文字")
  337. }
  338. }
  339. }
  340. private func updateNodeTextRecursion(node: MindNode, text:String)-> MindNode {
  341. if node.data?.id != nil && node.data?.id == self.selectedNode?.id {
  342. let newData = node.data!
  343. newData.text = text
  344. node.data = newData
  345. self.selectedNode = newData
  346. self.canvas?.reSelected(newSelected: newData)
  347. } else {
  348. if let nChild = node.children {
  349. var children: [MindNode] = []
  350. for item in nChild {
  351. let newChild = self.updateNodeTextRecursion(node: item, text: text)
  352. children.append(newChild)
  353. }
  354. node.children = children
  355. }
  356. }
  357. return node
  358. }
  359. // 删除节点
  360. private func deleteNode() {
  361. DDLogDebug("删除选中的节点")
  362. if self.selectedNode == nil {
  363. DDLogError("请先选择节点!!")
  364. return
  365. }
  366. self.showDefaultConfirm(title: "提示", message: "确定要删除【\(self.selectedNode?.text ?? "")】这个节点吗") { action in
  367. if let node = self.root?.root {
  368. let newNode = self.deleteSelectNodeRecursion(node: node)
  369. self.root?.root = newNode
  370. self.notifyDataChanged()
  371. } else {
  372. DDLogError("脑图数据对象不存在,无法删除")
  373. }
  374. }
  375. }
  376. private func deleteSelectNodeRecursion(node: MindNode)-> MindNode {
  377. if let nChild = node.children {
  378. var children: [MindNode] = []
  379. for item in nChild {
  380. if item.data?.id != nil && item.data?.id == self.selectedNode?.id {
  381. // 找到后清除选中的节点
  382. self.selectedNode = nil
  383. self.canvas?.reSelected(newSelected: nil)
  384. } else {
  385. let newChild = self.deleteSelectNodeRecursion(node: item)
  386. children.append(newChild)
  387. }
  388. }
  389. node.children = children
  390. }
  391. return node
  392. }
  393. // 节点设置超链接
  394. private func addLink() {
  395. DDLogDebug("添加超链接")
  396. if self.selectedNode == nil {
  397. DDLogError("请先选择节点!!")
  398. return
  399. }
  400. let dialog = O2MindMapAddLinkDialog.mindMapNodeLinkDialog(link: self.selectedNode?.hyperlink, linkTitle: self.selectedNode?.hyperlinkTitle, delegate: self)
  401. dialog.show()
  402. }
  403. private func updateNodeLink(link:String, linkTitle: String) {
  404. if let node = self.root?.root {
  405. let newNode = self.updateNodeLinkRecursion(node: node, link: link, linkTitle: linkTitle)
  406. self.root?.root = newNode
  407. self.notifyDataChanged()
  408. } else {
  409. DDLogError("脑图数据对象不存在,无法修改超链接")
  410. }
  411. }
  412. private func updateNodeLinkRecursion(node: MindNode, link:String, linkTitle: String)-> MindNode {
  413. if node.data?.id != nil && node.data?.id == self.selectedNode?.id {
  414. let newData = node.data!
  415. newData.hyperlink = link
  416. newData.hyperlinkTitle = linkTitle
  417. node.data = newData
  418. self.selectedNode = newData
  419. self.canvas?.reSelected(newSelected: newData)
  420. } else {
  421. if let nChild = node.children {
  422. var children: [MindNode] = []
  423. for item in nChild {
  424. let newChild = self.updateNodeLinkRecursion(node: item, link: link, linkTitle: linkTitle)
  425. children.append(newChild)
  426. }
  427. node.children = children
  428. }
  429. }
  430. return node
  431. }
  432. private func addIcon() {
  433. DDLogDebug("添加图标")
  434. if self.selectedNode == nil {
  435. DDLogError("请先选择节点!!")
  436. return
  437. }
  438. let dialog = O2MindMapAddIconDialog.mindMapNodeIconDialog(progress: self.selectedNode?.progress, priority: self.selectedNode?.priority, delegate: self)
  439. dialog.show()
  440. }
  441. private func updateNodeIcons(progress:Int?, priority:Int?) {
  442. if let node = self.root?.root {
  443. let newNode = self.updateNodeIconsRecursion(node: node, progress: progress, priority: priority)
  444. self.root?.root = newNode
  445. self.notifyDataChanged()
  446. } else {
  447. DDLogError("脑图数据对象不存在,无法修改Icons。")
  448. }
  449. }
  450. private func updateNodeIconsRecursion(node: MindNode, progress:Int?, priority:Int?)-> MindNode {
  451. if node.data?.id != nil && node.data?.id == self.selectedNode?.id {
  452. let newData = node.data!
  453. newData.progress = progress
  454. newData.priority = priority
  455. node.data = newData
  456. self.selectedNode = newData
  457. self.canvas?.reSelected(newSelected: newData)
  458. } else {
  459. if let nChild = node.children {
  460. var children: [MindNode] = []
  461. for item in nChild {
  462. let newChild = self.updateNodeIconsRecursion(node: item, progress: progress, priority: priority)
  463. children.append(newChild)
  464. }
  465. node.children = children
  466. }
  467. }
  468. return node
  469. }
  470. // 节点添加图片
  471. private func addImage() {
  472. DDLogDebug("添加图片")
  473. if self.selectedNode == nil {
  474. DDLogError("请先选择节点!!")
  475. return
  476. }
  477. var hasImage = false
  478. if let _ = self.selectedNode?.image {
  479. hasImage = true
  480. }
  481. var menus :[UIAlertAction] = []
  482. if hasImage {
  483. let deleteBtn = UIAlertAction(title: "删除图片", style: .destructive) { (ok) in
  484. DDLogInfo("删除图片")
  485. self.deleteImage()
  486. }
  487. menus.append(deleteBtn)
  488. }
  489. let chooseImg = UIAlertAction(title: "选择图片", style: .default) { (ok) in
  490. DDLogInfo("选择图片")
  491. self.chooseImageForNode()
  492. }
  493. menus.append(chooseImg)
  494. let camera = UIAlertAction(title: "拍照", style: .default) { (ok) in
  495. DDLogInfo("拍照")
  496. self.takePhotoImageForNode()
  497. }
  498. menus.append(camera)
  499. self.showSheetAction(title: "添加图片", message: "", actions: menus)
  500. }
  501. // 删除节点上的图片
  502. private func deleteImage() {
  503. if let node = self.root?.root {
  504. let newNode = self.updateNodeImageRecursion(node: node, image: nil, imageId: nil, size: nil)
  505. self.root?.root = newNode
  506. self.notifyDataChanged()
  507. } else {
  508. DDLogError("脑图数据对象不存在,无法删除图片")
  509. }
  510. }
  511. // 选择图片加入节点
  512. private func chooseImageForNode() {
  513. self.choosePhotoWithImagePicker { fileName, data in
  514. DDLogDebug("图片:\(fileName)")
  515. var size:CGSize? = nil
  516. if let image: UIImage = UIImage(data: data) {
  517. DDLogDebug("图片:\(image.size)")
  518. size = image.size
  519. }
  520. self.uploadImage(fileName: fileName, data: data, size: size)
  521. }
  522. }
  523. // 拍照加入节点
  524. private func takePhotoImageForNode() {
  525. self.takePhoto(delegate: self)
  526. }
  527. // 上传图片到服务器
  528. private func uploadImage(fileName: String, data: Data, size: CGSize?) {
  529. self.showLoading()
  530. self.viewModel
  531. .saveMindMapNodeImage(filename: fileName, data: data, id: self.mindMapItem!.id!)
  532. .then { id in
  533. self.hideLoading()
  534. // 生成url 计算size对象
  535. let urlstr = O2AuthSDK.shared.getFileDownloadUrl(fileId: id)
  536. let imageSize = self.calcSize(size: size)
  537. if let node = self.root?.root {
  538. let newNode = self.updateNodeImageRecursion(node: node, image: urlstr, imageId: id, size: imageSize)
  539. self.root?.root = newNode
  540. self.notifyDataChanged()
  541. } else {
  542. DDLogError("脑图数据对象不存在,无法删除图片")
  543. }
  544. }.catch { err in
  545. DDLogError(err.localizedDescription)
  546. self.showError(title: "图片上传失败!")
  547. }
  548. }
  549. // 计算图片大小,最大的尺寸400 等比例压缩
  550. private func calcSize(size:CGSize?)-> MindNodeImageSize? {
  551. if let s = size {
  552. let width = s.width
  553. let height = s.height
  554. var newH: CGFloat = 400
  555. if height <= 0 {
  556. newH = 400
  557. } else {
  558. newH = height
  559. }
  560. var newW: CGFloat = 400
  561. if width <= 0 {
  562. newW = 400
  563. }else {
  564. newW = width
  565. }
  566. if (height > width) {
  567. if (height > 400) {
  568. let scale = height / 400
  569. newH = 400
  570. newW = width / scale
  571. }
  572. } else {
  573. if (width > 400) {
  574. let scale = width / 400;
  575. newH = height / scale
  576. newW = 400
  577. }
  578. }
  579. let imageSize = MindNodeImageSize()
  580. imageSize.width = Int(newW)
  581. imageSize.height = Int(newH)
  582. return imageSize
  583. }
  584. return nil
  585. }
  586. // 更新图片对象到选中的节点
  587. private func updateNodeImageRecursion(node: MindNode, image:String?, imageId:String?, size: MindNodeImageSize?)-> MindNode {
  588. if node.data?.id != nil && node.data?.id == self.selectedNode?.id {
  589. let newData = node.data!
  590. newData.image = image
  591. if let _ = image {
  592. } else {
  593. newData.imageTitle = nil
  594. }
  595. newData.imageId = imageId
  596. newData.imageSize = size
  597. node.data = newData
  598. self.selectedNode = newData
  599. self.canvas?.reSelected(newSelected: newData)
  600. } else {
  601. if let nChild = node.children {
  602. var children: [MindNode] = []
  603. for item in nChild {
  604. let newChild = self.updateNodeImageRecursion(node: item, image: image, imageId: imageId, size: size)
  605. children.append(newChild)
  606. }
  607. node.children = children
  608. }
  609. }
  610. return node
  611. }
  612. }
  613. // MARK: - 点击底部工具栏按钮
  614. extension O2MindMapCanvasController: O2MindMapCanvasBottomBtnDelegate {
  615. func clickBtn(type: O2MindMapCanvasBottomBtnType) {
  616. switch type {
  617. case .createSubNode:
  618. self.createSubNode()
  619. break
  620. case .createSameLevelNode:
  621. self.createSameLevelNode()
  622. break
  623. case .editNode:
  624. self.updateNodeText()
  625. break
  626. case .deleteNode:
  627. self.deleteNode()
  628. break
  629. case .addImg:
  630. self.addImage()
  631. break
  632. case .addLink:
  633. self.addLink()
  634. break
  635. case .addIcon:
  636. self.addIcon()
  637. break
  638. }
  639. }
  640. }
  641. // MARK: - 添加超链接的delegate
  642. extension O2MindMapCanvasController: O2MindMapAddLinkDialogDelegate {
  643. func deleteLink() {
  644. DDLogInfo("删除超链接")
  645. self.updateNodeLink(link: "", linkTitle: "")
  646. }
  647. func saveLink(link: String, linkTitle: String) {
  648. DDLogInfo("保存超链接 link: \(link) linkTitle: \(linkTitle)")
  649. self.updateNodeLink(link: link, linkTitle: linkTitle)
  650. }
  651. }
  652. // MARK: - 添加图标delegate
  653. extension O2MindMapCanvasController: O2MindMapAddIconDialogDelegate {
  654. func saveIcons(progress: Int?, priority: Int?) {
  655. DDLogInfo("保存图标 progress: \(String(describing: progress)) priority: \(String(describing: priority))")
  656. self.updateNodeIcons(progress: progress, priority: priority)
  657. }
  658. }
  659. // MARK: - 拍照返回
  660. extension O2MindMapCanvasController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {
  661. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
  662. if let image = info[.editedImage] as? UIImage, let newData = image.pngData() {
  663. let fileName = "\(UUID().uuidString).png"
  664. let size = image.size
  665. self.uploadImage(fileName: fileName, data: newData, size: size)
  666. } else {
  667. DDLogError("没有选择到图片!")
  668. }
  669. picker.dismiss(animated: true, completion: nil)
  670. }
  671. }