A clean, Markdown-based publishing platform made for writers. Write together and build a community.
Find a file
Michael Demetriou 6cca097de0 Needs testing: frontend (javascript) code for image uploads.
I was trying to create the image upload functionality of *writefreely*. The spec said that after the user drops an image on the textarea a markdown image placeholder should appear `[image](file.png)`, just like it happens on [discourse](https://www.discourse.org/).

However there were a few challenges. *Writefreely* is a blogging platform, not a forum, so longer texts are possible, if not common. Appending the markdown at the end of the text, especially if the text overflows the viewport is bad user interface as the user will have no confirmation whether his action had a result or not (the text would have been appended off-screen). Also, in blog posts we often append larger images which take some time to upload, so a progress indicator would be useful.

Another challenge was multiple uploads. Since additional markup isn't allowed inside the textarea I had to find a way to update the right markdown block when the respective file upload updates or finishes.

Lastly the default action when one hovers a file over a textarea, the browser insinuates that the file will be dropped in a specific place under the mouse cursor by showing a text cursor in the text. However it doesn't give the developers any way to know where that text will be dropped. It does drop the local `file://` URI however so we can replace that with our markdown. What if the user had the same `file://` uri elsewhere in the text, however? Perhaps he was writing about including local files in markdown documents.

The above function does not work reliably in Chrome on GNOME. Sometimes it inserts the `file://` URI under the cursor and sometimes it navigates to the image directly. So I'll probably have to browser-sniff there (bad, I know)

So the solution had to deal with the following stuff:

1. Insert the image in the last editing position unless the browser allows you to drop at a specific place
2. If the browser allows you to drop at a specific place, do it.
3. Update the markdown with progress and when the upload is done go replace the loading thing with the remote filename the server returned

This is simple. You can use `textarea.selectionStart` and `textarea.selectionEnd` to get the selection index (if text is selected) or the cursor position if these two values are equal. If text is selected replace with the dropped image.

This was not so simple and despite being able to create a [demo](http://qwazix.com/shared/firefox%20dropping%20in%20place.webm) this did not work for more than one files (this is fixable) nor it could work in chrome (this doesn't seem fixable, chrome sometimes drops the `file://` URI in the textarea and others navigates to it). Thus I'm inclined to drop this functionality as the code for it is dirty anyway. [And then I found out that this doesn't work on Windows/MacOS so yeah...][Now I have dropped this]

<video><source src="http://qwazix.com/shared/firefox%20dropping%20in%20place.webm" type="video/webm"></video>

However I'd like to talk about my solution: in the drop event `textarea.value` still holds the text without the `file://` URI. However ten milliseconds later the text has already updated. Here's why the dirty code: a `setTimeout({...},10)` that allow us to get to the updated text. We can then iterate over all the occurrences of `file://.*?/filename.ext` and check if they exist in the previous text. If we find one that doesn't then we can replace that with our markdown. This is the code:

``` javascript
        function insertTextAtDropPoint(text, textarea, filename){
            if (textarea.nodeName == "TEXTAREA") {
                // save cursor position so that we can set it
                // back after the operation on value
                var startingPosition = textarea.selectionStart;
                var rxp = new RegExp('file://.*?/'+filename);
                console.log(rxp);
                textarea.value = replaceNewPatternInString(rxp, oldtext, textarea.value, text);
                // set the cursor to its original position
                textarea.selectionStart = textarea.selectionEnd = startingPosition + text.length;
            } else throw "Element is not a textarea"
        }

function replaceNewPatternInString(pattern, oldstring, newstring, replacement){
            var index = 0;
            do {
                // find first occurence of pattern
                var match = newstring.substr(index, newstring.length).match(pattern);
                index += match.index;
                // take the part of the string from the beginning until
                // the end of the pattern
                // and check if you can find it in the old string
                // if you can go to the next occurence of the pattern
                // until you find one
                // that doesn't exist in the old string.
                index += match[0].length;
            } while (oldstring.indexOf(newstring.substr(0, index)) != -1);
            return newstring.substr(0,index-match[0].length) + replacement + newstring.substr(index,newstring.length);
        }
```

Still, believing that the file will be dropped under the cursor (which is probably the user's expectation when they see the moving **I** cursor) and then have it dropped in the last edit position is not good UX. Chrome disables that animation if you `preventDefault()` on `dragover` but Firefox does not so I'll have to resort to using `pointer-events` to disable that functionality. [Update: it looks like doing `preventDefault()` on `dragenter` resolves this nicely]

We don't have additional markup inside the textarea so if the user drops multiple files we need to know which one to update when the upload progresses or finishes. *Discourse* solves this by using filenames and appending numbers to them if it finds the same filename being already used. I decided that creating a temporary hash is simpler so I prepend one before each filename, making the markdown unique. Then it's a matter of search and replace to update progress.

Replacing text in the textarea entails getting the contents, changing them in javascript and putting them back in. This causes the spellcheck to fire again creating a subtle flicker in the wavy red lines. I was testing with *lorem ipsum* which has wavy red lines everywhere and that flicker quickly became annoying. Updating the progress every 5, or even 10% makes that annoyance a lot less noticeable and I suspect that in regular text with much less misspelled words this will not be an issue.

Finally, after each change we also need to put the cursor back to where it was because updating `textarea.value` sets the cursor to the end.

This was surprisingly easy. I just copied everything into `pad.tmpl` and it (almost) just worked. I had to point the `xhr` endpoint to my `upload.php` test file and enable **CORS** there but this was trivial.

 ## Backend

Next step: backend image handling.

Also posted here: https://mixt.qwazix.com/d/naac7l07tr
Individual commits while I developed this: https://github.com/qwazix/minimalist-ajax-upload/commits/dragDrop
2019-07-12 15:03:19 +03:00
.github Automatically assign "bug?" label to bug reports 2019-03-14 09:43:48 -04:00
author Fix IsValidUsername check when PagesParentDir isn't current dir 2019-03-06 10:44:32 -05:00
cmd/writefreely Fix whitespace 2019-07-01 13:33:26 -04:00
config Merge pull request #123 from writeas/private-instance 2019-07-01 19:14:20 -04:00
key Add TODO for multierror 2019-06-14 19:12:14 -04:00
keys Move key generation to app from keys.sh 2018-11-11 17:52:24 -05:00
less Explicitly set background-color 2019-06-28 08:26:15 -04:00
migrations Run migrations on db initialization 2019-04-15 13:48:19 -04:00
page Hide Reader link on private instance when unauth'd 2019-06-16 20:29:31 -04:00
pages Make landing page dynamic 2019-06-27 17:06:37 -04:00
parse Describe package parse 2019-01-18 11:36:56 -05:00
scripts fix: update script: non-standard version numbers 2019-06-14 17:26:56 -07:00
static Move MathJax to git submodule 2019-05-14 07:50:37 -04:00
templates Needs testing: frontend (javascript) code for image uploads. 2019-07-12 15:03:19 +03:00
.dockerignore added .git to make builds cache more effectively and run faster 2018-11-22 06:56:29 -06:00
.gitignore Fix go-bindata error in Travis build 2019-04-06 10:45:19 -04:00
.gitmodules Use HTTP for MathJax submodule 2019-05-14 07:58:36 -04:00
.travis.yml Fix go-bindata error in Travis build 2019-04-06 10:45:19 -04:00
account.go Move key generation and Keychain to key pkg 2019-06-13 13:47:28 -04:00
activitypub.go Fix more missing hostNames 2019-06-20 21:08:30 -04:00
activitypub_test.go fixes issue #100 - can't follow from pubgate 2019-05-21 07:02:35 -07:00
admin.go Rename getLandingPage -> getLandingBody 2019-06-27 22:22:21 -04:00
app.go Accept Apper in writefreely.ResetPassword() 2019-07-03 14:39:43 -04:00
auth.go Fix spacing around copyright notices 2018-12-31 01:05:26 -05:00
AUTHORS.md Add @nkoehring to AUTHORS 2019-01-24 17:24:07 -05:00
bindata-lib.go Include schema.sql when built with wflib tag 2019-06-13 20:25:30 -04:00
cache.go Fix spacing around copyright notices 2018-12-31 01:05:26 -05:00
collections.go Remove global hostName var 2019-06-14 18:54:04 -04:00
config.ini.example Make WriteFreely spacing consistent 2019-04-11 21:33:33 -04:00
CONTRIBUTING.md Explain where to post questions, feedback, bugs 2018-11-11 10:30:50 -05:00
database-lib.go Allow compiling without go-sql-driver/mysql pkg 2019-06-13 13:47:27 -04:00
database-no-sqlite.go Allow compiling without go-sql-driver/mysql pkg 2019-06-13 13:47:27 -04:00
database-sqlite.go Allow compiling without go-sql-driver/mysql pkg 2019-06-13 13:47:27 -04:00
database.go Fix #96 2019-06-27 18:15:58 +03:00
docker-compose.yml Improved the Docker dev workflow slightly. 2018-11-16 14:53:42 -06:00
docker-setup.sh Improved the Docker dev workflow slightly. 2018-11-16 14:53:42 -06:00
Dockerfile Install the writefreely cmd properly 2019-04-19 13:05:01 +02:00
errors.go Fix spacing around copyright notices 2018-12-31 01:05:26 -05:00
export.go Merge branch 'develop' into librarization 2019-06-13 20:44:55 -04:00
feed.go Remove global hostName var 2019-06-14 18:54:04 -04:00
go.mod Add go-bindata as a module dependency 2019-05-17 16:22:44 -07:00
go.sum Add go-bindata as a module dependency 2019-05-17 16:22:44 -07:00
handle.go Fix userlevel error logging 2019-07-01 16:45:35 -04:00
hostmeta.go Make App struct public 2019-05-12 17:19:38 -04:00
instance.go Fix spacing around copyright notices 2018-12-31 01:05:26 -05:00
invites.go Make App struct public 2019-05-12 17:19:38 -04:00
keys.go Break functionality out of Serve() func 2019-06-13 18:50:23 -04:00
LICENSE Use AGPL 2018-10-29 10:06:45 -04:00
Makefile Include ARMv7 build in make release 2019-07-08 08:56:25 -04:00
nodeinfo.go Add invites flag in NodeInfo 2019-04-11 20:37:01 -04:00
pad.go Make App struct public 2019-05-12 17:19:38 -04:00
pages.go Rename getLandingPage -> getLandingBody 2019-06-27 22:22:21 -04:00
postrender.go Make WriteFreely spacing consistent 2019-04-11 21:33:33 -04:00
posts.go Add comments about isRaw logic 2019-07-01 19:10:29 -04:00
read.go Remove global hostName var 2019-06-14 18:54:04 -04:00
README.md Add Documentation section and fix dev setup link 2019-07-02 11:41:43 -04:00
request.go Fix spacing around copyright notices 2018-12-31 01:05:26 -05:00
routes.go Use UserLevelReader func for read routes 2019-06-19 19:26:10 -04:00
schema.sql Set up migrations table on initial setup 2019-01-24 17:08:08 -05:00
session.go Break functionality out of Serve() func 2019-06-13 18:50:23 -04:00
sitemap.go Remove global hostName var 2019-06-14 18:54:04 -04:00
sqlite.sql Set up migrations table on initial setup 2019-01-24 17:08:08 -05:00
templates.go Break functionality out of Serve() func 2019-06-13 18:50:23 -04:00
unregisteredusers.go Make App struct public 2019-05-12 17:19:38 -04:00
users.go Move key generation and Keychain to key pkg 2019-06-13 13:47:28 -04:00
webfinger.go Remove global hostName var 2019-06-14 18:54:04 -04:00

 

WriteFreely


Latest release Go Report Card Build status

 

WriteFreely is a beautifully pared-down blogging platform that's simple on the surface, yet powerful underneath.

It's designed to be flexible and share your writing widely, so it's built around plain text and can publish to the fediverse via ActivityPub. It's easy to install and light enough to run on a Raspberry Pi.

Try the editor

Find an instance

Features

  • Start a blog for yourself, or host a community of writers
  • Form larger federated networks, and interact over modern protocols like ActivityPub
  • Write on a fast, dead-simple, and distraction-free editor
  • Format text with Markdown
  • Organize posts with hashtags
  • Create static pages
  • Publish drafts and let others proofread them by sharing a private link
  • Create multiple lightweight blogs under a single account
  • Export all data in plain text files
  • Read a stream of other posts in your writing community
  • Build more advanced apps and extensions with the well-documented API
  • Designed around user privacy and consent

Hosting

We offer two kinds of hosting services that make WriteFreely deployment painless: Write.as for individuals, and WriteFreely.host for communities. Besides saving you time, as a customer you directly help fund WriteFreely development.

Write.as

Start a personal blog on Write.as, our flagship instance. Built to eliminate setup friction and preserve your privacy, Write.as helps you start a blog in seconds. It supports custom domains (with SSL) and multiple blogs / pen names per account. Read more here.

WriteFreely.host

WriteFreely.host makes it easy to start a close-knit community — to share knowledge, complement your Mastodon instance, or publish updates in your organization. We take care of the hosting, upgrades, backups, and maintenance so you can focus on writing.

Quick start

WriteFreely has minimal requirements to get up and running — you only need to be able to run an executable.

Note

this is currently alpha software. We're quickly moving out of this v0.x stage, but while we're in it, there are no guarantees that this is ready for production use.

To get started, head over to our Getting Started guide. For production use, jump to the Running in Production section.

Packages

WriteFreely is available in these package repositories:

Documentation

Read our full documentation on WriteFreely.org. Help us improve by contributing to the writefreely/documentation repo.

Development

Ready to hack on your site? Get started with our developer guide.

Docker

Read about using Docker in the documentation.

Contributing

We gladly welcome contributions to WriteFreely, whether in the form of code, bug reports, feature requests, translations, or documentation improvements.

Before contributing anything, please read our Contributing Guide. It describes the correct channels for submitting contributions and any potential requirements.

License

Licensed under the AGPL.