markmap-view 0.18.2 → 0.18.4-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1001,7 +1001,8 @@
1001
1001
  this.revokers.push(
1002
1002
  refreshHook.tap(() => {
1003
1003
  this.setData();
1004
- })
1004
+ }),
1005
+ () => this.observer.disconnect()
1005
1006
  );
1006
1007
  }
1007
1008
  getStyleContent() {
@@ -1144,18 +1145,43 @@
1144
1145
  }
1145
1146
  async renderData(originData) {
1146
1147
  const { paddingX, autoFit, color, maxWidth } = this.options;
1147
- if (!this.state.data) return;
1148
+ const rootNode = this.state.data;
1149
+ if (!rootNode) return;
1150
+ const nodeMap = {};
1151
+ const parentMap = {};
1148
1152
  const nodes = [];
1149
- walkTree(this.state.data, (item, next) => {
1153
+ walkTree(rootNode, (item, next, parent) => {
1150
1154
  var _a;
1151
1155
  if (!((_a = item.payload) == null ? void 0 : _a.fold)) next();
1156
+ nodeMap[item.state.id] = item;
1157
+ if (parent) parentMap[item.state.id] = parent.state.id;
1152
1158
  nodes.push(item);
1153
1159
  });
1154
- const origin = originData || this.state.data;
1155
- const originRect = origin.state.rect;
1156
- const mmG = this.g.selectAll(childSelector("g")).data(nodes, (d) => d.state.key);
1157
- const mmGEnter = mmG.enter().append("g").attr("data-depth", (d) => d.state.depth).attr("data-path", (d) => d.state.path);
1158
- const mmGExit = mmG.exit();
1160
+ const originMap = {};
1161
+ const sourceRectMap = {};
1162
+ const setOriginNode = (originNode) => {
1163
+ if (!originNode || originMap[originNode.state.id]) return;
1164
+ walkTree(originNode, (item, next) => {
1165
+ originMap[item.state.id] = originNode.state.id;
1166
+ next();
1167
+ });
1168
+ };
1169
+ const getOriginSourceRect = (node) => {
1170
+ const rect = sourceRectMap[originMap[node.state.id]];
1171
+ return rect || rootNode.state.rect;
1172
+ };
1173
+ const getOriginTargetRect = (node) => (nodeMap[originMap[node.state.id]] || rootNode).state.rect;
1174
+ sourceRectMap[rootNode.state.id] = rootNode.state.rect;
1175
+ if (originData) setOriginNode(originData);
1176
+ const mmG = this.g.selectAll(childSelector("g")).each((d) => {
1177
+ sourceRectMap[d.state.id] = d.state.rect;
1178
+ }).data(nodes, (d) => d.state.key);
1179
+ const mmGEnter = mmG.enter().append("g").attr("data-depth", (d) => d.state.depth).attr("data-path", (d) => d.state.path).each((d) => {
1180
+ setOriginNode(nodeMap[parentMap[d.state.id]]);
1181
+ });
1182
+ const mmGExit = mmG.exit().each((d) => {
1183
+ setOriginNode(nodeMap[parentMap[d.state.id]]);
1184
+ });
1159
1185
  const mmGMerge = mmG.merge(mmGEnter).attr(
1160
1186
  "class",
1161
1187
  (d) => {
@@ -1167,25 +1193,22 @@
1167
1193
  (d) => [d],
1168
1194
  (d) => d.state.key
1169
1195
  );
1170
- const mmLineEnter = mmLine.enter().append("line");
1171
- const mmLineExit = mmGExit.selectAll(
1172
- childSelector("line")
1173
- );
1196
+ const mmLineEnter = mmLine.enter().append("line").attr("stroke", (d) => color(d)).attr("stroke-width", 0);
1174
1197
  const mmLineMerge = mmLine.merge(mmLineEnter);
1175
- const mmCircle = mmGMerge.selectAll(
1176
- childSelector("circle")
1177
- ).data(
1198
+ const mmCircle = mmGMerge.selectAll(childSelector("circle")).data(
1178
1199
  (d) => {
1179
1200
  var _a;
1180
1201
  return ((_a = d.children) == null ? void 0 : _a.length) ? [d] : [];
1181
1202
  },
1182
1203
  (d) => d.state.key
1183
- ).join(
1184
- (enter) => {
1185
- return enter.append("circle").attr("stroke-width", "1.5").attr("r", 0).on("click", (e, d) => this.handleClick(e, d)).on("mousedown", stopPropagation);
1186
- },
1187
- (update) => update,
1188
- (exit) => exit.remove()
1204
+ );
1205
+ const mmCircleEnter = mmCircle.enter().append("circle").attr("stroke-width", 0).attr("r", 0).on("click", (e, d) => this.handleClick(e, d)).on("mousedown", stopPropagation);
1206
+ const mmCircleMerge = mmCircleEnter.merge(mmCircle).attr("stroke", (d) => color(d)).attr(
1207
+ "fill",
1208
+ (d) => {
1209
+ var _a;
1210
+ return ((_a = d.payload) == null ? void 0 : _a.fold) && d.children ? color(d) : "var(--markmap-circle-open-bg)";
1211
+ }
1189
1212
  );
1190
1213
  const observer = this.observer;
1191
1214
  const mmFo = mmGMerge.selectAll(childSelector("foreignObject")).data(
@@ -1216,7 +1239,14 @@
1216
1239
  );
1217
1240
  const mmPath = this.g.selectAll(childSelector("path")).data(links, (d) => d.target.state.key);
1218
1241
  const mmPathExit = mmPath.exit();
1219
- const mmPathEnter = mmPath.enter().insert("path", "g").attr("class", "markmap-link").attr("data-depth", (d) => d.target.state.depth).attr("data-path", (d) => d.target.state.path);
1242
+ const mmPathEnter = mmPath.enter().insert("path", "g").attr("class", "markmap-link").attr("data-depth", (d) => d.target.state.depth).attr("data-path", (d) => d.target.state.path).attr("d", (d) => {
1243
+ const originRect = getOriginSourceRect(d.target);
1244
+ const pathOrigin = [
1245
+ originRect.x + originRect.width,
1246
+ originRect.y + originRect.height
1247
+ ];
1248
+ return linkShape({ source: pathOrigin, target: pathOrigin });
1249
+ }).attr("stroke-width", 0);
1220
1250
  const mmPathMerge = mmPathEnter.merge(mmPath);
1221
1251
  this.svg.style(
1222
1252
  "--markmap-max-width",
@@ -1224,43 +1254,44 @@
1224
1254
  );
1225
1255
  await new Promise(requestAnimationFrame);
1226
1256
  this._relayout();
1257
+ mmGEnter.attr("transform", (d) => {
1258
+ const originRect = getOriginSourceRect(d);
1259
+ return `translate(${originRect.x + originRect.width - d.state.rect.width},${originRect.y + originRect.height - d.state.rect.height})`;
1260
+ });
1227
1261
  this.transition(mmGExit).attr("transform", (d) => {
1228
- const targetX = originRect.x + originRect.width - d.state.rect.width;
1229
- const targetY = originRect.y + originRect.height - d.state.rect.height;
1262
+ const targetRect = getOriginTargetRect(d);
1263
+ const targetX = targetRect.x + targetRect.width - d.state.rect.width;
1264
+ const targetY = targetRect.y + targetRect.height - d.state.rect.height;
1230
1265
  return `translate(${targetX},${targetY})`;
1231
1266
  }).remove();
1232
- mmGEnter.attr(
1233
- "transform",
1234
- (d) => `translate(${originRect.x + originRect.width - d.state.rect.width},${originRect.y + originRect.height - d.state.rect.height})`
1235
- );
1236
1267
  this.transition(mmGMerge).attr(
1237
1268
  "transform",
1238
1269
  (d) => `translate(${d.state.rect.x},${d.state.rect.y})`
1239
1270
  );
1240
- this.transition(mmLineExit).attr("x1", (d) => d.state.rect.width).attr("x2", (d) => d.state.rect.width);
1271
+ const mmLineExit = mmGExit.selectAll(
1272
+ childSelector("line")
1273
+ );
1274
+ this.transition(mmLineExit).attr("x1", (d) => d.state.rect.width).attr("stroke-width", 0);
1241
1275
  mmLineEnter.attr("x1", (d) => d.state.rect.width).attr("x2", (d) => d.state.rect.width);
1242
- mmLineMerge.attr("y1", (d) => d.state.rect.height).attr("y2", (d) => d.state.rect.height).attr("stroke", (d) => color(d));
1243
- this.transition(mmLineMerge).attr("x1", -1).attr("x2", (d) => d.state.rect.width + 2).attr("stroke-width", linkWidth);
1244
- mmCircle.attr("cx", (d) => d.state.rect.width).attr("cy", (d) => d.state.rect.height).attr("stroke", (d) => color(d)).attr(
1245
- "fill",
1246
- (d) => {
1247
- var _a;
1248
- return ((_a = d.payload) == null ? void 0 : _a.fold) && d.children ? color(d) : "var(--markmap-circle-open-bg)";
1249
- }
1276
+ mmLineMerge.attr("y1", (d) => d.state.rect.height).attr("y2", (d) => d.state.rect.height);
1277
+ this.transition(mmLineMerge).attr("x1", -1).attr("x2", (d) => d.state.rect.width + 2).attr("stroke", (d) => color(d)).attr("stroke-width", linkWidth);
1278
+ const mmCircleExit = mmGExit.selectAll(
1279
+ childSelector("circle")
1250
1280
  );
1251
- this.transition(mmCircle).attr("r", 6);
1281
+ this.transition(mmCircleExit).attr("r", 0).attr("stroke-width", 0);
1282
+ mmCircleMerge.attr("cx", (d) => d.state.rect.width).attr("cy", (d) => d.state.rect.height);
1283
+ this.transition(mmCircleMerge).attr("r", 6).attr("stroke-width", "1.5");
1252
1284
  this.transition(mmFoExit).style("opacity", 0);
1253
1285
  mmFoMerge.attr("width", (d) => Math.max(0, d.state.rect.width - paddingX * 2)).attr("height", (d) => d.state.rect.height);
1254
1286
  this.transition(mmFoMerge).style("opacity", 1);
1255
- const pathOrigin = [
1256
- originRect.x + originRect.width,
1257
- originRect.y + originRect.height
1258
- ];
1259
- this.transition(mmPathExit).attr("d", linkShape({ source: pathOrigin, target: pathOrigin })).remove();
1260
- mmPathEnter.attr(
1261
- "d",
1262
- linkShape({ source: pathOrigin, target: pathOrigin })
1263
- );
1287
+ this.transition(mmPathExit).attr("d", (d) => {
1288
+ const targetRect = getOriginTargetRect(d.target);
1289
+ const pathTarget = [
1290
+ targetRect.x + targetRect.width,
1291
+ targetRect.y + targetRect.height
1292
+ ];
1293
+ return linkShape({ source: pathTarget, target: pathTarget });
1294
+ }).attr("stroke-width", 0).remove();
1264
1295
  this.transition(mmPathMerge).attr("stroke", (d) => color(d.target)).attr("stroke-width", (d) => linkWidth(d.target)).attr("d", (d) => {
1265
1296
  const origSource = d.source;
1266
1297
  const origTarget = d.target;