Skip to content

Commit

Permalink
#555 Fixed problem with ignored game comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
mliebelt committed Jun 23, 2024
1 parent fad61fc commit f749e49
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/tickets/ticket522.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script src="/lib/dist.js" type="text/javascript"></script>
</head>
<body >
<p><a href="https://github.com/mliebelt/pgn-viewer/issues/522">Ticket 482 at GitHub</a>.
<p><a href="https://github.com/mliebelt/pgn-viewer/issues/522">Ticket 522 at GitHub</a>.

<div id="boardLeo1" style="width: 320px"></div>
<script>
Expand Down
17 changes: 17 additions & 0 deletions examples/tickets/ticket555.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Game Comment not showing up</title>
<script src="/lib/dist.js" type="text/javascript"></script>
</head>
<body >
<p><a href="https://github.com/mliebelt/pgn-viewer/issues/555">Ticket 555 at GitHub</a>.

<div id="board" style="width: 320px"></div>
<script>
var pgn = "{ First comment } 1. e4 { after first white } e5 { after first black }";
var board = PGNV.pgnView('board', { pgn: pgn});
</script>
</body>
</html>
78 changes: 43 additions & 35 deletions src/pgnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,39 @@ let pgnBase = function (boardId:string, configuration:PgnViewerConfiguration) {
}
}

/**
* Comments are generated inline, there is no special block rendering
* possible for them.
* @param comment the comment to render as span
* @param clazz class parameter appended to differentiate different comments
* @returns {HTMLElement} the new created span with the comment as text, or null, if text is null
*/
function generateCommentSpan (comment:string, clazz:string): HTMLElement {
if (comment && (typeof comment == "string")) {
const commentSpan = createEle('span', null, "comment " + clazz)
commentSpan.appendChild(document.createTextNode(" " + comment + " "))
return commentSpan
}
return null
}

function createFiller(span:HTMLElement) {
const filler = createEle('span', null, "move filler")
filler.appendChild(document.createTextNode("... "))
span.appendChild(filler)
}

function appendCommentSpan (span:HTMLElement, comment:string, clazz:string, fillNeeded?:boolean) {
let cSpan = generateCommentSpan(comment, clazz)
if (cSpan) {
if (fillNeeded) {
createFiller(span)
}
span.appendChild(cSpan)
return fillNeeded
}
return false
}
/**
* Inserts an element after targetElement
* @param {*} newElement the element to insert
Expand Down Expand Up @@ -942,39 +975,7 @@ let pgnBase = function (boardId:string, configuration:PgnViewerConfiguration) {
* @return {*} the current counter which may the next prev counter
*/
function generateMove (currentCounter:number, game:any, move:PgnReaderMove, prevCounter:number, movesDiv:HTMLElement, varStack:HTMLElement[]) {
/**
* Comments are generated inline, there is no special block rendering
* possible for them.
* @param comment the comment to render as span
* @param clazz class parameter appended to differentiate different comments
* @returns {HTMLElement} the new created span with the comment as text, or null, if text is null
*/
function generateCommentSpan (comment:string, clazz:string): HTMLElement {
if (comment && (typeof comment == "string")) {
const commentSpan = createEle('span', null, "comment " + clazz)
commentSpan.appendChild(document.createTextNode(" " + comment + " "))
return commentSpan
}
return null
}

function createFiller(span:HTMLElement) {
const filler = createEle('span', null, "move filler")
filler.appendChild(document.createTextNode("... "))
span.appendChild(filler)
}

function appendCommentSpan (span:HTMLElement, comment:string, clazz:string, fillNeeded?:boolean) {
let cSpan = generateCommentSpan(comment, clazz)
if (cSpan) {
if (fillNeeded) {
createFiller(span)
}
span.appendChild(cSpan)
return fillNeeded
}
return false
}

function appendVariation(div:HTMLElement, move:PgnReaderMove) {
// if (! movesDiv.lastChild.classList.contains("variations")) {
Expand Down Expand Up @@ -1214,11 +1215,16 @@ let pgnBase = function (boardId:string, configuration:PgnViewerConfiguration) {
let imc = document.querySelector('#' + boardId + " input.moveComment") as HTMLInputElement
imc.checked = true
let tac = document.querySelector('#' + boardId + " textarea.comment") as HTMLTextAreaElement
tac.value = myMove.commentMove
if (tac) {
tac.value = myMove.commentMove
}
} else {
try {
let tac = document.querySelector('#' + boardId + " textarea.comment") as HTMLTextAreaElement
tac.value = ""
if (tac) {
tac.value = ""
}

} catch (err) {
console.log("tac: " + '#' + boardId + " textarea.comment")
console.log(err)
Expand Down Expand Up @@ -1315,7 +1321,9 @@ let pgnBase = function (boardId:string, configuration:PgnViewerConfiguration) {
/* #338 Handle the game comment, if one is there.
*/
function handleGameComment(movesDiv:HTMLElement, gameComment:GameComment) {
// appendCommentSpan(movesDiv, gameComment.comment, "moveComment")
if (gameComment) {
appendCommentSpan(movesDiv, gameComment.comment, "moveComment")
}
}

if (hasMode(PgnViewerMode.Puzzle) && that.currentMove == undefined){
Expand Down

0 comments on commit f749e49

Please sign in to comment.