Here are the modifications to address both the parsing fallback for invalid move characters and the UI toggle for including comments in the Chinese notation export.
Replace the if (!dc) block inside your move parsing loop. When a token cannot be converted into coordinates, it will now be appended to the comments of the current node (the previous step).
JavaScript
dc = deriveCoordsFromMove(currentFen, moveEn, isRed);
}
if (!dc) {
// If coordinates cannot be derived, treat the token as a comment
// for the current node (which represents the previous step)
if (!currentCommentNode.c) {
currentCommentNode.c = "";
}
// Append the token, separating with a space if a comment already exists
currentCommentNode.c += (currentCommentNode.c ? " " : "") + token;
continue; // Skip node creation and continue parsing
}
To allow toggling the comment mode when the cn button is already active, you'll need a global or scope-level variable to track this state, and an update to your click event listener for the export format buttons.
Add this state tracker near where your export variables are defined:
JavaScript
let exportCnWithComments = false; // Tracks whether Chinese Notation should include comments
Update your event listener for the .exp-fmt-btn elements:
JavaScript
document.querySelectorAll('.exp-fmt-btn').forEach(btn => {
btn.addEventListener('click', function() {
const fmt = this.getAttribute('data-fmt');
// Check if the clicked button is 'cn' AND is already active
if (fmt === 'cn' && this.classList.contains('active')) {
// Toggle comment mode
exportCnWithComments = !exportCnWithComments;
// Update button text to reflect the current mode visually
this.innerText = exportCnWithComments ? "Chinese Notation (w/ Comments)" : "Chinese Notation";
} else {
// Handle normal format switching
document.querySelectorAll('.exp-fmt-btn').forEach(b => {
b.classList.remove('active');
// Reset text for the CN button if we switch away from it
if (b.getAttribute('data-fmt') === 'cn') {
b.innerText = "Chinese Notation";
}
});
this.classList.add('active');
// Reset comment mode when switching to a new format
if (fmt === 'cn') {
exportCnWithComments = false;
}
// Assuming you have a variable tracking the current format
// currentExportFormat = fmt;
}
// Call your function that regenerates and displays the exported text
// updateExportText();
});
});
In the function where you generate the text for the data-fmt="cn" export, use the exportCnWithComments flag to weave the comments into the main branch traversal.
JavaScript
// Example snippet to integrate into your text export generation logic
let exportText = "";
let tempNode = historyFEN; // Start at the root node
let stepCount = 1;
// Traverse only the main branch (index 0) as requested ("no need to add branches")
while (tempNode.v && tempNode.v.length > 0) {
let childNode = tempNode.v[0];
let isRed = (stepCount % 2 !== 0);
let roundNum = Math.ceil(stepCount / 2);
// Assuming NotationConverter handles English to Chinese conversion
let cnMove = NotationConverter.toChinese(childNode.move, isRed);
if (isRed) {
// Red's move: "1. 炮二平五"
exportText += `${roundNum}. ${cnMove} `;
} else {
// Black's move: "马8进7" followed by a newline
exportText += `${cnMove}\n`;
}
// Insert comments using the specified format if toggled ON
if (exportCnWithComments && childNode.c) {
// Flatten multiline comments
let commentText = childNode.c.replace(/\n/g, ' ');
if (isRed) {
// If the comment interrupts between Red and Black's move, use ellipses
exportText += `\n【${commentText}】\n ${roundNum}. ………… `;
} else {
// If the comment comes after Black's move
exportText += `【${commentText}】\n`;
}
}
tempNode = childNode;
stepCount++;
}
// Clean up any trailing whitespace
exportText = exportText.trim();
Would you like me to help integrate this export generation logic into your specific updateExportText function if you have it handy?