Giter Club home page Giter Club logo

Comments (4)

SjonHortensius avatar SjonHortensius commented on August 22, 2024 1

I have a POC ready - I went with iframes for the initial version. I implemented window.postMessage and allowed php.net as frame-ancestors in the csp. This allows an external domain to embed 3v4l and supply the initial code for the editor.

example of integration:

<!-- straight from the PHP manual -->
<p><strong>Example #1 Using a negative <code class="parameter" style="cursor: pointer;">offset</code></strong></p>
<div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">);    </span><span style="color: #FF8000">// returns "f"<br></span><span style="color: #0000BB">$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">2</span><span style="color: #007700">);    </span><span style="color: #FF8000">// returns "ef"<br></span><span style="color: #0000BB">$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "d"<br></span><span style="color: #0000BB">?&gt;</span></span></code></div>
</div>

<button type="button" class="run-code-on-3v4l">try it</button>

<script>
    // receives updates on status of execution
    window.addEventListener('message', (event) => {
        if (event.origin !== 'https://3v4l.org') return;

        switch (event.data) {
            case 'busy':
            case 'done':
                alert('script code is '+ event.data);
            break;
        }
    }, false);

    document.querySelectorAll('.run-code-on-3v4l').forEach(function (el) {
        el.addEventListener('click', function() {
            var i;
            //FIXME detect if an iframe has already been opened for this el
            if (true) {
                i = document.createElement('iframe');
                i.classList.add('3v4l-live');

                i.src = 'https://3v4l.org/#live';
                el.after(i);
            }

            i.addEventListener('load', (e) => {
                //FIXME make me dynamic
                var code = document.querySelectorAll('.example-contents')[0].innerText;
                i.contentWindow.postMessage(document.querySelectorAll('.example-contents')[0].textContent, 'https://3v4l.org');
            });
        });
    });
</script>

proposed change on the server side (not in production yet): SjonHortensius/3v4l_org@4137692

Additionally, I wouldn't mind documenting how to integrate the WASM binary from 3v4l directly - but the inline editing capabilities will have to be added by someone else.

from doc-en.

SjonHortensius avatar SjonHortensius commented on August 22, 2024

wasm first would be a good idea as it would reduce the load as the input & results don't need to be stored and it is resistant to abuse - I agree it would probably cover most cases. It also matches the current setup of 3v4l with the "real-time" option checked by default.

A separate endpoint on 3v4l would be needed that would accept the code and generate the correct output (via wasm) and include a "run on 3v4l" button. This would remove the needs to pre-store all examples on 3v4l and store the script-short code in the docs somewhere.
I'd prefer the layout of this to be less focused on the '3v4' button (than the current layout) as it would take users away from the docs and submit the script to the server for processing in all versions - which this integration doesn't focus on

from doc-en.

Jeckerson avatar Jeckerson commented on August 22, 2024

There is already OSS project, which uses WebAssembly:

Repository: https://github.com/seanmorris/php-wasm
Demo: https://seanmorris.github.io/php-wasm/

from doc-en.

pronskiy avatar pronskiy commented on August 22, 2024

Many projects are doing that, e.g. https://github.com/WordPress/wordpress-playground.

3v4l.org also already implements that capability. See the Live preview checkbox in the right-bottom corner.

Ideally, I envision being able to embed 3v4l.org snippets on the php.net page such that they initially appear static. Then, upon clicking, they load php-wasm behind the scenes, making the example editable.

from doc-en.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.