-
-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add toolbar support for admin panel (#36)
- Loading branch information
1 parent
547cce9
commit 16d5cc1
Showing
9 changed files
with
120 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/admin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import app from 'flarum/admin/app'; | ||
import { initialize } from '../common/index'; | ||
|
||
app.initializers.add('flarum-markdown', initialize); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/*! | ||
* Includes modified code from GitHub Markdown Toolbar Element | ||
* https://github.com/github/markdown-toolbar-element/ | ||
* | ||
* Original Copyright GitHub, Inc. | ||
* Released under the MIT license | ||
* https://github.com/github/markdown-toolbar-element/blob/master/LICENSE | ||
*/ | ||
|
||
import app from 'flarum/common/app'; | ||
import { extend, override } from 'flarum/common/extend'; | ||
import TextEditor from 'flarum/common/components/TextEditor'; | ||
import BasicEditorDriver from 'flarum/common/utils/BasicEditorDriver'; | ||
import styleSelectedText from 'flarum/common/utils/styleSelectedText'; | ||
|
||
import MarkdownToolbar from './components/MarkdownToolbar'; | ||
import MarkdownButton from './components/MarkdownButton'; | ||
import ItemList from 'flarum/common/utils/ItemList'; | ||
|
||
const modifierKey = navigator.userAgent.match(/Macintosh/) ? '⌘' : 'ctrl'; | ||
|
||
const styles = { | ||
header: { prefix: '### ' }, | ||
bold: { prefix: '**', suffix: '**', trimFirst: true }, | ||
italic: { prefix: '_', suffix: '_', trimFirst: true }, | ||
strikethrough: { prefix: '~~', suffix: '~~', trimFirst: true }, | ||
quote: { prefix: '> ', multiline: true, surroundWithNewlines: true }, | ||
code: { prefix: '`', suffix: '`', blockPrefix: '```', blockSuffix: '```' }, | ||
link: { prefix: '[', suffix: '](https://)', replaceNext: 'https://', scanFor: 'https?://' }, | ||
image: { prefix: '![', suffix: '](https://)', replaceNext: 'https://', scanFor: 'https?://' }, | ||
unordered_list: { prefix: '- ', multiline: true, surroundWithNewlines: true }, | ||
ordered_list: { prefix: '1. ', multiline: true, orderedList: true }, | ||
spoiler: { prefix: '>!', suffix: '!<', blockPrefix: '>! ', multiline: true, trimFirst: true }, | ||
}; | ||
|
||
const applyStyle = (id, editorDriver) => { | ||
// This is a nasty hack that breaks encapsulation of the editor. | ||
// In future releases, we'll need to tweak the editor driver interface | ||
// to support triggering events like this. | ||
styleSelectedText(editorDriver.el, styles[id]); | ||
}; | ||
|
||
function makeShortcut(id, key, editorDriver) { | ||
return function (e) { | ||
if (e.key === key && ((e.metaKey && modifierKey === '⌘') || (e.ctrlKey && modifierKey === 'ctrl'))) { | ||
e.preventDefault(); | ||
applyStyle(id, editorDriver); | ||
} | ||
}; | ||
} | ||
|
||
function markdownToolbarItems(oldFunc) { | ||
const items = typeof oldFunc === 'function' ? oldFunc() : new ItemList(); | ||
|
||
function tooltip(name, hotkey) { | ||
return app.translator.trans(`flarum-markdown.lib.composer.${name}_tooltip`) + (hotkey ? ` <${modifierKey}-${hotkey}>` : ''); | ||
} | ||
|
||
const makeApplyStyle = (id) => { | ||
return () => applyStyle(id, this.attrs.composer.editor); | ||
}; | ||
|
||
items.add('header', <MarkdownButton title={tooltip('header')} icon="fas fa-heading" onclick={makeApplyStyle('header')} />, 1000); | ||
items.add('bold', <MarkdownButton title={tooltip('bold', 'b')} icon="fas fa-bold" onclick={makeApplyStyle('bold')} />, 900); | ||
items.add('italic', <MarkdownButton title={tooltip('italic', 'i')} icon="fas fa-italic" onclick={makeApplyStyle('italic')} />, 800); | ||
items.add( | ||
'strikethrough', | ||
<MarkdownButton title={tooltip('strikethrough')} icon="fas fa-strikethrough" onclick={makeApplyStyle('strikethrough')} />, | ||
700 | ||
); | ||
items.add('quote', <MarkdownButton title={tooltip('quote')} icon="fas fa-quote-left" onclick={makeApplyStyle('quote')} />, 600); | ||
items.add('spoiler', <MarkdownButton title={tooltip('spoiler')} icon="fas fa-exclamation-triangle" onclick={makeApplyStyle('spoiler')} />, 500); | ||
items.add('code', <MarkdownButton title={tooltip('code')} icon="fas fa-code" onclick={makeApplyStyle('code')} />, 400); | ||
items.add('link', <MarkdownButton title={tooltip('link')} icon="fas fa-link" onclick={makeApplyStyle('link')} />, 300); | ||
items.add('image', <MarkdownButton title={tooltip('image')} icon="fas fa-image" onclick={makeApplyStyle('image')} />, 200); | ||
items.add( | ||
'unordered_list', | ||
<MarkdownButton title={tooltip('unordered_list')} icon="fas fa-list-ul" onclick={makeApplyStyle('unordered_list')} />, | ||
100 | ||
); | ||
items.add('ordered_list', <MarkdownButton title={tooltip('ordered_list')} icon="fas fa-list-ol" onclick={makeApplyStyle('ordered_list')} />, 0); | ||
|
||
return items; | ||
} | ||
|
||
export function initialize(app) { | ||
extend(BasicEditorDriver.prototype, 'keyHandlers', function (items) { | ||
items.add('bold', makeShortcut('bold', 'b', this)); | ||
items.add('italic', makeShortcut('italic', 'i', this)); | ||
}); | ||
|
||
if (TextEditor.prototype.markdownToolbarItems) { | ||
override(TextEditor.prototype, 'markdownToolbarItems', markdownToolbarItems); | ||
} else { | ||
TextEditor.prototype.markdownToolbarItems = markdownToolbarItems; | ||
} | ||
|
||
extend(TextEditor.prototype, 'toolbarItems', function (items) { | ||
items.add( | ||
'markdown', | ||
<MarkdownToolbar for={this.textareaId} setShortcutHandler={(handler) => (shortcutHandler = handler)}> | ||
{this.markdownToolbarItems().toArray()} | ||
</MarkdownToolbar>, | ||
100 | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,4 @@ | ||
/*! | ||
* Includes modified code from GitHub Markdown Toolbar Element | ||
* https://github.com/github/markdown-toolbar-element/ | ||
* | ||
* Original Copyright GitHub, Inc. | ||
* Released under the MIT license | ||
* https://github.com/github/markdown-toolbar-element/blob/master/LICENSE | ||
*/ | ||
|
||
import app from 'flarum/forum/app'; | ||
import { extend, override } from 'flarum/common/extend'; | ||
import TextEditor from 'flarum/common/components/TextEditor'; | ||
import BasicEditorDriver from 'flarum/common/utils/BasicEditorDriver'; | ||
import styleSelectedText from 'flarum/common/utils/styleSelectedText'; | ||
|
||
import MarkdownToolbar from './components/MarkdownToolbar'; | ||
import MarkdownButton from './components/MarkdownButton'; | ||
import ItemList from 'flarum/common/utils/ItemList'; | ||
|
||
const modifierKey = navigator.userAgent.match(/Macintosh/) ? '⌘' : 'ctrl'; | ||
|
||
const styles = { | ||
header: { prefix: '### ' }, | ||
bold: { prefix: '**', suffix: '**', trimFirst: true }, | ||
italic: { prefix: '_', suffix: '_', trimFirst: true }, | ||
strikethrough: { prefix: '~~', suffix: '~~', trimFirst: true }, | ||
quote: { prefix: '> ', multiline: true, surroundWithNewlines: true }, | ||
code: { prefix: '`', suffix: '`', blockPrefix: '```', blockSuffix: '```' }, | ||
link: { prefix: '[', suffix: '](https://)', replaceNext: 'https://', scanFor: 'https?://' }, | ||
image: { prefix: '![', suffix: '](https://)', replaceNext: 'https://', scanFor: 'https?://' }, | ||
unordered_list: { prefix: '- ', multiline: true, surroundWithNewlines: true }, | ||
ordered_list: { prefix: '1. ', multiline: true, orderedList: true }, | ||
spoiler: { prefix: '>!', suffix: '!<', blockPrefix: '>! ', multiline: true, trimFirst: true }, | ||
}; | ||
|
||
const applyStyle = (id, editorDriver) => { | ||
// This is a nasty hack that breaks encapsulation of the editor. | ||
// In future releases, we'll need to tweak the editor driver interface | ||
// to support triggering events like this. | ||
styleSelectedText(editorDriver.el, styles[id]); | ||
}; | ||
|
||
function makeShortcut(id, key, editorDriver) { | ||
return function (e) { | ||
if (e.key === key && ((e.metaKey && modifierKey === '⌘') || (e.ctrlKey && modifierKey === 'ctrl'))) { | ||
e.preventDefault(); | ||
applyStyle(id, editorDriver); | ||
} | ||
}; | ||
} | ||
|
||
function markdownToolbarItems(oldFunc) { | ||
const items = typeof oldFunc === 'function' ? oldFunc() : new ItemList(); | ||
|
||
function tooltip(name, hotkey) { | ||
return app.translator.trans(`flarum-markdown.forum.composer.${name}_tooltip`) + (hotkey ? ` <${modifierKey}-${hotkey}>` : ''); | ||
} | ||
|
||
const makeApplyStyle = (id) => { | ||
return () => applyStyle(id, this.attrs.composer.editor); | ||
}; | ||
|
||
items.add('header', <MarkdownButton title={tooltip('header')} icon="fas fa-heading" onclick={makeApplyStyle('header')} />, 1000); | ||
items.add('bold', <MarkdownButton title={tooltip('bold', 'b')} icon="fas fa-bold" onclick={makeApplyStyle('bold')} />, 900); | ||
items.add('italic', <MarkdownButton title={tooltip('italic', 'i')} icon="fas fa-italic" onclick={makeApplyStyle('italic')} />, 800); | ||
items.add( | ||
'strikethrough', | ||
<MarkdownButton title={tooltip('strikethrough')} icon="fas fa-strikethrough" onclick={makeApplyStyle('strikethrough')} />, | ||
700 | ||
); | ||
items.add('quote', <MarkdownButton title={tooltip('quote')} icon="fas fa-quote-left" onclick={makeApplyStyle('quote')} />, 600); | ||
items.add('spoiler', <MarkdownButton title={tooltip('spoiler')} icon="fas fa-exclamation-triangle" onclick={makeApplyStyle('spoiler')} />, 500); | ||
items.add('code', <MarkdownButton title={tooltip('code')} icon="fas fa-code" onclick={makeApplyStyle('code')} />, 400); | ||
items.add('link', <MarkdownButton title={tooltip('link')} icon="fas fa-link" onclick={makeApplyStyle('link')} />, 300); | ||
items.add('image', <MarkdownButton title={tooltip('image')} icon="fas fa-image" onclick={makeApplyStyle('image')} />, 200); | ||
items.add( | ||
'unordered_list', | ||
<MarkdownButton title={tooltip('unordered_list')} icon="fas fa-list-ul" onclick={makeApplyStyle('unordered_list')} />, | ||
100 | ||
); | ||
items.add('ordered_list', <MarkdownButton title={tooltip('ordered_list')} icon="fas fa-list-ol" onclick={makeApplyStyle('ordered_list')} />, 0); | ||
|
||
return items; | ||
} | ||
|
||
app.initializers.add('flarum-markdown', function (app) { | ||
extend(BasicEditorDriver.prototype, 'keyHandlers', function (items) { | ||
items.add('bold', makeShortcut('bold', 'b', this)); | ||
items.add('italic', makeShortcut('italic', 'i', this)); | ||
}); | ||
|
||
if (TextEditor.prototype.markdownToolbarItems) { | ||
override(TextEditor.prototype, 'markdownToolbarItems', markdownToolbarItems); | ||
} else { | ||
TextEditor.prototype.markdownToolbarItems = markdownToolbarItems; | ||
} | ||
import { initialize } from '../common/index'; | ||
|
||
extend(TextEditor.prototype, 'toolbarItems', function (items) { | ||
items.add( | ||
'markdown', | ||
<MarkdownToolbar for={this.textareaId} setShortcutHandler={(handler) => (shortcutHandler = handler)}> | ||
{this.markdownToolbarItems().toArray()} | ||
</MarkdownToolbar>, | ||
100 | ||
); | ||
}); | ||
}); | ||
app.initializers.add('flarum-markdown', initialize); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
flarum-markdown: | ||
forum: | ||
lib: | ||
composer: | ||
bold_tooltip: Add bold text | ||
code_tooltip: Insert code | ||
|