Skip to content

Commit

Permalink
Porting existing code from another project with up-to-date dependencies
Browse files Browse the repository at this point in the history
- Ported existing code from another project with up-to-date dependencies.
- Currently trying to integrating the embedding of images and other media using InsertEmbedModal and InsertMediaModal
  • Loading branch information
LoveDuckie committed Jun 26, 2024
1 parent e9b9c4f commit 5464626
Show file tree
Hide file tree
Showing 28 changed files with 24,457 additions and 109 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"search.exclude": {
"client/dist/**": true,
"vendor/**": true
}
}
23,605 changes: 23,604 additions & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion client/dist/styles/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions client/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// my-module/client/src/App.js
import React from 'react';
import MarkdownEditorComponent from './components/MarkdownEditorComponent/MarkdownEditorComponent';

const App = (props) => (
<div id="bootstrapped_markdown_editor_component">
<MarkdownEditorComponent
textArea={props.textArea}
toolbar={props.toolbar}
identifier={props.editorDataConfig.identifier}/>
</div>
);

export default App;
53 changes: 51 additions & 2 deletions client/src/boot/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
/* global window */
import React from 'react';
import { createRoot } from 'react-dom/client';
import $ from 'jquery';
import { ApolloProvider } from '@apollo/client';
import Injector, { provideInjector } from 'lib/Injector';
import registerComponents from 'boot/registerComponents';

window.document.addEventListener('DOMContentLoaded', () => {
registerComponents();
import App from 'App';

registerComponents();

Injector.ready(() => {
// const ss = typeof window.ss !== 'undefined' ? window.ss : {};
const { apolloClient, store } = window.ss;
const AppWithInjector = provideInjector(App);

$('.js-markdown-container:visible').entwine({
ReactRoot: null,

onmatch() {
this._super();
this.refresh();
},

onunmatch() {
this._super();
const root = this.getReactRoot();
if (root) {
root.unmount();
this.setReactRoot(null);
}
},

refresh() {
const textArea = $(this).parent().find('textarea')[0];
const editorDataConfig = JSON.parse(textArea.dataset.config);
const toolbar = window.ss.markdownConfigs.readToolbarConfigs(
editorDataConfig.toolbar
);

const root = createRoot(this[0]);
this.setReactRoot(root);
root.render(
<ApolloProvider client={apolloClient} store={store}>
<AppWithInjector
textArea={textArea}
editorDataConfig={editorDataConfig}
toolbar={toolbar}
/>
</ApolloProvider>
);
},
});
});
2 changes: 0 additions & 2 deletions client/src/boot/registerComponents.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import Injector from 'lib/Injector';
import ExampleComponent from 'components/ExampleComponent/ExampleComponent';
import MarkdownEditorComponent from 'components/MarkdownEditorComponent/MarkdownEditorComponent';

export default () => {
Injector.component.registerMany({
// List your React components here so Injector is aware of them
ExampleComponent,
MarkdownEditorComponent
});
};
5 changes: 3 additions & 2 deletions client/src/bundles/bundle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Include any legacy Entwine wrappers
import 'entwine/example-file';
import 'entwine/Markdown_ssembed';
import 'entwine/Markdown_ssmedia';

// Include boot entrypoint
import 'boot';
import 'boot/index';
7 changes: 0 additions & 7 deletions client/src/components/ExampleComponent/ExampleComponent.js

This file was deleted.

3 changes: 0 additions & 3 deletions client/src/components/ExampleComponent/ExampleComponent.scss

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';

import ReactDOM from 'react-dom';

import SimpleMdeReact from 'react-simplemde-editor';
import 'easymde/dist/easymde.min.css';
import jQuery from 'jquery';
import ShortCodeParser from '../ShortCodeParser/ShortCodeParser';

Expand All @@ -12,15 +11,17 @@ const EasyMDE = require('easymde');
const parser = new ShortCodeParser();

const ss = typeof window.ss !== 'undefined' ? window.ss : {};

if (typeof ss.markdownConfigs === 'undefined') {
ss.markdownConfigs = {};
}

ss.markdownConfigs.readToolbarConfigs = function (data) {
const toolbar = [];
for (let i = 0; i < data.length; i++) {
// for (const key in data) {
const key = data[i];
const dataKeys = Object.keys(data);
for (let i = 0; i < dataKeys.length; i++) {
// for (const key in data) {
const key = dataKeys[i];
const element = data[key];

if (typeof element === 'string') {
Expand Down Expand Up @@ -57,14 +58,11 @@ parser.registerShortCode(
`<img src="${opts.thumbnail} width="${opts.width} height="${opts.height}">`
);

class MarkdownEditorField extends React.Component {
class MarkdownEditorComponent extends React.Component {
constructor(props) {
super(props);
this.state = ss.markdownConfigs;
}

handleChange(value) {
this.props.textarea.value = value;
this._handleChange = this._handleChange.bind(this);
}

previewRender(plainText, preview) {
Expand All @@ -74,6 +72,12 @@ class MarkdownEditorField extends React.Component {
return this.parent.markdown(parsedText);
}

_handleChange(value) {
console.log(value);
console.log(this.props.textArea);
this.props.textArea.value = value;
}

static addCustomAction(key, action) {
ss.markdownConfigs[key] = action;
}
Expand All @@ -91,9 +95,10 @@ class MarkdownEditorField extends React.Component {
return (
<div className="editor-container">
<SimpleMdeReact
value={this.props.textarea.value}
onChange={this.handleChange.bind(this)}
value={this.props.textArea.value}
onChange={this._handleChange}
options={{
// autoDownloadFontAwesome: false,
spellChecker: true,
dragDrop: false,
keyMap: 'sublime',
Expand All @@ -107,61 +112,40 @@ class MarkdownEditorField extends React.Component {
}
}

window.MarkdownEditorField = MarkdownEditorField;
window.MarkdownEditorField = MarkdownEditorComponent;

jQuery.entwine('ss', ($) => {
MarkdownEditorField.addCustomAction('ssEmbed', (editor) => {
MarkdownEditorComponent.addCustomAction('ssEmbed', (editor) => {
if (window.InsertMediaModal) {
let dialog = $('#insert-md-embed-react__dialog-wrapper');
if (!dialog.length) {
dialog = $('<div id="insert-md-embed-react__dialog-wrapper" />');
$('body').append(dialog);
}
console.log(dialog);
console.log(editor);
dialog.setElement(editor);
dialog.open();
} else {
alert('Media embed is not supported');
}
});

MarkdownEditorField.addCustomAction('ssImage', (editor) => {
MarkdownEditorComponent.addCustomAction('ssImage', (editor) => {
if (window.InsertMediaModal) {
let dialog = $('#insert-md-media-react__dialog-wrapper');
if (!dialog.length) {
dialog = $('<div id="insert-md-media-react__dialog-wrapper" />');
$('body').append(dialog);
}
console.log(dialog);
console.log(editor);
dialog.setElement(editor);
dialog.open();
} else {
EasyMDE.drawImage(editor);
}
});

$('.js-markdown-container:visible').entwine({
onunmatch() {
this._super();
ReactDOM.unmountComponentAtNode(this[0]);
},
onmatch() {
this._super();
this.refresh();
},
refresh() {
const textArea = $(this).parent().find('textarea')[0];
const data = JSON.parse(textArea.dataset.config);
const toolbar = ss.markdownConfigs.readToolbarConfigs(data.toolbar);

ReactDOM.render(
<MarkdownEditorField
textarea={textArea}
toolbar={toolbar}
identifier={data.identifier}
/>,
this[0]
);
},
});
});

export default MarkdownEditorField;
export default MarkdownEditorComponent;
Empty file.
9 changes: 6 additions & 3 deletions client/src/components/ShortCodeParser/ShortCodeParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ class ShortCodeParser {
}

parse(plainText) {
for (const name in this.shortCodes) {
const shortCodeKeys = Object.keys(this.shortCodes);
// for (const name in this.shortCodes) {
for (let i = 0; i < shortCodeKeys.length; i++) {
const name = shortCodeKeys[i];
const regex = {
wrapper: new RegExp(
util.format(SHORTCODE_OPEN, name) +
Expand All @@ -123,8 +126,8 @@ class ShortCodeParser {
let matches = plainText.match(regex.wrapper);

if (matches) {
for (let m, data, i = 0, len = matches.length; i < len; i++) {
m = matches[i];
for (let m, data, j = 0, len = matches.length; j < len; j++) {
m = matches[j];
data = this.parseShortcode(name, m);
plainText = plainText.replace(
m,
Expand Down
Loading

0 comments on commit 5464626

Please sign in to comment.