/* eslint-disable max-lines */ require('./metadataEditor.less'); const React = require('react'); const createClass = require('create-react-class'); const _ = require('lodash'); import request from '../../utils/request-middleware.js'; const Nav = require('naturalcrit/nav/nav.jsx'); const Combobox = require('client/components/combobox.jsx'); const TagInput = require('../tagInput/tagInput.jsx'); const Themes = require('themes/themes.json'); const validations = require('./validations.js'); const SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder']; const homebreweryThumbnail = require('../../thumbnail.png'); const callIfExists = (val, fn, ...args)=>{ if(val[fn]) { val[fn](...args); } }; const MetadataEditor = createClass({ displayName : 'MetadataEditor', getDefaultProps : function() { return { metadata : { editId : null, shareId : null, title : '', description : '', thumbnail : '', tags : [], published : false, authors : [], systems : [], renderer : 'legacy', theme : '5ePHB', lang : 'en' }, onChange : ()=>{}, reportError : ()=>{} }; }, getInitialState : function(){ return { showThumbnail : true }; }, toggleThumbnailDisplay : function(){ this.setState({ showThumbnail : !this.state.showThumbnail }); }, renderThumbnail : function(){ if(!this.state.showThumbnail) return; return ; }, handleFieldChange : function(name, e){ // load validation rules, and check input value against them const inputRules = validations[name] ?? []; const validationErr = inputRules.map((rule)=>rule(e.target.value)).filter(Boolean); // if no validation rules, save to props if(validationErr.length === 0){ callIfExists(e.target, 'setCustomValidity', ''); this.props.onChange({ ...this.props.metadata, [name] : e.target.value }); } else { // if validation issues, display built-in browser error popup with each error. const errMessage = validationErr.map((err)=>{ return `- ${err}`; }).join('\n'); callIfExists(e.target, 'setCustomValidity', errMessage); callIfExists(e.target, 'reportValidity'); } }, handleSystem : function(system, e){ if(e.target.checked){ this.props.metadata.systems.push(system); } else { this.props.metadata.systems = _.without(this.props.metadata.systems, system); } this.props.onChange(this.props.metadata); }, handleRenderer : function(renderer, e){ if(e.target.checked){ this.props.metadata.renderer = renderer; if(renderer == 'legacy') this.props.metadata.theme = '5ePHB'; } this.props.onChange(this.props.metadata, 'renderer'); }, handlePublish : function(val){ this.props.onChange({ ...this.props.metadata, published : val }); }, handleTheme : function(theme){ this.props.metadata.renderer = theme.renderer; this.props.metadata.theme = theme.path; this.props.onChange(this.props.metadata, 'theme'); }, handleLanguage : function(languageCode){ this.props.metadata.lang = languageCode; this.props.onChange(this.props.metadata); }, handleDelete : function(){ if(this.props.metadata.authors && this.props.metadata.authors.length <= 1){ if(!confirm('Are you sure you want to delete this brew? Because you are the only owner of this brew, the document will be deleted permanently.')) return; if(!confirm('Are you REALLY sure? You will not be able to recover the document.')) return; } else { if(!confirm('Are you sure you want to remove this brew from your collection? This will remove you as an editor, but other owners will still be able to access the document.')) return; if(!confirm('Are you REALLY sure? You will lose editor access to this document.')) return; } request.delete(`/api/${this.props.metadata.googleId ?? ''}${this.props.metadata.editId}`) .send() .end((err, res)=>{ if(err) { this.props.reportError(err); } else { window.location.href = '/'; } }); }, renderSystems : function(){ return _.map(SYSTEMS, (val)=>{ return ; }); }, renderPublish : function(){ if(this.props.metadata.published){ return ; } else { return ; } }, renderDelete : function(){ if(!this.props.metadata.editId) return; return
; }, renderAuthors : function(){ let text = 'None.'; if(this.props.metadata.authors && this.props.metadata.authors.length){ text = this.props.metadata.authors.join(', '); } return
{text}
; }, renderThemeDropdown : function(){ if(!global.enable_themes) return; const mergedThemes = _.merge(Themes, this.props.userThemes); const listThemes = (renderer)=>{ return _.map(_.values(mergedThemes[renderer]), (theme)=>{ if(theme.path == this.props.metadata.shareId) return; const preview = theme.thumbnail || `/themes/${theme.renderer}/${theme.path}/dropdownPreview.png`; const texture = theme.thumbnail || `/themes/${theme.renderer}/${theme.path}/dropdownTexture.png`; return
this.handleTheme(theme)} title={''}> {theme.author ?? renderer} : {theme.name}
{theme.name} preview
; }); }; const currentRenderer = this.props.metadata.renderer; const currentTheme = mergedThemes[`${_.upperFirst(this.props.metadata.renderer)}`][this.props.metadata.theme] ?? { name: `!!! THEME MISSING !!! ID=${this.props.metadata.theme}` }; let dropdown; if(currentRenderer == 'legacy') { dropdown =
{`Themes are not supported in the Legacy Renderer`}
; } else { dropdown =
{currentTheme.author ?? _.upperFirst(currentRenderer)} : {currentTheme.name}
{listThemes(currentRenderer)}
; } return
{dropdown}
; }, renderLanguageDropdown : function(){ const langCodes = ['en', 'de', 'de-ch', 'fr', 'ja', 'es', 'it', 'sv', 'ru', 'zh-Hans', 'zh-Hant']; const listLanguages = ()=>{ return _.map(langCodes.sort(), (code, index)=>{ const localName = new Intl.DisplayNames([code], { type: 'language' }); const englishName = new Intl.DisplayNames('en', { type: 'language' }); return
{`${code}`}
{`${localName.of(code)}`}
; }); }; const debouncedHandleFieldChange = _.debounce(this.handleFieldChange, 500); return
this.handleLanguage(value)} onEntry={(e)=>{ e.target.setCustomValidity(''); //Clear the validation popup while typing debouncedHandleFieldChange('lang', e); }} options={listLanguages()} autoSuggest={{ suggestMethod : 'startsWith', clearAutoSuggestOnClick : true, filterOn : ['data-value', 'data-detail', 'title'] }} > Sets the HTML Lang property for your brew. May affect hyphenation or spellcheck.
; }, renderRenderOptions : function(){ if(!global.enable_v3) return; return
Click here to see the demo page for the old Legacy renderer!
; }, render : function(){ return

Properties Editor

this.handleFieldChange('title', e)} />