Skip to content

Commit

Permalink
Yaml Editor: Auto-fold section in RKE2 cluster config (rancher#10108)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwmac authored Dec 14, 2023
1 parent 1598a46 commit 83550bd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions shell/components/ResourceYaml.vue
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ export default {
cm.foldLinesMatching(/managedFields/);
// Allow the model to supply an array of json paths to fold other sections in the YAML for the given resource type
if (this.value?.yamlFolding) {
this.value.yamlFolding.forEach((path) => cm.foldYaml(path));
}
// regardless of edit or create we should probably fold all the comments so they dont get out of hand.
const saved = cm.getMode().fold;
Expand Down
7 changes: 7 additions & 0 deletions shell/models/provisioning.cattle.io.cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,4 +901,11 @@ export default class ProvCluster extends SteveModel {

return null;
}

// JSON Paths that should be folded in the YAML editor by default
get yamlFolding() {
return [
'spec.rkeConfig.machinePools.dynamicSchemaSpec',
];
}
}
41 changes: 41 additions & 0 deletions shell/plugins/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,47 @@ CodeMirror.defineExtension('foldLinesMatching', function(regex) {
});
});

function countSpaces(line) {
for (let i = 0; i < line.length; i++) {
if (line[i] !== ' ') {
return i;
}
}

return line.length;
}

CodeMirror.defineExtension('foldYaml', function(path) {
this.operation(() => {
let elements = [];

for (let i = this.firstLine(), e = this.lastLine(); i <= e; i++) {
const line = this.getLine(i);
const index = countSpaces(line);
const trimmed = line.trim();

if (trimmed.endsWith(':') || trimmed.endsWith(': >-')) {
const name = trimmed.split(':')[0].substr(0, trimmed.length - 1);

// Remove all elements of the same are greater index
elements = elements.filter((e) => e.index < index);

// Add on this one
elements.push({
index,
name
});

const currentPath = elements.map((e) => e.name).join('.');

if (currentPath === path) {
this.foldCode(CodeMirror.Pos(i, 0), null, 'fold');
}
}
}
});
});

CodeMirror.registerHelper('fold', 'yamlcomments', (cm, start) => {
if ( !isLineComment(cm, start.line) ) {
return;
Expand Down
7 changes: 7 additions & 0 deletions shell/plugins/dashboard-store/resource-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1927,4 +1927,11 @@ export default class Resource {
get creationTimestamp() {
return this.metadata?.creationTimestamp;
}

/**
* Allows model to specify JSON Paths that should be folded in the YAML editor by default
*/
get yamlFolding() {
return [];
}
}

0 comments on commit 83550bd

Please sign in to comment.