diff --git a/install/package.json b/install/package.json index 18929861ba..2b96dc8d08 100644 --- a/install/package.json +++ b/install/package.json @@ -98,7 +98,7 @@ "multer": "2.0.2", "nconf": "0.13.0", "nodebb-plugin-2factor": "7.6.1", - "nodebb-plugin-composer-default": "10.3.1", + "nodebb-plugin-composer-default": "file:vendor/nodebb-plugin-composer-default", "nodebb-plugin-dbsearch": "6.3.4", "nodebb-plugin-emoji": "6.0.5", "nodebb-plugin-emoji-android": "4.1.1", @@ -204,4 +204,4 @@ "url": "https://github.com/barisusakli" } ] -} +} \ No newline at end of file diff --git a/vendor/nodebb-plugin-composer-default/.gitattributes b/vendor/nodebb-plugin-composer-default/.gitattributes new file mode 100644 index 0000000000..412eeda78d --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/vendor/nodebb-plugin-composer-default/.gitignore b/vendor/nodebb-plugin-composer-default/.gitignore new file mode 100644 index 0000000000..a2430ed3ad --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/.gitignore @@ -0,0 +1,228 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +.vscode + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + +# can't have it committed because it interferes with the package-lock.json +# generated by each individual install +package-lock.json +yarn.lock + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +sftp-config.json +node_modules/ + +*.sublime-project +*.sublime-workspace \ No newline at end of file diff --git a/vendor/nodebb-plugin-composer-default/.npmignore b/vendor/nodebb-plugin-composer-default/.npmignore new file mode 100644 index 0000000000..d913c17382 --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/.npmignore @@ -0,0 +1,2 @@ +sftp-config.json +node_modules/ diff --git a/vendor/nodebb-plugin-composer-default/LICENSE b/vendor/nodebb-plugin-composer-default/LICENSE new file mode 100644 index 0000000000..b8658d3aa1 --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2016 NodeBB Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/vendor/nodebb-plugin-composer-default/README.md b/vendor/nodebb-plugin-composer-default/README.md new file mode 100644 index 0000000000..7bcfff9aff --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/README.md @@ -0,0 +1,11 @@ +# Default Composer for NodeBB + +This plugin activates the default composer for NodeBB. It is activated by default, but can be swapped out as necessary. + +## Screenshots + +### Desktop +![Desktop Composer](screenshots/desktop.png?raw=true) + +### Mobile Devices +![Mobile Composer](screenshots/mobile.png?raw=true) \ No newline at end of file diff --git a/vendor/nodebb-plugin-composer-default/controllers.js b/vendor/nodebb-plugin-composer-default/controllers.js new file mode 100644 index 0000000000..cef271849f --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/controllers.js @@ -0,0 +1,11 @@ +'use strict'; + +const Controllers = {}; + +Controllers.renderAdminPage = function (req, res) { + res.render('admin/plugins/composer-default', { + title: 'Composer (Default)', + }); +}; + +module.exports = Controllers; diff --git a/vendor/nodebb-plugin-composer-default/eslint.config.mjs b/vendor/nodebb-plugin-composer-default/eslint.config.mjs new file mode 100644 index 0000000000..f0da2045c6 --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/eslint.config.mjs @@ -0,0 +1,10 @@ +'use strict'; + +import serverConfig from 'eslint-config-nodebb'; +import publicConfig from 'eslint-config-nodebb/public'; + +export default [ + ...publicConfig, + ...serverConfig, +]; + diff --git a/vendor/nodebb-plugin-composer-default/library.js b/vendor/nodebb-plugin-composer-default/library.js new file mode 100644 index 0000000000..113e712b37 --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/library.js @@ -0,0 +1,334 @@ +'use strict'; + +const url = require('url'); + +const nconf = require.main.require('nconf'); +const validator = require('validator'); + +const plugins = require.main.require('./src/plugins'); +const topics = require.main.require('./src/topics'); +const categories = require.main.require('./src/categories'); +const posts = require.main.require('./src/posts'); +const user = require.main.require('./src/user'); +const meta = require.main.require('./src/meta'); +const privileges = require.main.require('./src/privileges'); +const translator = require.main.require('./src/translator'); +const utils = require.main.require('./src/utils'); +const helpers = require.main.require('./src/controllers/helpers'); +const SocketPlugins = require.main.require('./src/socket.io/plugins'); +const socketMethods = require('./websockets'); + +const plugin = module.exports; + +plugin.socketMethods = socketMethods; + +plugin.init = async function (data) { + const { router } = data; + const routeHelpers = require.main.require('./src/routes/helpers'); + const controllers = require('./controllers'); + SocketPlugins.composer = socketMethods; + routeHelpers.setupAdminPageRoute(router, '/admin/plugins/composer-default', controllers.renderAdminPage); +}; + +plugin.appendConfig = async function (config) { + config['composer-default'] = await meta.settings.get('composer-default'); + return config; +}; + +plugin.addAdminNavigation = async function (header) { + header.plugins.push({ + route: '/plugins/composer-default', + icon: 'fa-edit', + name: 'Composer (Default)', + }); + return header; +}; + +plugin.addPrefetchTags = async function (hookData) { + const { req } = hookData; + if (req.uid > 0) { + const prefetch = [ + '/assets/templates/composer.js', + `/assets/language/${meta.config.defaultLang || 'en-GB'}/topic.json`, + `/assets/language/${meta.config.defaultLang || 'en-GB'}/modules.json`, + `/assets/language/${meta.config.defaultLang || 'en-GB'}/tags.json`, + ]; + + hookData.links = hookData.links.concat(prefetch.map(path => ({ + rel: 'prefetch', + href: `${nconf.get('relative_path') + path}?${meta.config['cache-buster']}`, + }))); + } + + return hookData; +}; + +plugin.getFormattingOptions = async function () { + const defaultVisibility = { + mobile: true, + desktop: true, + + // op or reply + main: true, + reply: true, + }; + let payload = { + defaultVisibility, + options: [ + { + name: 'tags', + title: '[[global:tags.tags]]', + className: 'fa fa-tags', + visibility: { + ...defaultVisibility, + desktop: false, + }, + }, + { + name: 'zen', + title: '[[modules:composer.zen-mode]]', + className: 'fa fa-arrows-alt', + visibility: defaultVisibility, + }, + ], + }; + if (parseInt(meta.config.allowTopicsThumbnail, 10) === 1) { + payload.options.push({ + name: 'thumbs', + title: '[[topic:composer.thumb-title]]', + className: 'fa fa-address-card-o', + badge: true, + visibility: { + ...defaultVisibility, + reply: false, + }, + }); + } + + payload = await plugins.hooks.fire('filter:composer.formatting', payload); + + payload.options.forEach((option) => { + option.visibility = { + ...defaultVisibility, + ...option.visibility || {}, + }; + }); + + return payload ? payload.options : null; +}; + +plugin.filterComposerBuild = async function (hookData) { + const { req, res } = hookData; + + if (req.query.p) { + try { + const a = url.parse(req.query.p, true, true); + return helpers.redirect(res, `/${(a.path || '').replace(/^\/*/, '')}`); + } catch (e) { + return helpers.redirect(res, '/'); + } + } else if (!req.query.pid && !req.query.tid && !req.query.cid) { + return helpers.redirect(res, '/'); + } + + await checkPrivileges(req, res); + + const [ + isMainPost, + postData, + topicData, + categoryData, + isAdmin, + isMod, + formatting, + tagWhitelist, + globalPrivileges, + canTagTopics, + canScheduleTopics, + ] = await Promise.all([ + posts.isMain(req.query.pid), + getPostData(req), + getTopicData(req), + categories.getCategoryFields(req.query.cid, [ + 'name', 'icon', 'color', 'bgColor', 'backgroundImage', 'imageClass', 'minTags', 'maxTags', + ]), + user.isAdministrator(req.uid), + isModerator(req), + plugin.getFormattingOptions(), + getTagWhitelist(req.query, req.uid), + privileges.global.get(req.uid), + canTag(req), + canSchedule(req), + ]); + + const isEditing = !!req.query.pid; + const isGuestPost = postData && parseInt(postData.uid, 10) === 0; + const save_id = utils.generateSaveId(req.uid); + const discardRoute = generateDiscardRoute(req, topicData); + const body = await generateBody(req, postData); + + let action = 'topics.post'; + let isMain = isMainPost; + if (req.query.tid) { + action = 'posts.reply'; + } else if (req.query.pid) { + action = 'posts.edit'; + } else { + isMain = true; + } + globalPrivileges['topics:tag'] = canTagTopics; + const cid = parseInt(req.query.cid, 10); + const topicTitle = topicData && topicData.title ? + topicData.title : + validator.escape(String(req.query.title || '')); + return { + req: req, + res: res, + templateData: { + disabled: !req.query.pid && !req.query.tid && !req.query.cid, + pid: parseInt(req.query.pid, 10), + tid: parseInt(req.query.tid, 10), + cid: cid || (topicData ? topicData.cid : null), + action: action, + toPid: parseInt(req.query.toPid, 10), + discardRoute: discardRoute, + + resizable: false, + allowTopicsThumbnail: parseInt(meta.config.allowTopicsThumbnail, 10) === 1 && isMain, + + // can't use title property as that is used for page title + topicTitle: topicTitle, + titleLength: topicTitle ? topicTitle.length : 0, + titleLabel: translator.compile( + isEditing ? + 'topic:composer.editing-in' : + 'topic:composer.replying-to', + `"${topicTitle}"` + ), + + topic: topicData, + thumb: topicData ? topicData.thumb : '', + body: body, + + isMain: isMain, + isTopicOrMain: !!req.query.cid || isMain, + maximumTitleLength: meta.config.maximumTitleLength, + maximumPostLength: meta.config.maximumPostLength, + minimumTagLength: meta.config.minimumTagLength || 3, + maximumTagLength: meta.config.maximumTagLength || 15, + tagWhitelist: tagWhitelist, + selectedCategory: cid ? categoryData : null, + minTags: categoryData.minTags, + maxTags: categoryData.maxTags, + + isTopic: !!req.query.cid, + isEditing: isEditing, + canSchedule: canScheduleTopics, + showHandleInput: meta.config.allowGuestHandles === 1 && + (req.uid === 0 || (isEditing && isGuestPost && (isAdmin || isMod))), + handle: postData ? postData.handle || '' : undefined, + formatting: formatting, + isAdminOrMod: isAdmin || isMod, + save_id: save_id, + privileges: globalPrivileges, + 'composer:showHelpTab': meta.config['composer:showHelpTab'] === 1, + }, + }; +}; + +async function checkPrivileges(req, res) { + const notAllowed = ( + (req.query.cid && !await privileges.categories.can('topics:create', req.query.cid, req.uid)) || + (req.query.tid && !await privileges.topics.can('topics:reply', req.query.tid, req.uid)) || + (req.query.pid && !await privileges.posts.can('posts:edit', req.query.pid, req.uid)) + ); + + if (notAllowed) { + await helpers.notAllowed(req, res); + } +} + +function generateDiscardRoute(req, topicData) { + if (req.query.cid) { + return `${nconf.get('relative_path')}/category/${validator.escape(String(req.query.cid))}`; + } else if ((req.query.tid || req.query.pid)) { + if (topicData) { + return `${nconf.get('relative_path')}/topic/${topicData.slug}`; + } + return `${nconf.get('relative_path')}/`; + } +} + +async function generateBody(req, postData) { + let body = ''; + // Quoted reply + if (req.query.toPid && parseInt(req.query.quoted, 10) === 1 && postData) { + const username = await user.getUserField(postData.uid, 'username'); + const translated = await translator.translate(`[[modules:composer.user-said, ${username}]]`); + body = `${translated}\n` + + `> ${postData ? `${postData.content.replace(/\n/g, '\n> ')}\n\n` : ''}`; + } else if (req.query.body || req.query.content) { + body = validator.escape(String(req.query.body || req.query.content)); + } + body = postData ? postData.content : ''; + return translator.escape(body); +} + +async function getPostData(req) { + if (!req.query.pid && !req.query.toPid) { + return null; + } + + return await posts.getPostData(req.query.pid || req.query.toPid); +} + +async function getTopicData(req) { + if (req.query.tid) { + return await topics.getTopicData(req.query.tid); + } else if (req.query.pid) { + return await topics.getTopicDataByPid(req.query.pid); + } + return null; +} + +async function isModerator(req) { + if (!req.loggedIn) { + return false; + } + const cid = cidFromQuery(req.query); + return await user.isModerator(req.uid, cid); +} + +async function canTag(req) { + if (parseInt(req.query.cid, 10)) { + return await privileges.categories.can('topics:tag', req.query.cid, req.uid); + } + return true; +} + +async function canSchedule(req) { + if (parseInt(req.query.cid, 10)) { + return await privileges.categories.can('topics:schedule', req.query.cid, req.uid); + } + return false; +} + +async function getTagWhitelist(query, uid) { + const cid = await cidFromQuery(query); + const [tagWhitelist, isAdminOrMod] = await Promise.all([ + categories.getTagWhitelist([cid]), + privileges.categories.isAdminOrMod(cid, uid), + ]); + return categories.filterTagWhitelist(tagWhitelist[0], isAdminOrMod); +} + +async function cidFromQuery(query) { + if (query.cid) { + return query.cid; + } else if (query.tid) { + return await topics.getTopicField(query.tid, 'cid'); + } else if (query.pid) { + return await posts.getCidByPid(query.pid); + } + return null; +} diff --git a/vendor/nodebb-plugin-composer-default/package.json b/vendor/nodebb-plugin-composer-default/package.json new file mode 100644 index 0000000000..216433eb2d --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/package.json @@ -0,0 +1,40 @@ +{ + "name": "nodebb-plugin-composer-default", + "version": "10.3.9", + "description": "Default composer for NodeBB", + "main": "library.js", + "repository": { + "type": "git", + "url": "https://github.com/NodeBB/nodebb-plugin-composer-default" + }, + "scripts": { + "lint": "eslint ." + }, + "keywords": [ + "nodebb", + "plugin", + "composer", + "markdown" + ], + "author": { + "name": "NodeBB Team", + "email": "sales@nodebb.org" + }, + "license": "MIT", + "bugs": { + "url": "https://github.com/NodeBB/nodebb-plugin-composer-default/issues" + }, + "readmeFilename": "README.md", + "nbbpm": { + "compatibility": "^4.5.0" + }, + "dependencies": { + "screenfull": "^5.0.2", + "validator": "^13.7.0" + }, + "devDependencies": { + "eslint": "^9.25.1", + "eslint-config-nodebb": "^1.1.4", + "eslint-plugin-import": "^2.31.0" + } +} diff --git a/vendor/nodebb-plugin-composer-default/plugin.json b/vendor/nodebb-plugin-composer-default/plugin.json new file mode 100644 index 0000000000..c75ef14259 --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/plugin.json @@ -0,0 +1,35 @@ +{ + "id": "nodebb-plugin-composer-default", + "url": "https://github.com/NodeBB/nodebb-plugin-composer-default", + "library": "library.js", + "hooks": [ + { "hook": "static:app.load", "method": "init" }, + { "hook": "filter:config.get", "method": "appendConfig" }, + { "hook": "filter:composer.build", "method": "filterComposerBuild" }, + { "hook": "filter:admin.header.build", "method": "addAdminNavigation" }, + { "hook": "filter:meta.getLinkTags", "method": "addPrefetchTags" } + ], + "scss": [ + "./static/scss/composer.scss" + ], + "scripts": [ + "./static/lib/client.js", + "./node_modules/screenfull/dist/screenfull.js" + ], + "modules": { + "composer.js": "./static/lib/composer.js", + "composer/categoryList.js": "./static/lib/composer/categoryList.js", + "composer/controls.js": "./static/lib/composer/controls.js", + "composer/drafts.js": "./static/lib/composer/drafts.js", + "composer/formatting.js": "./static/lib/composer/formatting.js", + "composer/preview.js": "./static/lib/composer/preview.js", + "composer/resize.js": "./static/lib/composer/resize.js", + "composer/scheduler.js": "./static/lib/composer/scheduler.js", + "composer/tags.js": "./static/lib/composer/tags.js", + "composer/uploads.js": "./static/lib/composer/uploads.js", + "composer/autocomplete.js": "./static/lib/composer/autocomplete.js", + "composer/post-queue.js": "./static/lib/composer/post-queue.js", + "../admin/plugins/composer-default.js": "./static/lib/admin.js" + }, + "templates": "static/templates" +} \ No newline at end of file diff --git a/vendor/nodebb-plugin-composer-default/screenshots/desktop.png b/vendor/nodebb-plugin-composer-default/screenshots/desktop.png new file mode 100644 index 0000000000..a6d4631e4e Binary files /dev/null and b/vendor/nodebb-plugin-composer-default/screenshots/desktop.png differ diff --git a/vendor/nodebb-plugin-composer-default/screenshots/mobile.png b/vendor/nodebb-plugin-composer-default/screenshots/mobile.png new file mode 100644 index 0000000000..a50a01ea93 Binary files /dev/null and b/vendor/nodebb-plugin-composer-default/screenshots/mobile.png differ diff --git a/vendor/nodebb-plugin-composer-default/static/lib/admin.js b/vendor/nodebb-plugin-composer-default/static/lib/admin.js new file mode 100644 index 0000000000..cc693300ea --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/static/lib/admin.js @@ -0,0 +1,15 @@ +'use strict'; + +define('admin/plugins/composer-default', ['settings'], function (Settings) { + const ACP = {}; + + ACP.init = function () { + Settings.load('composer-default', $('.composer-default-settings')); + + $('#save').on('click', function () { + Settings.save('composer-default', $('.composer-default-settings')); + }); + }; + + return ACP; +}); diff --git a/vendor/nodebb-plugin-composer-default/static/lib/client.js b/vendor/nodebb-plugin-composer-default/static/lib/client.js new file mode 100644 index 0000000000..2b46e406b8 --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/static/lib/client.js @@ -0,0 +1,89 @@ +'use strict'; + +$(document).ready(function () { + $(window).on('action:app.load', function () { + require(['composer/drafts'], function (drafts) { + drafts.migrateGuest(); + drafts.loadOpen(); + }); + }); + + $(window).on('action:composer.topic.new', function (ev, data) { + if (config['composer-default'].composeRouteEnabled !== 'on') { + require(['composer'], function (composer) { + composer.newTopic({ + cid: data.cid, + title: data.title || '', + body: data.body || '', + tags: data.tags || [], + }); + }); + } else { + ajaxify.go( + 'compose?cid=' + data.cid + + (data.title ? '&title=' + encodeURIComponent(data.title) : '') + + (data.body ? '&body=' + encodeURIComponent(data.body) : '') + ); + } + }); + + $(window).on('action:composer.post.edit', function (ev, data) { + if (config['composer-default'].composeRouteEnabled !== 'on') { + require(['composer'], function (composer) { + composer.editPost({ pid: data.pid }); + }); + } else { + ajaxify.go('compose?pid=' + data.pid); + } + }); + + $(window).on('action:composer.post.new', function (ev, data) { + // backwards compatibility + data.body = data.body || data.text; + data.title = data.title || data.topicName; + if (config['composer-default'].composeRouteEnabled !== 'on') { + require(['composer'], function (composer) { + composer.newReply({ + tid: data.tid, + toPid: data.pid, + title: data.title, + body: data.body, + }); + }); + } else { + ajaxify.go( + 'compose?tid=' + data.tid + + (data.pid ? '&toPid=' + data.pid : '') + + (data.title ? '&title=' + encodeURIComponent(data.title) : '') + + (data.body ? '&body=' + encodeURIComponent(data.body) : '') + ); + } + }); + + $(window).on('action:composer.addQuote', function (ev, data) { + data.body = data.body || data.text; + data.title = data.title || data.topicName; + if (config['composer-default'].composeRouteEnabled !== 'on') { + require(['composer'], function (composer) { + var topicUUID = composer.findByTid(data.tid); + composer.addQuote({ + tid: data.tid, + toPid: data.pid, + selectedPid: data.selectedPid, + title: data.title, + username: data.username, + body: data.body, + uuid: topicUUID, + }); + }); + } else { + ajaxify.go('compose?tid=' + data.tid + '&toPid=' + data.pid + '"ed=1&username=' + data.username); + } + }); + + $(window).on('action:composer.enhance', function (ev, data) { + require(['composer'], function (composer) { + composer.enhance(data.container); + }); + }); +}); diff --git a/vendor/nodebb-plugin-composer-default/static/lib/composer.js b/vendor/nodebb-plugin-composer-default/static/lib/composer.js new file mode 100644 index 0000000000..a47bac9c72 --- /dev/null +++ b/vendor/nodebb-plugin-composer-default/static/lib/composer.js @@ -0,0 +1,893 @@ +'use strict'; + +define('composer', [ + 'taskbar', + 'translator', + 'composer/uploads', + 'composer/formatting', + 'composer/drafts', + 'composer/tags', + 'composer/categoryList', + 'composer/preview', + 'composer/resize', + 'composer/autocomplete', + 'composer/scheduler', + 'composer/post-queue', + 'scrollStop', + 'topicThumbs', + 'api', + 'bootbox', + 'alerts', + 'hooks', + 'messages', + 'search', + 'screenfull', +], function (taskbar, translator, uploads, formatting, drafts, tags, + categoryList, preview, resize, autocomplete, scheduler, postQueue, scrollStop, + topicThumbs, api, bootbox, alerts, hooks, messagesModule, search, screenfull) { + var composer = { + active: undefined, + posts: {}, + bsEnvironment: undefined, + formatting: undefined, + }; + + $(window).off('resize', onWindowResize).on('resize', onWindowResize); + onWindowResize(); + + $(window).on('action:composer.topics.post', function (ev, data) { + localStorage.removeItem('category:' + data.data.cid + ':bookmark'); + localStorage.removeItem('category:' + data.data.cid + ':bookmark:clicked'); + }); + + $(window).on('popstate', function () { + var env = utils.findBootstrapEnvironment(); + if (composer.active && (env === 'xs' || env === 'sm')) { + if (!composer.posts[composer.active].modified) { + composer.discard(composer.active); + if (composer.discardConfirm && composer.discardConfirm.length) { + composer.discardConfirm.modal('hide'); + delete composer.discardConfirm; + } + return; + } + + composer.discardConfirm = bootbox.confirm('[[modules:composer.discard]]', function (confirm) { + if (confirm) { + composer.discard(composer.active); + } else { + composer.posts[composer.active].modified = true; + } + }); + composer.posts[composer.active].modified = false; + } + }); + + function removeComposerHistory() { + var env = composer.bsEnvironment; + if (ajaxify.data.template.compose === true || env === 'xs' || env === 'sm') { + history.back(); + } + } + + function onWindowResize() { + var env = utils.findBootstrapEnvironment(); + var isMobile = env === 'xs' || env === 'sm'; + + if (preview.toggle) { + if (preview.env !== env && isMobile) { + preview.env = env; + preview.toggle(false); + } + preview.env = env; + } + + if (composer.active !== undefined) { + resize.reposition($('.composer[data-uuid="' + composer.active + '"]')); + + if (!isMobile && window.location.pathname.startsWith(config.relative_path + '/compose')) { + /* + * If this conditional is met, we're no longer in mobile/tablet + * resolution but we've somehow managed to have a mobile + * composer load, so let's go back to the topic + */ + history.back(); + } else if (isMobile && !window.location.pathname.startsWith(config.relative_path + '/compose')) { + /* + * In this case, we're in mobile/tablet resolution but the composer + * that loaded was a regular composer, so let's fix the address bar + */ + mobileHistoryAppend(); + } + } + composer.bsEnvironment = env; + } + + function alreadyOpen(post) { + // If a composer for the same cid/tid/pid is already open, return the uuid, else return bool false + var type; + var id; + + if (post.hasOwnProperty('cid')) { + type = 'cid'; + } else if (post.hasOwnProperty('tid')) { + type = 'tid'; + } else if (post.hasOwnProperty('pid')) { + type = 'pid'; + } + + id = post[type]; + + // Find a match + for (const uuid of Object.keys(composer.posts)) { + if (composer.posts[uuid].hasOwnProperty(type) && id === composer.posts[uuid][type]) { + return uuid; + } + } + + // No matches... + return false; + } + + function push(post) { + if (!post) { + return; + } + + var uuid = utils.generateUUID(); + var existingUUID = alreadyOpen(post); + + if (existingUUID) { + taskbar.updateActive(existingUUID); + return composer.load(existingUUID); + } + + var actionText = '[[topic:composer.new-topic]]'; + if (post.action === 'posts.reply') { + actionText = '[[topic:composer.replying-to]]'; + } else if (post.action === 'posts.edit') { + actionText = '[[topic:composer.editing-in]]'; + } + + translator.translate(actionText, function (translatedAction) { + taskbar.push('composer', uuid, { + title: translatedAction.replace('%1', '"' + post.title + '"'), + }); + }); + + composer.posts[uuid] = post; + composer.load(uuid); + } + + async function composerAlert(post_uuid, message) { + $('.composer[data-uuid="' + post_uuid + '"]').find('.composer-submit').removeAttr('disabled'); + + const { showAlert } = await hooks.fire('filter:composer.error', { post_uuid, message, showAlert: true }); + + if (showAlert) { + alerts.alert({ + type: 'danger', + timeout: 10000, + title: '', + message: message, + alert_id: 'post_error', + }); + } + } + + composer.findByTid = function (tid) { + // Iterates through the initialised composers and returns the uuid of the matching composer + for (const uuid of Object.keys(composer.posts)) { + if (composer.posts[uuid].hasOwnProperty('tid') && String(composer.posts[uuid].tid) === String(tid)) { + return uuid; + } + } + + return null; + }; + + composer.addButton = function (iconClass, onClick, title) { + formatting.addButton(iconClass, onClick, title); + }; + + composer.newTopic = async (data) => { + let pushData = { + save_id: data.save_id, + action: 'topics.post', + cid: data.cid, + handle: data.handle, + title: data.title || '', + body: data.body || '', + tags: data.tags || [], + thumbs: data.thumbs || [], + modified: !!((data.title && data.title.length) || (data.body && data.body.length)), + isMain: true, + }; + + ({ pushData } = await hooks.fire('filter:composer.topic.push', { + data: data, + pushData: pushData, + })); + + push(pushData); + }; + + composer.addQuote = function (data) { + // tid, toPid, selectedPid, title, username, text, uuid + data.uuid = data.uuid || composer.active; + + var escapedTitle = (data.title || '') + .replace(/([\\`*_{}[\]()#+\-.!])/g, '\\$1') + .replace(/\[/g, '[') + .replace(/\]/g, ']') + .replace(/%/g, '%') + .replace(/,/g, ','); + + if (data.body) { + data.body = '> ' + data.body.replace(/\n/g, '\n> ') + '\n\n'; + } + + const composerTid = data.uuid ? composer.posts[data.uuid].tid : data.tid; + const inDifferentTopic = parseInt(data.tid, 10) !== parseInt(composerTid, 10); + const useTopicLink = data.title && (data.selectedPid || data.toPid) && inDifferentTopic; + const postHref = `${config.relative_path}/post/${encodeURIComponent(data.selectedPid || data.toPid)}`; + const topicLink = `[${escapedTitle}](${postHref})`; + + const quoteKey = useTopicLink ? + '> [[modules:composer.user-said-in, ' + data.username + ', ' + topicLink + ']]\n>\n' : + '> [[modules:composer.user-said, ' + data.username + ', ' + postHref + ']]\n>\n'; + + if (data.uuid === undefined) { + composer.newReply({ + tid: data.tid, + toPid: data.toPid, + title: data.title, + body: quoteKey + data.body, + }); + return; + } else if (data.uuid !== composer.active) { + // If the composer is not currently active, activate it + composer.load(data.uuid); + } + + var postContainer = $('.composer[data-uuid="' + data.uuid + '"]'); + var bodyEl = postContainer.find('textarea'); + var prevText = bodyEl.val(); + + translator.translate(quoteKey, config.defaultLang, function (translated) { + composer.posts[data.uuid].body = (prevText.length ? prevText + '\n\n' : '') + translated + data.body; + bodyEl.val(composer.posts[data.uuid].body); + focusElements(postContainer); + preview.render(postContainer); + }); + }; + + composer.newReply = function (data) { + translator.translate(data.body, config.defaultLang, function (translated) { + push({ + save_id: data.save_id, + action: 'posts.reply', + tid: data.tid, + toPid: data.toPid, + title: data.title, + body: translated, + modified: !!(translated && translated.length), + isMain: false, + }); + }); + }; + + composer.editPost = function (data) { + // pid, text + socket.emit('plugins.composer.push', data.pid, function (err, postData) { + if (err) { + return alerts.error(err); + } + postData.save_id = data.save_id; + postData.action = 'posts.edit'; + postData.pid = data.pid; + postData.modified = false; + if (data.body) { + postData.body = data.body; + postData.modified = true; + } + if (data.title) { + postData.title = data.title; + postData.modified = true; + } + push(postData); + }); + }; + + composer.load = function (post_uuid) { + var postContainer = $('.composer[data-uuid="' + post_uuid + '"]'); + if (postContainer.length) { + activate(post_uuid); + resize.reposition(postContainer); + focusElements(postContainer); + onShow(); + } else if (composer.formatting) { + createNewComposer(post_uuid); + } else { + socket.emit('plugins.composer.getFormattingOptions', function (err, options) { + if (err) { + return alerts.error(err); + } + composer.formatting = options; + createNewComposer(post_uuid); + }); + } + }; + + composer.enhance = function (postContainer, post_uuid, postData) { + /* + This method enhances a composer container with client-side sugar (preview, etc) + Everything in here also applies to the /compose route + */ + + if (!post_uuid && !postData) { + post_uuid = utils.generateUUID(); + composer.posts[post_uuid] = ajaxify.data; + postData = ajaxify.data; + postContainer.attr('data-uuid', post_uuid); + } + + categoryList.init(postContainer, composer.posts[post_uuid]); + scheduler.init(postContainer, composer.posts); + + formatting.addHandler(postContainer); + formatting.addComposerButtons(); + preview.handleToggler(postContainer); + postQueue.showAlert(postContainer, postData); + uploads.initialize(post_uuid); + tags.init(postContainer, composer.posts[post_uuid]); + autocomplete.init(postContainer, post_uuid); + + postContainer.on('change', 'input, textarea', function () { + composer.posts[post_uuid].modified = true; + }); + + postContainer.on('click', '.composer-submit', function (e) { + e.preventDefault(); + e.stopPropagation(); // Other click events bring composer back to active state which is undesired on submit + + $(this).attr('disabled', true); + post(post_uuid); + }); + + require(['mousetrap'], function (mousetrap) { + mousetrap(postContainer.get(0)).bind('mod+enter', function () { + postContainer.find('.composer-submit').attr('disabled', true); + post(post_uuid); + }); + }); + + postContainer.find('.composer-discard').on('click', function (e) { + e.preventDefault(); + + if (!composer.posts[post_uuid].modified) { + composer.discard(post_uuid); + return removeComposerHistory(); + } + + formatting.exitFullscreen(); + + const btn = $(this).prop('disabled', true); + bootbox.confirm('[[modules:composer.discard]]', function (confirm) { + if (confirm) { + composer.discard(post_uuid); + removeComposerHistory(); + } + btn.prop('disabled', false); + }); + }); + + postContainer.find('.composer-minimize, .minimize .trigger').on('click', function (e) { + e.preventDefault(); + e.stopPropagation(); + composer.minimize(post_uuid); + }); + + const textareaEl = postContainer.find('textarea'); + textareaEl.on('input propertychange', utils.debounce(function () { + preview.render(postContainer); + }, 250)); + + textareaEl.on('scroll', function () { + preview.matchScroll(postContainer); + }); + + drafts.init(postContainer, postData); + const draft = drafts.get(postData.save_id); + + preview.render(postContainer, function () { + preview.matchScroll(postContainer); + }); + + if (postData.action === 'posts.edit' && !utils.isNumber(postData.pid)) { + handleRemotePid(postContainer); + } + handleHelp(postContainer); + handleSearch(postContainer); + focusElements(postContainer); + if (postData.action === 'posts.edit' || postData.action === 'topics.post') { + composer.updateThumbCount(post_uuid, postContainer); + } + + // Hide "zen mode" if fullscreen API is not enabled/available (ahem, iOS...) + if (!screenfull.isEnabled) { + $('[data-format="zen"]').parent().addClass('hidden'); + } + + hooks.fire('action:composer.enhanced', { postContainer, postData, draft }); + }; + + async function getSelectedCategory(postData) { + const { template } = ajaxify.data; + const { cid } = postData; + if ((template.category || template.world) && String(cid) === String(ajaxify.data.cid)) { + // no need to load data if we are already on the category page + return ajaxify.data; + } else if (cid) { + const categoryUrl = cid !== -1 ? `/api/category/${encodeURIComponent(postData.cid)}` : `/api/world`; + return await api.get(categoryUrl, {}); + } + return null; + } + + async function createNewComposer(post_uuid) { + var postData = composer.posts[post_uuid]; + + var isTopic = postData ? postData.hasOwnProperty('cid') : false; + var isMain = postData ? !!postData.isMain : false; + var isEditing = postData ? !!postData.pid : false; + var isGuestPost = postData ? parseInt(postData.uid, 10) === 0 : false; + const isScheduled = postData.timestamp > Date.now(); + + postData.category = await getSelectedCategory(postData); + const privileges = postData.category ? postData.category.privileges : ajaxify.data.privileges; + const topicTemplate = isTopic && postData.category ? postData.category.topicTemplate : ''; + + let data = { + topicTitle: postData.title, + titleLength: postData.title.length, + titleLabel: translator.compile( + isEditing ? + 'topic:composer.editing-in' : + 'topic:composer.replying-to', + `"${postData.title}"` + ), + body: utils.escapeHTML(translator.escape(postData.body) || topicTemplate), + mobile: composer.bsEnvironment === 'xs' || composer.bsEnvironment === 'sm', + resizable: true, + thumb: postData.thumb, + isTopicOrMain: isTopic || isMain, + maximumTitleLength: config.maximumTitleLength, + maximumPostLength: config.maximumPostLength, + minimumTagLength: config.minimumTagLength, + maximumTagLength: config.maximumTagLength, + 'composer:showHelpTab': config['composer:showHelpTab'], + isTopic: isTopic, + isEditing: isEditing, + canSchedule: !!(isMain && privileges && + ((privileges['topics:schedule'] && !isEditing) || (isScheduled && privileges.view_scheduled))), + canUploadImage: app.user.privileges['upload:post:image'] && (config.maximumFileSize > 0 || app.user.isAdmin), + canUploadFile: app.user.privileges['upload:post:file'] && (config.maximumFileSize > 0 || app.user.isAdmin), + showHandleInput: config.allowGuestHandles && + (app.user.uid === 0 || (isEditing && isGuestPost && app.user.isAdmin)), + handle: postData ? postData.handle || '' : undefined, + formatting: composer.formatting, + tagWhitelist: postData.category ? postData.category.tagWhitelist : ajaxify.data.tagWhitelist, + privileges: app.user.privileges, + selectedCategory: postData.category, + submitOptions: [ + // Add items using `filter:composer.create`, or just add them to the