From fc9c94c817662b798b6618c3861f01ded787418a Mon Sep 17 00:00:00 2001 From: minjk-bl Date: Mon, 27 Sep 2021 17:36:41 +0900 Subject: [PATCH 1/2] change Apps > Markdown title from text to Markdown --- src/api_block/constData.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api_block/constData.js b/src/api_block/constData.js index 3c17b171..bbbb7be5 100644 --- a/src/api_block/constData.js +++ b/src/api_block/constData.js @@ -287,7 +287,7 @@ define([ const STR_CODE = 'code'; const STR_COMMENT = 'comment'; const STR_NODE = 'node'; - const STR_TEXT = 'text'; + const STR_TEXT = 'Markdown'; const STR_PRINT = 'print'; const STR_ELIF = 'elif'; const STR_ELSE = 'else'; From fba67aff17911dd28fbb1a8723d502f31847fb34 Mon Sep 17 00:00:00 2001 From: minjk-bl Date: Mon, 27 Sep 2021 17:45:59 +0900 Subject: [PATCH 2/2] Apps > PDF added --- css/common/pdf.css | 182 ++++++++++++++ src/api_block/constData.js | 3 + src/api_block/index.html | 4 + src/api_block/init.js | 3 +- src/common/pycode.js | 79 ++++++ src/common/vpPDF.js | 499 +++++++++++++++++++++++++++++++++++++ 6 files changed, 769 insertions(+), 1 deletion(-) create mode 100644 css/common/pdf.css create mode 100644 src/common/pycode.js create mode 100644 src/common/vpPDF.js diff --git a/css/common/pdf.css b/css/common/pdf.css new file mode 100644 index 00000000..60620a24 --- /dev/null +++ b/css/common/pdf.css @@ -0,0 +1,182 @@ +.vp-pdf { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + z-index: 1200; + background-color: rgba(0,0,0,.4); +} +.vp-pdf-container { + position: relative; + left: 50%; + top: 50%; + transform:translate(-50%, -50%); + min-width: 470px; + min-height: 420px; + width: 50%; + height: 60%; + background-color: white; +} +.vp-pdf-title { + height: 40px; + padding: 5px 0px 5px 10px; + background-color: #EEE; + border: 1px solid #ddd;; + display: flex; + flex-direction: row; + position: relative; + align-items: center; + font-size: 16px; + font-weight: 700; +} +.vp-pdf-close, +.vp-pdf-popup-close { + position: fixed; + z-index: 3; + right: 5px; + width: 30px; + height: 20px; + line-height: 20px; + top: 10px; + text-align: center; + cursor: pointer; +} +.vp-pdf-body { + width: 100%; + height: calc(100% - 90px); + padding: 10px; +} +.vp-pdf-grid-box { + display: grid; + width: 100%; + height: 100%; + grid-row-gap: 5px; + /* grid-template-rows: 60px calc(100% - 80px); */ + /* grid-template-rows: 80px calc(100% - 100px); */ + /* grid-template-rows: 80px min-content; */ + grid-template-rows: 60px 140px 150px; +} +.vp-pdf-prepare-box { + display: grid; + grid-template-columns: 50% 50%; + align-items: baseline; + align-content: center; + border: 1px solid #E4E4E4; + border-radius: 5px; + box-sizing: border-box; + padding: 15px; +} +.vp-pdf-link { + color: var(--highlight-color); +} +.vp-pdf-install-btn{ + margin-left: 7px; +} +.vp-pdf-check-btn { + display: inline-block; + margin-left: 5px; +} +.vp-pdf-import-btn { + margin-left: 10px; +} +.vp-pdf-show-box { + display: grid; + /* grid-template-rows: min-content min-content; */ + grid-row-gap: 10px; + border: 1px solid #E4E4E4; + border-radius: 5px; + box-sizing: border-box; + padding: 15px; +} +.vp-pdf-df-box { + display: grid; + grid-template-columns: 95px auto; + grid-row-gap: 3px; + align-items: baseline; + align-content: space-evenly; +} +.vp-pdf #vp_pdfVariable { + width: 62%; + height: 30px; +} +.vp-pdf-input { + width: 62%; +} +.vp-pdf-run-btn { + width: 112px !important; + margin-left: 5px; +} +.vp-pdf-preview-box { + display: none; + width: 100%; + height: 100px; + position: absolute; + bottom: 50px; + + background: #F7F7F7; + border: 0.25px solid #E4E4E4; + padding: 10px; +} +/** Buttons */ +.vp-pdf-btn-box { + position: relative; + height: 50px; + border-top: 0.25px solid var(--border-gray-color); +} +.vp-pdf-btn { + position: absolute; +} +.vp-pdf-btn-preview { + left: 15px; + top: 9px; +} +.vp-pdf-btn-dataview { + left: 105px; + top: 9px; +} +.vp-pdf-btn-cancel { + top: 9px; + right: 105px; +} +.vp-pdf-btn-runadd { + position: absolute; + top: 9px; + right: 15px; + width: fit-content; + height: 30px; + background: #F38504; + border-radius: 2px; +} +.vp-pdf-btn-run { + display: inline-block; + width: 60px; + border-radius: 2px 0px 0px 2px; + border-right: 0.25px solid var(--border-gray-color) !important; +} +.vp-pdf-btn-detail { + display: inline-block; + width: 20px; + height: 28px; + border-radius: 0px 2px 2px 0px; + padding: 0; +} +.vp-pdf-detail-box { + display: none; + background: white; + border: 0.25px solid var(--border-gray-color); + position: absolute; + bottom: 35px; + right: 1px; + width: 84px; + height: 62px; + text-align: center; + line-height: 30px; +} +.vp-pdf-detail-item { + color: var(--font-primary); +} +.vp-pdf-detail-item:hover { + color: var(--font-hightlight); + background: var(--light-gray-color); +} diff --git a/src/api_block/constData.js b/src/api_block/constData.js index bbbb7be5..18b4fdf4 100644 --- a/src/api_block/constData.js +++ b/src/api_block/constData.js @@ -776,6 +776,9 @@ define([ }, 'profiling': { file: 'nbextensions/visualpython/src/common/vpProfiling' + }, + 'pdf': { + file: 'nbextensions/visualpython/src/common/vpPDF' } } diff --git a/src/api_block/index.html b/src/api_block/index.html index cc12b13a..f7e1e851 100644 --- a/src/api_block/index.html +++ b/src/api_block/index.html @@ -94,6 +94,10 @@
Profiling
+
+ +
PDF
+
Merge
diff --git a/src/api_block/init.js b/src/api_block/init.js index 283c01f4..be0093d7 100644 --- a/src/api_block/init.js +++ b/src/api_block/init.js @@ -266,6 +266,7 @@ define([ case 'frame': case 'chart': case 'profiling': + case 'pdf': blockContainer.setSelectBlock(null); blockContainer.createAppsPage(menu, file, config); break; @@ -379,7 +380,7 @@ define([ }); /** Apps Menu Apply event */ - $(document).on('subset_run frame_run', '#vp_appsCode', function(evt) { + $(document).on('subset_run frame_run pdf_run', '#vp_appsCode', function(evt) { var code = evt.code; var title = evt.title; var state = evt.state; diff --git a/src/common/pycode.js b/src/common/pycode.js new file mode 100644 index 00000000..5fe67e2b --- /dev/null +++ b/src/common/pycode.js @@ -0,0 +1,79 @@ +/* + * Project Name : Visual Python + * Description : GUI-based Python code generator + * File Name : pycode.js + * Author : Black Logic + * Note : Define constant value + * License : GNU GPLv3 with Visual Python special exception + * Date : 2021. 08. 14 + * Change Date : + */ + +//============================================================================ +// Define constant +//============================================================================ +define ([ +], function() { + 'use strict'; + + //======================================================================== + // vpPDF + //======================================================================== + const PDF_SHOW = '!pip show PyMuPDF nltk' + const PDF_INSTALL1 = '!pip install PyMuPDF' + const PDF_INSTALL2 = '!pip install nltk' + + const PDF_IMPORT = `import pandas as pd +import fitz +from nltk.tokenize import sent_tokenize`; + + const PDF_FUNC = `def vp_pdf_get_sentence(fname_lst): + ''' + Get sentence from pdf file by PyMuPDF + ''' + df = pd.DataFrame() + for fname in fname_lst: + if fname.split('.')[-1] != 'pdf': continue + try: + doc = fitz.open(fname) + sentence_lst = [] + for page in doc: + block_lst = page.getText('blocks') + + text_lst = [block[4] for block in block_lst if block[6] == 0] + text = '\\n'.join(text_lst) + + sentence_lst.extend([sentence for sentence in sent_tokenize(text)]) + + doc.close() + except: + continue + + df_doc = pd.DataFrame({ + 'fname': fname, + 'sentence': sentence_lst + }) + df = pd.concat([df,df_doc]) + + return df.reset_index().drop('index', axis=1)`; + + const PDF_CMD = 'df = vp_pdf_get_sentence(pdf_lst)\ndf' + + + + //======================================================================== + // return value + //======================================================================== + return { + PDF_SHOW, + PDF_INSTALL1, + PDF_INSTALL2, + PDF_IMPORT, + PDF_FUNC, + PDF_CMD + }; + +}); /* function, define */ + +/* End of file */ + diff --git a/src/common/vpPDF.js b/src/common/vpPDF.js new file mode 100644 index 00000000..61fb4101 --- /dev/null +++ b/src/common/vpPDF.js @@ -0,0 +1,499 @@ +/* + * Project Name : Visual Python + * Description : GUI-based Python code generator + * File Name : vpPDF.js + * Author : Black Logic + * Note : Define constant value + * License : GNU GPLv3 with Visual Python special exception + * Date : 2021. 08. 14 + * Change Date : + */ + +//============================================================================ +// Define constant +//============================================================================ +define([ + 'nbextensions/visualpython/src/common/constant', + 'nbextensions/visualpython/src/common/StringBuilder', + 'nbextensions/visualpython/src/common/vpCommon', + 'nbextensions/visualpython/src/common/pycode', + // file navigation + 'nbextensions/visualpython/src/pandas/fileNavigation/index', + + 'codemirror/lib/codemirror', + 'codemirror/mode/python/python', + 'notebook/js/codemirror-ipython', + 'codemirror/addon/display/placeholder', + 'codemirror/addon/display/autorefresh', +], function (vpConst, sb, vpCommon, pycode, fileNavigation, codemirror) { + + //======================================================================== + // Define variable + //======================================================================= + const VP_PDF = 'vp-pdf'; + const VP_PDF_CONTAINER = 'vp-pdf-container'; + const VP_PDF_TITLE = 'vp-pdf-title'; + const VP_PDF_CLOSE = 'vp-pdf-close'; + const VP_PDF_BODY = 'vp-pdf-body'; + const VP_PDF_GRID_BOX = 'vp-pdf-grid-box'; + + const VP_PDF_PREPARE_BOX = 'vp-pdf-prepare-box'; + const VP_PDF_INSTALL_BTN = 'vp-pdf-install-btn'; + const VP_PDF_CHECK_BTN = 'vp-pdf-check-btn'; + const VP_PDF_IMPORT_BTN = 'vp-pdf-import-btn'; + const VP_PDF_SHOW_BOX = 'vp-pdf-show-box'; + const VP_PDF_DF_BOX = 'vp-pdf-df-box'; + const VP_PDF_RUN_BTN = 'vp-pdf-run-btn'; + + const VP_PDF_PREVIEW_BOX = 'vp-pdf-preview-box'; + const VP_PDF_BUTTON_BOX = 'vp-pdf-btn-box'; + const VP_PDF_BUTTON_PREVIEW = 'vp-pdf-btn-preview'; + const VP_PDF_BUTTON_CANCEL = 'vp-pdf-btn-cancel'; + const VP_PDF_BUTTON_RUNADD = 'vp-pdf-btn-runadd'; + const VP_PDF_BUTTON_RUN = 'vp-pdf-btn-run'; + const VP_PDF_BUTTON_DETAIL = 'vp-pdf-btn-detail'; + const VP_PDF_DETAIL_BOX = 'vp-pdf-detail-box'; + const VP_PDF_DETAIL_ITEM = 'vp-pdf-detail-item'; + + const VP_PDF_FILEPATH = 'vp-pdf-filepath'; + const VP_PDF_FILENAME = 'vp-pdf-filename'; + + //======================================================================== + // [CLASS] PDF + //======================================================================== + class PDF { + /** + * constructor + * @param {object} pageThis + * @param {string} targetId + */ + constructor(pageThis, targetId) { + this.pageThis = pageThis; + this.targetId = targetId; + this.uuid = 'u' + vpCommon.getUUID(); + + // file navigation state + this.state = { + paramData:{ + encoding: 'utf-8' // encoding + , delimiter: ',' // seperater + }, + returnVariable:'', + isReturnVariable: false, + fileExtension: ['pdf'] + }; + this.fileResultState = { + pathInputId : this.wrapSelector('.' + VP_PDF_FILEPATH), + fileInputId : this.wrapSelector('.' + VP_PDF_FILENAME) + }; + } + + wrapSelector(query = '') { + return vpCommon.formatString('.{0}.{1} {2}', VP_PDF, this.uuid, query); + } + + open(config={}) { + this.config = { + ...this.config, + ...config + } + + if (this.config.state) { + this.init(this.config.state); + } else { + this.init(); + } + $(this.wrapSelector()).show(); + + // load state + if (this.config.state) { + this.loadState(this.config.state); + } + } + + close() { + this.unbindEvent(); + $(this.wrapSelector()).remove(); + } + + init() { + // state + vpCommon.loadCss(Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH + 'common/pdf.css'); + + this.cmpreviewall = undefined; + this.previewOpened = false; + + this.render(); + this.checkInstalled(); + this.bindEvent(); + } + + loadState(state) { + var { + filepath, filename, allocateTo + } = state; + + // load variable + $(this.wrapSelector('.' + VP_PDF_FILEPATH)).val(filepath); + $(this.wrapSelector('.' + VP_PDF_FILENAME)).val(filename); + $(this.wrapSelector('#vp_pdfReturn')).val(allocateTo); + } + + saveState() { + this.state.filepath = $(this.wrapSelector('.' + VP_PDF_FILEPATH)).val(); + this.state.filename = $(this.wrapSelector('.' + VP_PDF_FILENAME)).val(); + this.state.allocateTo = $(this.wrapSelector('#vp_pdfReturn')).val(); + } + + render() { + var page = new sb.StringBuilder(); + page.appendFormatLine('
', VP_PDF, this.uuid); + page.appendFormatLine('
', VP_PDF_CONTAINER); + + // title + page.appendFormat('
{1}
', + VP_PDF_TITLE, 'PyMuPDF - Extract text from pdf'); + + // close button + page.appendFormatLine('
', + VP_PDF_CLOSE, '/nbextensions/visualpython/resource/close_big.svg'); + + // body start + page.appendFormatLine('
', VP_PDF_BODY); + page.appendFormatLine('
', VP_PDF_GRID_BOX); + + // prepare box + page.appendFormatLine('
', VP_PDF_PREPARE_BOX); + page.appendFormatLine('', + 'Prepare to use PyMuPDF', 'https://pymupdf.readthedocs.io/', 'fa fa-link', 'vp-pdf-link', 'Go to PyMuPDF Documentaion'); + page.appendLine('
'); + page.appendFormatLine('', 'vp-button activated', VP_PDF_INSTALL_BTN, 'Install'); + page.appendFormatLine('
', 'vp-cursor', VP_PDF_CHECK_BTN, 'Check if installed', '/nbextensions/visualpython/resource/refresh.svg'); + // page.appendLine('
'); + page.appendFormatLine('', 'vp-button', VP_PDF_IMPORT_BTN, 'Import and Add defined function', 'Import'); + page.appendLine('
'); + page.appendLine('
'); + + // page.appendLine('
'); + // show box + page.appendFormatLine('
', VP_PDF_SHOW_BOX); + // Select DataFrame + page.appendFormatLine('
', VP_PDF_DF_BOX); + + page.appendFormatLine('', 'vp_pdfVariable', 'vp-orange-text', 'File Path'); + + page.appendLine('
'); + + page.appendFormatLine('
', + VP_PDF_FILEPATH, vpConst.FILE_BROWSER_INPUT_BUTTON) + + page.appendLine('
'); + + page.appendFormatLine('', 'vp_pdfReturn', 'Allocate to'); + page.appendLine('
'); + page.appendFormatLine('', 'vp_pdfReturn', 'vp-pdf-input', 'New variable name'); + page.appendLine('
'); + + // hidden inputs + page.appendFormatLine('', VP_PDF_FILENAME); + page.appendLine('
'); // VP_PDF_DF_BOX + + page.appendLine('
'); // VP_PDF_SHOW_BOX + + page.appendLine('
'); // VP_PDF_GRID_BOX + page.appendLine('
'); // VP_PDF_BODY + + // preview box + page.appendFormatLine('
', VP_PDF_PREVIEW_BOX, 'vp-apiblock-scrollbar'); + page.appendFormatLine('', 'vp_codePreview'); + page.appendLine('
'); + + // button box + page.appendFormatLine('
', VP_PDF_BUTTON_BOX); + page.appendFormatLine('' + , 'vp-button', 'vp-pdf-btn', VP_PDF_BUTTON_PREVIEW, 'Code view'); + page.appendFormatLine('' + , 'vp-button cancel', 'vp-pdf-btn', VP_PDF_BUTTON_CANCEL, 'Cancel'); + page.appendFormatLine('
', VP_PDF_BUTTON_RUNADD); + page.appendFormatLine('' + , 'vp-button activated', VP_PDF_BUTTON_RUN, 'Apply to Board & Run Cell', 'Run'); + page.appendFormatLine('' + , 'vp-button activated', VP_PDF_BUTTON_DETAIL, '/nbextensions/visualpython/resource/arrow_short_up.svg'); + page.appendFormatLine('
', VP_PDF_DETAIL_BOX, 'vp-cursor'); + page.appendFormatLine('
{3}
', VP_PDF_DETAIL_ITEM, 'apply', 'Apply to Board', 'Apply'); + page.appendFormatLine('
{3}
', VP_PDF_DETAIL_ITEM, 'add', 'Apply to Board & Add Cell', 'Add'); + page.appendLine('
'); // VP_PDF_DETAIL_BOX + page.appendLine('
'); // VP_PDF_BUTTON_RUNADD + page.appendLine('
'); // VP_PDF_BUTTON_BOX + + page.appendLine('
'); // VP_PDF_CONTAINER + page.appendLine('
'); // VP_PDF + + $('#vp-wrapper').append(page.toString()); + $(this.wrapSelector()).hide(); + } + + checkInstalled() { + var that = this; + // set state as 'Checking' + $(this.wrapSelector('.' + VP_PDF_INSTALL_BTN)).text('Checking...'); + // set disabled + if (!$(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).hasClass('disabled')) { + $(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).addClass('disabled'); + } + var checking = true; + + // check installed + Jupyter.notebook.kernel.execute( + pycode.PDF_SHOW, + { + iopub: { + output: function (msg) { + if (!checking) { + return; + } + if (msg.content['name'] == 'stderr' || msg.content['text'].includes('not found')) { + checking = false; + $(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).text('Install'); + // set enabled + if ($(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).hasClass('disabled')) { + $(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).removeClass('disabled'); + } + } else { + $(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).text('Installed'); + // set disabled + if (!$(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).hasClass('disabled')) { + $(that.wrapSelector('.' + VP_PDF_INSTALL_BTN)).addClass('disabled'); + } + } + } + } + } + ); + } + + unbindEvent() { + $(document).off(this.wrapSelector('*')); + + $(document).off('click', this.wrapSelector('.' + VP_PDF_CLOSE)); + $(document).off('click', this.wrapSelector('.' + VP_PDF_INSTALL_BTN)); + $(document).off('click', this.wrapSelector('.' + VP_PDF_CHECK_BTN)); + $(document).off('click', this.wrapSelector('.' + VP_PDF_IMPORT_BTN)); + $(document).off('click', this.wrapSelector('#vp_openFileNavigationBtn')); + $(document).off('click', this.wrapSelector('.' + VP_PDF_BUTTON_RUN)); + $(document).off('click', this.wrapSelector('.' + VP_PDF_BUTTON_DETAIL)); + $(document).off('click', this.wrapSelector('.' + VP_PDF_DETAIL_ITEM)); + $(document).off('click', this.wrapSelector('.' + VP_PDF_BUTTON_PREVIEW)); + $(document).off('click', this.wrapSelector('.' + VP_PDF_BUTTON_CANCEL)); + $(document).off('click.' + this.uuid); + } + + bindEvent() { + var that = this; + + // close popup + $(document).on('click', this.wrapSelector('.' + VP_PDF_CLOSE), function () { + that.close(); + }); + + // click install + $(document).on('click', this.wrapSelector('.' + VP_PDF_INSTALL_BTN + ':not(.disabled)'), function () { + vpCommon.cellExecute([{ command: pycode.PDF_INSTALL1, exec: true, type: 'code' }]); + vpCommon.cellExecute([{ command: pycode.PDF_INSTALL2, exec: true, type: 'code' }]); + }); + + // click check installed + $(document).on('click', this.wrapSelector('.' + VP_PDF_CHECK_BTN), function () { + that.checkInstalled(); + }); + + // click import + $(document).on('click', this.wrapSelector('.' + VP_PDF_IMPORT_BTN), function () { + vpCommon.cellExecute([{ command: pycode.PDF_IMPORT, exec: true, type: 'code' }]); + vpCommon.cellExecute([{ command: pycode.PDF_FUNC, exec: true, type: 'code' }]); + }); + + // click file navigation button + $(document).on('click', this.wrapSelector('#vp_openFileNavigationBtn'), async function() { + var loadURLstyle = Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH; + var loadURLhtml = Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.SOURCE_PATH + "component/fileNavigation/index.html"; + + vpCommon.loadCss( loadURLstyle + "component/fileNavigation.css"); + + await $(`
`) + .load(loadURLhtml, () => { + + $('#vp_fileNavigation').removeClass("hide"); + $('#vp_fileNavigation').addClass("show"); + + var { vp_init + , vp_bindEventFunctions } = fileNavigation; + + fileNavigation.vp_init(that); + fileNavigation.vp_bindEventFunctions(); + }) + .appendTo("#site"); + }); + + // click run + $(document).on('click', this.wrapSelector('.' + VP_PDF_BUTTON_RUN), function(event) { + that.apply(true, true); + that.close(); + }); + + // click detail button + $(document).on('click', this.wrapSelector('.' + VP_PDF_BUTTON_DETAIL), function(evt) { + evt.stopPropagation(); + $(that.wrapSelector('.' + VP_PDF_DETAIL_BOX)).show(); + }); + + // click add / apply + $(document).on('click', this.wrapSelector('.' + VP_PDF_DETAIL_ITEM), function() { + var type = $(this).data('type'); + if (type == 'add') { + that.apply(true); + that.close(); + } else if (type == 'apply') { + that.apply(); + that.close(); + } + }); + + // click preview + $(document).on('click', this.wrapSelector('.' + VP_PDF_BUTTON_PREVIEW), function(evt) { + // evt.stopPropagation(); + if (that.previewOpened) { + that.closePreview(); + } else { + that.openPreview(); + } + }); + + // click cancel + $(document).on('click', this.wrapSelector('.' + VP_PDF_BUTTON_CANCEL), function(event) { + that.close(); + }); + + // click others + $(document).on('click.' + this.uuid, function(evt) { + if (!$(evt.target).hasClass(VP_PDF_BUTTON_DETAIL)) { + $(that.wrapSelector('.' + VP_PDF_DETAIL_BOX)).hide(); + } + if (!$(evt.target).hasClass(VP_PDF_BUTTON_PREVIEW) + && !$(evt.target).hasClass(VP_PDF_PREVIEW_BOX) + && $(that.wrapSelector('.' + VP_PDF_PREVIEW_BOX)).has(evt.target).length === 0) { + that.closePreview(); + } + }); + } + + apply(addCell=false, runCell=false) { + var code = this.generateCode(); + + // save state for block + this.saveState(); + + if (this.pageThis) { + $(this.pageThis.wrapSelector('#' + this.targetId)).val(code); + $(this.pageThis.wrapSelector('#' + this.targetId)).trigger({ + type: 'pdf_run', + title: 'PDF', + code: code, + state: this.state, + addCell: addCell, + runCell: runCell + }); + } else { + $(vpCommon.wrapSelector('#' + this.targetId)).val(code); + $(vpCommon.wrapSelector('#' + this.targetId)).trigger({ + type: 'pdf_run', + title: 'PDF', + code: code, + state: this.state, + addCell: addCell, + runCell: runCell + }); + } + } + + /** + * Open preview box + */ + openPreview() { + $(this.wrapSelector('.' + VP_PDF_PREVIEW_BOX)).show(); + + if (!this.cmpreviewall) { + // codemirror setting + this.cmpreviewall = codemirror.fromTextArea($(this.wrapSelector('#vp_codePreview'))[0], { + mode: { + name: 'python', + version: 3, + singleLineStringErrors: false + }, // text-cell(markdown cell) set to 'htmlmixed' + height: '100%', + width: '100%', + indentUnit: 4, + matchBrackets: true, + readOnly:true, + autoRefresh: true, + theme: "ipython", + extraKeys: {"Enter": "newlineAndIndentContinueMarkdownList"}, + scrollbarStyle: "null" + }); + } else { + this.cmpreviewall.refresh(); + } + + // get current code + var code = this.generateCode(); + this.cmpreviewall.setValue(code); + this.cmpreviewall.save(); + + var that = this; + setTimeout(function() { + that.cmpreviewall.refresh(); + },1); + + this.previewOpened = true; + } + + /** + * Close preview box + */ + closePreview() { + this.previewOpened = false; + $(this.wrapSelector('.' + VP_PDF_PREVIEW_BOX)).hide(); + } + + /** + * Generate code + * @returns generatedCode + */ + generateCode() { + var code = new sb.StringBuilder(); + + var filePath = $(this.wrapSelector('.' + VP_PDF_FILEPATH)).val(); + var allocateTo = $(this.wrapSelector('#vp_pdfReturn')).val(); + + // FIXME: change it after implementing filenavigation multi-select + code.appendFormatLine("pdf_lst = ['{0}']", filePath); + + if (allocateTo && allocateTo != '') { + code.appendFormat('{0} = ', allocateTo); + } + code.append('vp_pdf_get_sentence(pdf_lst)'); + + if (allocateTo && allocateTo != '') { + code.appendLine(); + code.append(allocateTo); + } + + return code.toString(); + } + } + + return PDF; + +}); /* function, define */ + +/* End of file */