From 8e1f15e88d1c7a29881b199b8eba2b6d9719c0d7 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 12 Oct 2017 14:12:26 +0200 Subject: [PATCH] Get title suggestion based on poll's description --- www/common/sframe-common-codemirror.js | 78 ++++++++++++++------------ www/poll/inner.js | 8 ++- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/www/common/sframe-common-codemirror.js b/www/common/sframe-common-codemirror.js index 098972161..c3a49703f 100644 --- a/www/common/sframe-common-codemirror.js +++ b/www/common/sframe-common-codemirror.js @@ -61,6 +61,46 @@ define([ editor.scrollTo(scroll.left, scroll.top); }; + module.getHeadingText = function (editor) { + var lines = editor.getValue().split(/\n/); + + var text = ''; + lines.some(function (line) { + // lines including a c-style comment are also valuable + var clike = /^\s*(\/\*|\/\/)(.*)?(\*\/)*$/; + if (clike.test(line)) { + line.replace(clike, function (a, one, two) { + if (!(two && two.replace)) { return; } + text = two.replace(/\*\/\s*$/, '').trim(); + }); + return true; + } + + // lisps? + var lispy = /^\s*(;|#\|)+(.*?)$/; + if (lispy.test(line)) { + line.replace(lispy, function (a, one, two) { + text = two; + }); + return true; + } + + // lines beginning with a hash are potentially valuable + // works for markdown, python, bash, etc. + var hash = /^#+(.*?)$/; + if (hash.test(line)) { + line.replace(hash, function (a, one) { + text = one; + }); + return true; + } + + // TODO make one more pass for multiline comments + }); + + return text.trim(); + }; + module.create = function (Common, defaultMode, CMeditor) { var exp = {}; var Messages = Cryptpad.Messages; @@ -151,43 +191,7 @@ define([ }()); exp.getHeadingText = function () { - var lines = editor.getValue().split(/\n/); - - var text = ''; - lines.some(function (line) { - // lines including a c-style comment are also valuable - var clike = /^\s*(\/\*|\/\/)(.*)?(\*\/)*$/; - if (clike.test(line)) { - line.replace(clike, function (a, one, two) { - if (!(two && two.replace)) { return; } - text = two.replace(/\*\/\s*$/, '').trim(); - }); - return true; - } - - // lisps? - var lispy = /^\s*(;|#\|)+(.*?)$/; - if (lispy.test(line)) { - line.replace(lispy, function (a, one, two) { - text = two; - }); - return true; - } - - // lines beginning with a hash are potentially valuable - // works for markdown, python, bash, etc. - var hash = /^#+(.*?)$/; - if (hash.test(line)) { - line.replace(hash, function (a, one) { - text = one; - }); - return true; - } - - // TODO make one more pass for multiline comments - }); - - return text.trim(); + return module.getHeadingText(editor); }; exp.configureLanguage = function (cb, onModeChanged) { diff --git a/www/poll/inner.js b/www/poll/inner.js index 7e2cf9763..2ea839e69 100644 --- a/www/poll/inner.js +++ b/www/poll/inner.js @@ -998,6 +998,11 @@ define([ Cryptpad.findOKButton().click(); }; + var getHeadingText = function () { + if (!APP.editor) { return; } + return SframeCM.getHeadingText(APP.editor); + }; + var onCreate = function (info) { APP.myID = info.myID; @@ -1011,7 +1016,8 @@ define([ metadataMgr = common.getMetadataMgr(); - Title = common.createTitle(); + var titleCfg = { getHeadingText: getHeadingText }; + Title = common.createTitle(titleCfg); var configTb = { displayed: ['title', 'useradmin', 'spinner', 'share', 'userlist', 'newpad', 'limit'],