Skip to content

Commit

Permalink
fix: refactor the code and remove new line form char count
Browse files Browse the repository at this point in the history
  • Loading branch information
dineshsutihar committed Oct 2, 2024
1 parent 44fb2d9 commit bd87033
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <h2>Have a play.</h2>

<div class="controls" style="width: 49%;">

<p id="charCount">Characters: <span></span></p>
<p id="charCount">Characters: <span>0</span></p>

<div class="pull-right">
<!--<a href="#" class="btn btn-primary">Download Your Code</a>-->
Expand Down Expand Up @@ -106,22 +106,20 @@ <h2>Have a play.</h2>
<script type="text/javascript" src="./examples/js/pdfobject.min.js"></script>
<script type="text/javascript" src="./examples/js/editor.js"></script>

<!-- Character Count Updater -->
<!-- Character Count -->
<script>
$(document).ready(function() {
const $editor = $('#editor');
const $charCount = $('#charCount span');
const $templateSelector = ace.edit("editor").getSession();
const initialCount = 51; // Initial Default Count in the editor

function updateCharCount() {
const charCount = $editor.text().length - initialCount;
$charCount.text(charCount);
var editor = ace.edit("editor");

function countCharacters() {
var text = editor.getValue();
var textWithoutNewlines = text.replace(/(\r\n|\n|\r)/g, '');
var count = textWithoutNewlines.length;
$('#charCount span').text(count);
}

$(window).on('load', updateCharCount);
$editor.on('keyup', updateCharCount);
$templateSelector.on('change', updateCharCount);

editor.getSession().on('change', countCharacters);
countCharacters();
});
</script>
</body>
Expand Down

0 comments on commit bd87033

Please sign in to comment.