Double-click code block selection

Adding double-click code block selection1) to DokuWiki requires some modification to the core engine.

Because you're modifying core engine code, you need to reinstate this change any time you upgrade your install.

  1. Define a function that takes an element as an argument, then selects all of that element's text. You need to do this somewhere in a script file loaded by default (e.g. /lib/scripts/script.js):

    /**
     * Function to select code blocks on double-click
     */
    function firefive_selectCode(codeBlock) {
     
      var range;
     
      if (document.body.createTextRange) {
     
        range = document.body.createTextRange();
        range.moveToElementText(codeBlock);
        range.select();
     
      } else if (window.getSelection) {
     
        range = document.createRange();
        range.selectNodeContents(codeBlock);
     
        var selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(range);
      }
    }
  2. Call the function when a code block is double-clicked, by adding an ondblclick attribute to the <pre> tag that opens a code block. These tags are prepared in the _highlight() function, found in /inc/parser/xhtml.php.

    Replace:

    '<pre class="'.$type.'">'
    // ...
    "<pre title="TIP: Double-click anywhere in the block to select all the code" class=\"$class $language\">"

    with

    '<pre ondblclick="firefive_selectCode(this);" class="' . $type . '">'
    // ...
    '<pre title="TIP: Double-click anywhere in the block to select all the code" ondblclick="firefive_selectCode(this);" class="' . $class . ' ' . $language . '">'

The line of code appears in different places for different wiki versions:

Version File Line number
Greebo inc/parser/xhtml.php 697, 702

1)
Double-click one of the code blocks below to see what this means!