Re: 將製作ejcees(中國象棋打譜程式)
发表于 : 2026年 5月 1日 13:44
gemini pro
You’ve reached your Pro model limit
Responses will use other models until it resets on May 1, 4:52 PM.
You’ve reached your Pro model limit
Responses will use other models until it resets on May 1, 4:52 PM.
代码: 全选
Here are the modifications to address your requirements. The URL logic has been updated to strictly require `#`, and FEN encoding/decoding has been modified to handle `r` and `f` in place of the standard `-` placeholders (or appended to `i`).
### 1. Update `encodeUrlParam()`
Update the `encodeUrlParam` method to inject the `r` and `f` flags into the FEN string.
```javascript
encodeUrlParam(node, isRoot) {
let res = '';
if (isRoot) {
let rFlag = this.isRotateEnabled ? 'r' : '';
let fFlag = this.isFlipEnabled ? 'f' : '';
if (node.fen === this.INITIAL_FEN) {
res += 'i' + rFlag + fFlag;
} else {
// Replace the '-' castling/en passant placeholders with 'r' and 'f'
let fenArr = node.fen.split(' ');
if (this.isRotateEnabled && fenArr.length >= 3) fenArr[2] = 'r';
if (this.isFlipEnabled && fenArr.length >= 4) fenArr[3] = 'f';
// Replace spaces with commas
res += fenArr.join(',');
}
}
if (node.c) {
res += ';' + encodeURIComponent(node.c);
}
if (node.v && node.v.length > 0) {
if (node.v.length === 1) {
let child = node.v[0];
let moveStr = child.move;
res += '+' + moveStr + this.encodeUrlParam(child, false);
} else {
res += '@';
let branchStrs = [];
for (let i = 0; i < node.v.length; i++) {
let child = node.v[i];
let moveStr = child.move;
branchStrs.push(moveStr + this.encodeUrlParam(child, false));
}
res += branchStrs.join('&') + '$';
}
}
return res;
}
```
### 2. Update `parseUrlHash()`
Update the `parseUrlHash` method to extract the `r` and `f` flags, revert them back to `-`, and save their state to the root node.
```javascript
parseUrlHash(hashStr) {
let pos = 0;
const peek = () => {
return pos < hashStr.length ? hashStr[pos] : null;
};
const consume = () => {
return pos < hashStr.length ? hashStr[pos++] : null;
};
// 1. Read FEN and extract rotate/flip flags
let fenStr = '';
let parsedR = false;
let parsedF = false;
if (peek() === 'i') {
consume();
fenStr = this.INITIAL_FEN;
// Handle variations like 'ir', 'if', 'irf', 'ifr'
while (peek() === 'r' || peek() === 'f') {
if (peek() === 'r') { parsedR = true; consume(); }
else if (peek() === 'f') { parsedF = true; consume(); }
}
} else {
let fenChars = [];
while (peek() !== null && peek() !== ';' && peek() !== '+' && peek() !== '@') {
fenChars.push(consume());
}
let fenRaw = fenChars.join('');
let fenArr = fenRaw.split(',');
// Revert 'r' and 'f' back to standard '-' FEN placeholders
if (fenArr.length >= 3 && fenArr[2] === 'r') {
parsedR = true;
fenArr[2] = '-';
}
if (fenArr.length >= 4 && fenArr[3] === 'f') {
parsedF = true;
fenArr[3] = '-';
}
fenStr = fenArr.join(' ');
}
let rootNode = {
fen: fenStr,
move: null,
r: parsedR ? 1 : 0,
f: parsedF ? 1 : 0,
c: '',
v: []
};
const createChildFromMove = (parentFen, moveStr) => {
// ... keep the rest of parseUrlHash unchanged
```
### 3. Update `importExportedText()`
Replace the two separate URL checking blocks at the beginning of `importExportedText` with this combined logic. `#` is now the strict requirement.
```javascript
importExportedText(text, importedFen = null) {
text = text.trim();
if (!text || text.startsWith('go ')) {
this.renderRecordUI();
this.renderNoteUI();
this.updateToolHighlights();
return;
}
// NEW URL PARSING LOGIC: Containing '#' is a strict requirement
if (text.includes('#')) {
let parts = text.split('#');
let beforeHash = parts[0].trim();
let hashPart = parts.length > 1 ? parts[1].trim() : '';
if (hashPart === '') {
this.baseUrl = beforeHash;
this.renderRecordUI();
this.renderNoteUI();
this.updateToolHighlights();
return;
} else {
let currentDomain = window.location.origin + window.location.pathname;
if (beforeHash !== '' && beforeHash !== currentDomain) {
this.baseUrl = beforeHash;
}
try {
let parsedTree = this.parseUrlHash(hashPart);
if (parsedTree && parsedTree.fen) {
this.historyFEN = parsedTree;
this.initBranch();
this.currentStepIndex = 0;
// Assign rotate and flip states based on parsed data
this.isRotateEnabled = this.historyFEN.r === 1;
this.isFlipEnabled = this.historyFEN.f === 1;
this.reapplyVisualPositions();
this.renderRecordUI();
this.renderNoteUI();
this.jumpToStep(0);
this.saveStateToUndo();
this.updateToolHighlights();
return;
}
} catch (e) {
console.log('Failed to parse as URL parameter, falling back to text notation.', e);
}
}
}
// Normalize Chinese notations
text = text
.replace(/车/g, '車')
// ... keep the rest of importExportedText unchanged (remove the old `if (text.includes('#') || text.startsWith('i;') ...)` block since it is now consolidated above)
```