From 1d300ace9897e250bc5d0ec74b59bb7e2db87639 Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Fri, 29 Aug 2014 23:53:13 +1000 Subject: [PATCH] Add prototype code for token breaker --- modules/cms/assets/js/october.tokenbreaker.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 modules/cms/assets/js/october.tokenbreaker.js diff --git a/modules/cms/assets/js/october.tokenbreaker.js b/modules/cms/assets/js/october.tokenbreaker.js new file mode 100644 index 000000000..64954a28c --- /dev/null +++ b/modules/cms/assets/js/october.tokenbreaker.js @@ -0,0 +1,59 @@ +/* + * Token Breaker plugin + * Locates Twig tokens and replaces them with potential content inside. + * + * Data attributes: + * - data-control="dragcomponents" - enables the plugin on an element + * - data-option="value" - an option with a value + * + * JavaScript API: + * $('a#someElement').dragComponents({ option: 'value' }) + * + * Dependences: + * - Some other plugin (filename.js) + */ +var $editorArea = $('#cms-master-tabs > div.tab-content > .tab-pane.active [data-control="codeeditor"]') +var $editor = $editorArea.codeEditor('getEditorObject'), + $selection = $editor.getSelection(), + $session = $editor.getSession(), + $document = $session.getDocument() + +$selection.on('changeCursor', function (event){ + + var cursor = $selection.getCursor(), + line = $document.getLine(cursor.row), + wordRange = $session.getWordRange(cursor.row, cursor.column), + word = $session.getTextRange(wordRange) + + if (word == 'component') { + + var lineRegex = new RegExp(/{%\s*component\s(['"])([^"']+)(?:\1)[^(?:%})]+%}/i), + result = lineRegex.exec(line), + start, + end, + lastEnd = 0, + lineLength = line.length + + if (result) { + while (true) { + start = line.indexOf(result[0], lastEnd), + end = start + result[0].length + if (start == -1) break + + if (isMatchingRegex(start, end, cursor.column)) { + console.log(result[2] + "Found result at: " + end) + } + + lastEnd = end + } + } + + + } +}) + +function isMatchingRegex(sampleStart, sampleEnd, target) +{ + return sampleStart < target && sampleEnd > target +} +