outline.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import jsonParse from "./jsonParse"
  2. export default () => {
  3. let jsonAST = {}
  4. let path = []
  5. const init = (jsonStr) => {
  6. jsonAST = jsonParse(jsonStr)
  7. linkParents(jsonAST)
  8. }
  9. const setNewText = (jsonStr) => {
  10. init(jsonStr)
  11. path = []
  12. }
  13. const linkParents = (node) => {
  14. if (node.kind === "Object") {
  15. if (node.members) {
  16. node.members.forEach((m) => {
  17. m.parent = node
  18. linkParents(m)
  19. })
  20. }
  21. } else if (node.kind === "Array") {
  22. if (node.values) {
  23. node.values.forEach((v) => {
  24. v.parent = node
  25. linkParents(v)
  26. })
  27. }
  28. } else if (node.kind === "Member") {
  29. if (node.value) {
  30. node.value.parent = node
  31. linkParents(node.value)
  32. }
  33. }
  34. }
  35. const genPath = (index) => {
  36. let output = {}
  37. path = []
  38. let current = jsonAST
  39. if (current.kind === "Object") {
  40. path.push({ label: "{}", obj: "root" })
  41. } else if (current.kind === "Array") {
  42. path.push({ label: "[]", obj: "root" })
  43. }
  44. let over = false
  45. try {
  46. while (!over) {
  47. if (current.kind === "Object") {
  48. let i = 0
  49. let found = false
  50. while (i < current.members.length) {
  51. const m = current.members[i]
  52. if (m.start <= index && m.end >= index) {
  53. path.push({ label: m.key.value, obj: m })
  54. current = current.members[i]
  55. found = true
  56. break
  57. }
  58. i++
  59. }
  60. if (!found) over = true
  61. } else if (current.kind === "Array") {
  62. if (current.values) {
  63. let i = 0
  64. let found = false
  65. while (i < current.values.length) {
  66. const m = current.values[i]
  67. if (m.start <= index && m.end >= index) {
  68. path.push({ label: `[${i.toString()}]`, obj: m })
  69. current = current.values[i]
  70. found = true
  71. break
  72. }
  73. i++
  74. }
  75. if (!found) over = true
  76. } else over = true
  77. } else if (current.kind === "Member") {
  78. if (current.value) {
  79. if (current.value.start <= index && current.value.end >= index) {
  80. current = current.value
  81. } else over = true
  82. } else over = true
  83. } else if (
  84. current.kind === "String" ||
  85. current.kind === "Number" ||
  86. current.kind === "Boolean" ||
  87. current.kind === "Null"
  88. ) {
  89. if (current.start <= index && current.end >= index) {
  90. path.push({ label: `${current.value}`, obj: current })
  91. }
  92. over = true
  93. }
  94. }
  95. output = { success: true, res: path.map((p) => p.label) }
  96. } catch (e) {
  97. output = { success: false, res: e }
  98. }
  99. return output
  100. }
  101. const getSiblings = (index) => {
  102. const parent = path[index]?.obj?.parent
  103. if (!parent) return []
  104. else if (parent.kind === "Object") {
  105. return parent.members
  106. } else if (parent.kind === "Array") {
  107. return parent.values
  108. } else return []
  109. }
  110. return {
  111. init,
  112. genPath,
  113. getSiblings,
  114. setNewText,
  115. }
  116. }