Tips for adding styling to HTML in Vue by entering CSS code into the CodeMirror editor

Is there a way to style HTML by entering CSS code into the Codemirror editor? For instance, if we have the HTML code <head> </head>, how can I apply the CSS code, head{ color : red }, typed in the Codemirror editor to stylize this HTML code?

mounted(){
  
    this.htmlCode = CodeMirror.fromTextArea(document.getElementById('editor'),{
      lineNumbers: true,  
      theme: 'dracula',
      mode: 'xml',
      autoCloseTags: true,
    })
    this.cssCode = CodeMirror.fromTextArea(document.getElementById('editor2'),{
      lineNumbers: true,  
      theme: 'dracula',
      mode: 'css',  
      autoCloseTags: true, 
    })
  };

methods : {
    clickRun(){
        let htmlCode = this.htmlCode.getValue()
        let cssCode = this.cssCode.getValue()
        let previewWindow = document.getElementById('preview').contentWindow.document      
        let cssAdd = previewWindow.head.append("<style type='text/css'>" + cssCode + "</style>")
  
        
        previewWindow.open();
        previewWindow.write(htmlCode + cssAdd);
        previewWindow.close();
      
    },
}

I tried console logging this code:

console.log(previewWindow.head)
///<head>
///   "<style type='text/css'>h1{color:red;} </style>"
///</head>

console.log(cssCode)
/// h1 {
///  color:red;
/// }

console.log(cssAdd)

/// undefined

Answer №1

There are certain elements such as cm-editor, cm-scroller, cm-cursor, etc., that you can style, but the default formatting of CodeMirror is more restricted. Themes can be customized to some extent. Themes are defined using EditorView.theme. This function takes an object with CSS selectors as properties and styles as values, returning an extension that applies the theme.

For more information, you can refer to the complete documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can eslint be used to enforce a particular named export?

Is there a way to use eslint to make it mandatory for JavaScript/TypeScript files to have a named export of a specific name? For instance, in the src/pages folder, I want all files to necessitate an export named config: Example of incorrect usage src/page ...

Utilize AngularJS to integrate a service into the router functionality

What is the best way to inject a service into my router so that its JSON result will be accessible throughout the entire application? Router: export default ['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterP ...

Steps to link a specific section of a Google pie chart:

I have a question that may be a little confusing, so let me explain. I recently used Google Charts to create a simple pie chart and it worked perfectly. Now, I am trying to figure out how to link each section/part of the pie chart to display a jQuery moda ...

Utilizing Office.js: Incorporating Angular CLI to Call a Function in a Generated Function-File

After using angular-cli to create a new project, I integrated ng-office-ui-fabric and its dependencies. I included in index.html, added polyfills to angular.json, and everything seemed to be working smoothly. When testing the add-in in Word, the taskpane ...

JavaScript library for making HTTP requests

Can someone provide guidance on creating a JavaScript command line application that interacts with a public API using an HTTP client library? What is the preferred JavaScript HTTP library for this task? ...

Update your MySQL database with ease by leveraging the power of AJAX through a dropdown menu

Can you provide guidance on updating a MySQL database using a dropdown menu and Ajax without reloading the entire webpage? I am facing issues with implementing the code, even after referring to various tutorials. Below is a snippet of my PHP script within ...

What is the best way to retrieve information from a local JSON file and store it in the state of

I am currently working on a project to develop a movies/series search app using Next.js and React based class components. I have successfully imported JSON content and displayed it using the map function as shown below: <div> {Appletv.shows.map(( ...

Issue encountered in React: Unable to access object value within ComponentDidUpdate method

I'm struggling to retrieve the value from an object key. componentDidUpdate(prevProps) { if (prevProps !== this.props) { console.log("component did update in top menu", this.props.topmenudata[0]) this.setState({ ...

Retrieve data from the redux store within the components nested under redux-simple-router

I am currently working on finding a way to access the redux store within a route in order to dispatch actions directly from that location. Below is an example of how my main Component is structured: class App extends Component { render() { return ( ...

What is the best way to verify the invocation of a Vuex Mutation within a Vue component?

Imagine you have a Vue component with a method structured like this: methods:{ doSomething(someParameter){ //potentially manipulate the parameter in some way this.$store.commit("storeSomething",someParameter); let someP ...

Introduce a pause interval between successive ajax get calls

I've created a script that uses an ajax GET request when the user reaches near the end of the page. $(function(){ window.addEventListener('scroll', fetchImages); window.addEventListener('scroll', fetchNotifications); }); ...

tips for using Node Mailer to send emails without using SMTP

Currently, I am facing an issue with sending emails through nodemailer. Although I have successfully used my gmail account for this purpose in the past, I now wish to switch to using my business email to communicate with clients on a regular basis. The cu ...

Using Javascript to Highlight a Single Row in a Table

Greetings esteemed members of the skilled community at StackOverflow, I must humbly ask for your expertise in solving a dilemma that I am currently facing. The situation is as follows: I have a table generated from an SQL query, and it is crucial for the ...

Issue: Attempting to write data after reaching the end in Node.js while using

I have encountered the following error: Heading Caught exception: Error: write after end at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15) at ServerResponse.res.write (/home/projectfolder/node_modules/express/node_modules/connect/lib/mid ...

What is the method employed by the script to ascertain the value of n within the function(n)?

I've recently started learning about jQuery. I came across a program online that uses a function where the value of n starts from 0 and goes up to the total number of elements. In the example below, there is only one img element and jQuery targets thi ...

Can an action be activated when the mouse comes to a halt?

Currently, I am having trouble triggering an event when a user stops moving their mouse over a div element. The current event is set up to show and follow another element along with the mouse movement, but I want it to only display when the mouse stops mov ...

Making a REST call with values containing an apostrophe

Currently, I am utilizing REST and ajax to retrieve data from SharePoint using the URL below: https:xxxxxxxx/_vti_bin/ListData.svc/RMSD_Tasks?$orderby=IssueValue asc,StatusValue desc&$filter="+dropValue+" eq '"+secondFilterVal+"'&groupby ...

JavaScript client receives a response from the server

After filling out an HTML form and submitting it, the client validates the data (such as checking if the EULA checkbox is accepted) before sending it to the server. The server then checks the data and returns a status code. But how can I retrieve this stat ...

When a block is clicked, jQuery will reveal that block while hiding the others sequentially, starting with the second block, then the third, and finally the fourth

Creating a navigation menu with 4 blocks can be a bit tricky, especially when trying to show one block at a time upon click. Here is my code attempt, but unfortunately it's not working as expected. I would greatly appreciate any help or suggestions on ...

Adjust the Appearance of Highcharts Legend Post-Rendering

After a Highchart is rendered, is it possible to modify the display settings without redrawing the chart? For instance, I would like to relocate the legend box from the right to the bottom upon screen resize, as illustrated in this image: --Example Pictur ...